Skip to content

Commit 208c1c8

Browse files
refactor(plugins): reduce nesting in collect_autoload_paths
Extract collect_psr_paths and collect_string_array_paths helpers to address qlty nested-control-flow and function-complexity findings.
1 parent 03d6eef commit 208c1c8

File tree

2 files changed

+73
-47
lines changed

2 files changed

+73
-47
lines changed

qlty-check/src/tool/php/composer.rs

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,7 @@ impl Composer {
264264

265265
let link = sandbox_dir.join(relative_path);
266266
if link.symlink_metadata().is_ok() {
267-
debug!(
268-
"Skipping autoload symlink, path already exists: {:?}",
269-
link
270-
);
267+
debug!("Skipping autoload symlink, path already exists: {:?}", link);
271268
continue;
272269
}
273270

@@ -291,54 +288,63 @@ impl Composer {
291288
let mut paths = vec![];
292289

293290
for section in &["autoload", "autoload-dev"] {
294-
if let Some(autoload) = composer_json.get(section) {
295-
for key in &["psr-4", "psr-0"] {
296-
if let Some(mappings) = autoload.get(key).and_then(|v| v.as_object()) {
297-
for (_, path_value) in mappings {
298-
match path_value {
299-
Value::String(s) if !s.is_empty() => {
300-
paths.push(s.trim_end_matches('/').to_string());
301-
}
302-
Value::Array(arr) => {
303-
for v in arr {
304-
if let Some(s) = v.as_str() {
305-
if !s.is_empty() {
306-
paths.push(
307-
s.trim_end_matches('/').to_string(),
308-
);
309-
}
310-
}
311-
}
312-
}
313-
_ => {}
314-
}
315-
}
316-
}
317-
}
291+
let Some(autoload) = composer_json.get(section) else {
292+
continue;
293+
};
318294

319-
if let Some(classmap) = autoload.get("classmap").and_then(|v| v.as_array()) {
320-
for v in classmap {
321-
if let Some(s) = v.as_str() {
322-
if !s.is_empty() {
323-
paths.push(s.trim_end_matches('/').to_string());
324-
}
325-
}
295+
for key in &["psr-4", "psr-0"] {
296+
if let Some(mappings) = autoload.get(key).and_then(|v| v.as_object()) {
297+
for (_, path_value) in mappings {
298+
Self::collect_psr_paths(path_value, &mut paths);
326299
}
327300
}
301+
}
328302

329-
if let Some(files) = autoload.get("files").and_then(|v| v.as_array()) {
330-
for v in files {
331-
if let Some(s) = v.as_str() {
332-
if !s.is_empty() {
333-
paths.push(s.to_string());
334-
}
335-
}
336-
}
303+
Self::collect_string_array_paths(autoload.get("classmap"), true, &mut paths);
304+
Self::collect_string_array_paths(autoload.get("files"), false, &mut paths);
305+
}
306+
307+
paths
308+
}
309+
310+
fn collect_psr_paths(path_value: &Value, paths: &mut Vec<String>) {
311+
match path_value {
312+
Value::String(s) if !s.is_empty() => {
313+
paths.push(s.trim_end_matches('/').to_string());
314+
}
315+
Value::Array(arr) => {
316+
for s in arr
317+
.iter()
318+
.filter_map(|v| v.as_str())
319+
.filter(|s| !s.is_empty())
320+
{
321+
paths.push(s.trim_end_matches('/').to_string());
337322
}
338323
}
324+
_ => {}
339325
}
326+
}
340327

341-
paths
328+
fn collect_string_array_paths(
329+
value: Option<&Value>,
330+
trim_trailing_slash: bool,
331+
paths: &mut Vec<String>,
332+
) {
333+
let Some(arr) = value.and_then(|v| v.as_array()) else {
334+
return;
335+
};
336+
337+
for s in arr
338+
.iter()
339+
.filter_map(|v| v.as_str())
340+
.filter(|s| !s.is_empty())
341+
{
342+
if trim_trailing_slash {
343+
paths.push(s.trim_end_matches('/').to_string());
344+
} else {
345+
paths.push(s.to_string());
346+
}
347+
}
342348
}
343349
}
344350

@@ -802,8 +808,18 @@ pub mod test {
802808
Composer::create_autoload_symlinks(pkg)?;
803809

804810
let sandbox_dir = PathBuf::from(pkg.directory());
805-
assert!(sandbox_dir.join("app").symlink_metadata().unwrap().file_type().is_symlink());
806-
assert!(sandbox_dir.join("tests").symlink_metadata().unwrap().file_type().is_symlink());
811+
assert!(sandbox_dir
812+
.join("app")
813+
.symlink_metadata()
814+
.unwrap()
815+
.file_type()
816+
.is_symlink());
817+
assert!(sandbox_dir
818+
.join("tests")
819+
.symlink_metadata()
820+
.unwrap()
821+
.file_type()
822+
.is_symlink());
807823
assert_eq!(
808824
std::fs::read_link(sandbox_dir.join("app"))?,
809825
project_dir.join("app")
@@ -878,7 +894,12 @@ pub mod test {
878894
Composer::create_autoload_symlinks(pkg)?;
879895

880896
let sandbox_dir = PathBuf::from(pkg.directory());
881-
assert!(sandbox_dir.join("app").symlink_metadata().unwrap().file_type().is_symlink());
897+
assert!(sandbox_dir
898+
.join("app")
899+
.symlink_metadata()
900+
.unwrap()
901+
.file_type()
902+
.is_symlink());
882903
assert!(!sandbox_dir.join("does-not-exist").exists());
883904

884905
Ok(())

qlty-config/src/config/builder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ fn merge_enabled_plugins(existing: &EnabledPlugin, new: &EnabledPlugin) -> Enabl
466466
triggers: prioritize_new_array(&existing.triggers, &new.triggers),
467467
fetch: prioritize_new_array(&existing.fetch, &new.fetch),
468468
package_filters: prioritize_new_array(&existing.package_filters, &new.package_filters),
469+
preserve_autoload: new.preserve_autoload || existing.preserve_autoload,
469470
affects_cache: prioritize_new_array(&existing.affects_cache, &new.affects_cache),
470471
extra_packages: prioritize_new_array(&existing.extra_packages, &new.extra_packages),
471472
drivers: prioritize_new_array(&existing.drivers, &new.drivers),
@@ -788,6 +789,7 @@ mod test {
788789
path: "path1".to_string(),
789790
}],
790791
package_filters: vec!["filter1".to_string()],
792+
preserve_autoload: false,
791793
affects_cache: vec!["cache1".to_string()],
792794
extra_packages: vec![ExtraPackage {
793795
name: "pkg1".to_string(),
@@ -810,6 +812,7 @@ mod test {
810812
path: "path2".to_string(),
811813
}],
812814
package_filters: vec!["filter2".to_string()],
815+
preserve_autoload: false,
813816
affects_cache: vec!["cache2".to_string()],
814817
extra_packages: vec![ExtraPackage {
815818
name: "pkg2".to_string(),
@@ -1060,6 +1063,7 @@ mod test {
10601063
path: "path1".to_string(),
10611064
}],
10621065
package_filters: vec!["filter1".to_string()],
1066+
preserve_autoload: false,
10631067
affects_cache: vec!["cache1".to_string()],
10641068
extra_packages: vec![ExtraPackage {
10651069
name: "pkg1".to_string(),
@@ -1081,6 +1085,7 @@ mod test {
10811085
path: "path2".to_string(),
10821086
}],
10831087
package_filters: vec!["filter2".to_string()],
1088+
preserve_autoload: false,
10841089
affects_cache: vec!["cache2".to_string()],
10851090
extra_packages: vec![ExtraPackage {
10861091
name: "pkg2".to_string(),

0 commit comments

Comments
 (0)