use std::collections::{BTreeMap, BTreeSet};
use serde::{Deserialize, Serialize};
use crate::boundary::rules::Rules;
use crate::php::ClassRef;
use crate::surface::AcceptedSurface;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Reference {
pub file: String,
pub line: u32,
pub class: String,
pub internal: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Link {
pub from: String,
pub to: String,
pub refs: Vec<Reference>,
pub circular: bool,
}
impl Link {
pub fn files(&self) -> usize {
self.refs
.iter()
.map(|reference| reference.file.as_str())
.collect::<BTreeSet<_>>()
.len()
}
pub fn internal_files(&self) -> usize {
self.refs
.iter()
.filter(|reference| reference.internal)
.map(|reference| reference.file.as_str())
.collect::<BTreeSet<_>>()
.len()
}
pub fn is_problem(&self, rules: &Rules) -> bool {
(rules.no_circular_use && self.circular)
|| (rules.public_surface_only && self.internal_files() > 0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Cycle {
pub modules: Vec<String>,
pub carrier: String,
pub files: Vec<(String, usize)>,
}
pub(crate) fn circle_neighbour<'a>(links: &'a [Link], module: &str) -> Option<&'a str> {
links
.iter()
.find(|link| link.circular && link.from == module)
.map(|link| link.to.as_str())
}
pub(crate) struct ModuleSource<'a> {
pub(crate) name: &'a str,
pub(crate) on: bool,
pub(crate) namespace: &'a str,
pub(crate) surface: Option<&'a AcceptedSurface>,
pub(crate) files: Vec<(String, &'a [ClassRef])>,
pub(crate) declared: BTreeSet<&'a str>,
}
pub(crate) fn build(modules: &[ModuleSource<'_>], rules: &Rules) -> (Vec<Link>, Vec<Cycle>) {
let mut owners: Vec<&ModuleSource<'_>> = modules
.iter()
.filter(|module| !module.namespace.is_empty())
.collect();
owners.sort_by_key(|module| std::cmp::Reverse(module.namespace.len()));
let owner_of = |class: &str| -> Option<(&ModuleSource<'_>, String)> {
owners.iter().find_map(|module| {
let inside = class.strip_prefix(module.namespace)?.strip_prefix('\\')?;
Some((*module, inside.to_owned()))
})
};
let mut found: BTreeMap<(String, String), Vec<Reference>> = BTreeMap::new();
for module in modules {
for (file, refs) in &module.files {
for named in *refs {
let Some((owner, inside)) = owner_of(&named.class) else {
continue;
};
if owner.name == module.name || inside.is_empty() {
continue;
}
let as_namespace = format!("{}\\", named.class);
if !owner.declared.contains(named.class.as_str())
&& owner
.declared
.iter()
.any(|declared| declared.starts_with(&as_namespace))
{
continue;
}
found
.entry((module.name.to_owned(), owner.name.to_owned()))
.or_default()
.push(Reference {
file: file.clone(),
line: named.line,
class: named.class.clone(),
internal: rules.public_surface_only
&& module.on
&& owner.on
&& !match owner.surface {
Some(surface) => surface.has_class(&named.class),
None => rules.is_public(&inside),
},
});
}
}
}
let edges: BTreeSet<(&str, &str)> = found
.keys()
.map(|(from, to)| (from.as_str(), to.as_str()))
.collect();
let names: Vec<&str> = modules.iter().map(|module| module.name).collect();
let is_on = |name: &str| {
modules
.iter()
.any(|module| module.name == name && module.on)
};
let running: BTreeSet<(&str, &str)> = edges
.iter()
.copied()
.filter(|(from, to)| is_on(from) && is_on(to))
.collect();
let circles: Vec<Vec<String>> = circles(&names, &running)
.into_iter()
.map(|circle| circle.into_iter().map(str::to_owned).collect())
.collect();
let circle_of = |name: &str| {
circles
.iter()
.position(|circle| circle.iter().any(|member| member == name))
};
let cycles = circles
.iter()
.map(|circle| {
let mut files: Vec<(String, usize)> = circle
.iter()
.map(|member| {
let own: BTreeSet<&str> = found
.iter()
.filter(|((from, to), _)| from == member && circle.contains(to))
.flat_map(|(_, refs)| refs.iter().map(|reference| reference.file.as_str()))
.collect();
(member.clone(), own.len())
})
.collect();
files.sort();
let carrier = files
.iter()
.min_by(|a, b| a.1.cmp(&b.1).then_with(|| a.0.cmp(&b.0)))
.map(|(name, _)| name.clone())
.unwrap_or_default();
Cycle {
modules: files.iter().map(|(name, _)| name.clone()).collect(),
carrier,
files,
}
})
.collect();
let links = found
.into_iter()
.map(|((from, to), mut refs)| {
refs.sort_by(|a, b| {
b.internal
.cmp(&a.internal)
.then_with(|| a.file.cmp(&b.file))
.then_with(|| a.line.cmp(&b.line))
});
let circular = circle_of(&from).is_some() && circle_of(&from) == circle_of(&to);
Link {
from,
to,
refs,
circular,
}
})
.collect();
(links, cycles)
}
fn circles<'a>(names: &[&'a str], edges: &BTreeSet<(&'a str, &'a str)>) -> Vec<Vec<&'a str>> {
let index_of: BTreeMap<&str, usize> = names
.iter()
.enumerate()
.map(|(i, name)| (*name, i))
.collect();
let mut next: Vec<Vec<usize>> = vec![Vec::new(); names.len()];
for (from, to) in edges {
if let (Some(&from), Some(&to)) = (index_of.get(from), index_of.get(to)) {
next[from].push(to);
}
}
const UNSEEN: usize = usize::MAX;
let mut order = vec![UNSEEN; names.len()];
let mut low = vec![0; names.len()];
let mut on_stack = vec![false; names.len()];
let mut stack: Vec<usize> = Vec::new();
let mut counter = 0;
let mut groups: Vec<Vec<&str>> = Vec::new();
for start in 0..names.len() {
if order[start] != UNSEEN {
continue;
}
let mut work = vec![(start, 0)];
while let Some(&mut (node, ref mut followed)) = work.last_mut() {
if *followed == 0 {
order[node] = counter;
low[node] = counter;
counter += 1;
stack.push(node);
on_stack[node] = true;
}
if let Some(&target) = next[node].get(*followed) {
*followed += 1;
if order[target] == UNSEEN {
work.push((target, 0));
} else if on_stack[target] {
low[node] = low[node].min(order[target]);
}
continue;
}
work.pop();
if let Some(&(parent, _)) = work.last() {
low[parent] = low[parent].min(low[node]);
}
if low[node] == order[node] {
let mut group = Vec::new();
while let Some(member) = stack.pop() {
on_stack[member] = false;
group.push(names[member]);
if member == node {
break;
}
}
if group.len() > 1 {
group.sort_unstable();
groups.push(group);
}
}
}
}
groups.sort();
groups
}
pub fn columns(names: &[&str], links: &[Link]) -> Vec<Vec<String>> {
let edges: BTreeSet<(&str, &str)> = links
.iter()
.map(|link| (link.from.as_str(), link.to.as_str()))
.collect();
let circles = circles(names, &edges);
let group = |name: &str| -> String {
circles
.iter()
.find(|circle| circle.contains(&name))
.map_or_else(|| name.to_owned(), |circle| circle[0].to_owned())
};
let mut users: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
for link in links {
let (from, to) = (group(&link.from), group(&link.to));
if from != to {
users.entry(to).or_default().insert(from);
}
}
fn column(
node: &str,
users: &BTreeMap<String, BTreeSet<String>>,
known: &mut BTreeMap<String, usize>,
) -> usize {
if let Some(&column) = known.get(node) {
return column;
}
known.insert(node.to_owned(), 0);
let column = users.get(node).map_or(0, |users_of| {
users_of
.iter()
.map(|user| column(user, users, known) + 1)
.max()
.unwrap_or(0)
});
known.insert(node.to_owned(), column);
column
}
let mut known = BTreeMap::new();
let mut columns: Vec<Vec<String>> = Vec::new();
for name in names {
let at = column(&group(name), &users, &mut known);
if columns.len() <= at {
columns.resize(at + 1, Vec::new());
}
columns[at].push((*name).to_owned());
}
columns
}
#[cfg(test)]
mod tests {
use super::*;
fn named(class: &str, line: u32) -> ClassRef {
ClassRef {
class: class.to_owned(),
line,
}
}
fn source<'a>(
name: &'a str,
namespace: &'a str,
files: &'a [(&'a str, Vec<ClassRef>)],
) -> ModuleSource<'a> {
ModuleSource {
name,
on: true,
namespace,
surface: None,
files: files
.iter()
.map(|(file, refs)| ((*file).to_owned(), refs.as_slice()))
.collect(),
declared: BTreeSet::new(),
}
}
#[test]
fn a_link_is_made_of_files_that_name_classes_of_another_module() {
let checkout = [
(
"Modules/Checkout/app/A.php",
vec![
named(r"Modules\Billing\Models\Invoice", 5),
named(r"Modules\Checkout\Models\Order", 6),
],
),
(
"Modules/Checkout/app/B.php",
vec![
named(r"Modules\Billing\Contracts\Gateway", 7),
named(r"Illuminate\Support\Str", 3),
],
),
(
"Modules/Checkout/app/C.php",
vec![
named(r"Modules\Billing\Contracts", 4),
named(r"Modules\BillingExtras\Thing", 4),
],
),
];
let mut billing = source("Billing", r"Modules\Billing", &[]);
billing.declared = BTreeSet::from([
r"Modules\Billing\Contracts\Gateway",
r"Modules\Billing\Models\Invoice",
]);
let modules = [source("Checkout", r"Modules\Checkout", &checkout), billing];
let (links, cycles) = build(&modules, &Rules::default());
assert_eq!(links.len(), 1);
let link = &links[0];
assert_eq!(
(link.from.as_str(), link.to.as_str()),
("Checkout", "Billing")
);
assert!(!link.circular);
let refs: Vec<_> = link
.refs
.iter()
.map(|r| (r.file.as_str(), r.line, r.class.as_str(), r.internal))
.collect();
assert_eq!(
refs,
[
(
"Modules/Checkout/app/A.php",
5,
r"Modules\Billing\Models\Invoice",
true
),
(
"Modules/Checkout/app/B.php",
7,
r"Modules\Billing\Contracts\Gateway",
false
),
]
);
assert_eq!((link.files(), link.internal_files()), (2, 1));
assert!(link.is_problem(&Rules::default()));
assert!(cycles.is_empty());
}
#[test]
fn an_accepted_surface_takes_the_place_of_the_folder_list() {
let checkout = [(
"a.php",
vec![
named(r"Modules\Billing\Models\Invoice", 5),
named(r"Modules\Billing\Contracts\Gateway", 6),
],
)];
let surface = AcceptedSurface {
classes: vec![r"Modules\Billing\models\invoice".to_owned()],
..AcceptedSurface::default()
};
let mut billing = source("Billing", r"Modules\Billing", &[]);
billing.surface = Some(&surface);
let modules = [source("Checkout", r"Modules\Checkout", &checkout), billing];
let (links, _) = build(&modules, &Rules::default());
let internal: Vec<(&str, bool)> = links[0]
.refs
.iter()
.map(|r| (r.class.as_str(), r.internal))
.collect();
assert_eq!(
internal,
[
(r"Modules\Billing\Contracts\Gateway", true),
(r"Modules\Billing\Models\Invoice", false),
]
);
}
#[test]
fn without_the_rule_nothing_is_internal() {
let checkout = [("a.php", vec![named(r"Modules\Billing\Models\Invoice", 5)])];
let modules = [
source("Checkout", r"Modules\Checkout", &checkout),
source("Billing", r"Modules\Billing", &[]),
];
let rules = Rules {
public_surface_only: false,
..Rules::default()
};
let (links, _) = build(&modules, &rules);
assert!(!links[0].refs[0].internal);
assert!(!links[0].is_problem(&rules));
}
#[test]
fn two_modules_that_use_each_other_are_a_circle_counted_on_the_smaller_side() {
let checkout = [
(
"c1.php",
vec![named(r"Modules\Shipping\Contracts\Rates", 7)],
),
(
"c2.php",
vec![named(r"Modules\Shipping\Contracts\Requester", 6)],
),
];
let shipping = [
(
"s1.php",
vec![named(r"Modules\Checkout\Events\OrderPaid", 8)],
),
(
"s2.php",
vec![named(r"Modules\Checkout\Contracts\Lookup", 10)],
),
(
"s3.php",
vec![named(r"Modules\Checkout\Contracts\Lookup", 9)],
),
(
"s4.php",
vec![named(r"Modules\Checkout\Events\OrderShipped", 11)],
),
];
let modules = [
source("Checkout", r"Modules\Checkout", &checkout),
source("Shipping", r"Modules\Shipping", &shipping),
source("Users", r"Modules\Users", &[]),
];
let (links, cycles) = build(&modules, &Rules::default());
assert!(
links
.iter()
.all(|link| link.circular && link.is_problem(&Rules::default()))
);
assert_eq!(
cycles,
[Cycle {
modules: vec!["Checkout".into(), "Shipping".into()],
carrier: "Checkout".into(),
files: vec![("Checkout".into(), 2), ("Shipping".into(), 4)],
}]
);
let rules = Rules {
no_circular_use: false,
..Rules::default()
};
assert!(
links
.iter()
.all(|link| link.circular && !link.is_problem(&rules))
);
}
#[test]
fn a_circle_through_three_modules_is_found() {
let a = [("a.php", vec![named(r"M\B\Contracts\X", 1)])];
let b = [("b.php", vec![named(r"M\C\Contracts\X", 1)])];
let c = [
("c.php", vec![named(r"M\A\Contracts\X", 1)]),
("c2.php", vec![named(r"M\D\Contracts\X", 1)]),
];
let modules = [
source("A", r"M\A", &a),
source("B", r"M\B", &b),
source("C", r"M\C", &c),
source("D", r"M\D", &[]),
];
let (links, cycles) = build(&modules, &Rules::default());
assert_eq!(cycles.len(), 1);
assert_eq!(cycles[0].modules, ["A", "B", "C"]);
assert_eq!(cycles[0].carrier, "A");
let to_d = links.iter().find(|link| link.to == "D").unwrap();
assert!(!to_d.circular);
assert_eq!(circle_neighbour(&links, "B"), Some("C"));
assert_eq!(circle_neighbour(&links, "D"), None);
let mut modules = modules;
modules[2].on = false;
let (links, cycles) = build(&modules, &Rules::default());
assert!(cycles.is_empty());
assert!(links.iter().all(|link| !link.circular));
let a = [("a.php", vec![named(r"M\B\Contracts\X", 1)])];
let b = [
("b.php", vec![named(r"M\A\Contracts\X", 1)]),
("b2.php", vec![named(r"M\C\Contracts\X", 1)]),
];
let c = [("c.php", vec![named(r"M\B\Contracts\X", 1)])];
let mut modules = [
source("A", r"M\A", &a),
source("B", r"M\B", &b),
source("C", r"M\C", &c),
];
modules[2].on = false;
let (links, cycles) = build(&modules, &Rules::default());
assert_eq!(cycles.len(), 1);
assert_eq!(cycles[0].modules, ["A", "B"]);
let circular: Vec<_> = links
.iter()
.map(|link| (link.from.as_str(), link.to.as_str(), link.circular))
.collect();
assert_eq!(
circular,
[
("A", "B", true),
("B", "A", true),
("B", "C", false),
("C", "B", false)
]
);
}
#[test]
fn a_module_sits_left_of_what_it_uses_and_a_circle_shares_a_column() {
let link = |from: &str, to: &str| Link {
from: from.into(),
to: to.into(),
refs: Vec::new(),
circular: false,
};
let links = [
link("Notifications", "Checkout"),
link("Checkout", "Shipping"),
link("Shipping", "Checkout"),
link("Checkout", "Billing"),
link("Billing", "Users"),
link("Inventory", "Catalog"),
link("Checkout", "Catalog"),
];
let names = [
"Billing",
"Catalog",
"Checkout",
"Inventory",
"Notifications",
"Shipping",
"Users",
"Alone",
];
assert_eq!(
columns(&names, &links),
[
vec!["Inventory", "Notifications", "Alone"],
vec!["Checkout", "Shipping"],
vec!["Billing", "Catalog"],
vec!["Users"],
]
);
}
}