Completed
Push — update/wpcom-block-editor-shor... ( 9897fe...f43ed6 )
by Jeremy
148:26 queued 138:20
created

find-project-deps.php ➔ get_dependencies()   F

Complexity

Conditions 18
Paths 800

Size

Total Lines 75

Duplication

Lines 24
Ratio 32 %

Importance

Changes 0
Metric Value
cc 18
nc 800
nop 0
dl 24
loc 75
rs 0.9777
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
#!/usr/bin/env php
2
<?php
3
/**
4
 * Function to collect project direct dependencies.
5
 *
6
 * @package automattic/jetpack
7
 */
8
9
// phpcs:disable WordPress.WP.AlternativeFunctions
10
11
/**
12
 * Collect project dependencies.
13
 *
14
 * @return string[][] Key is the project slug, value is an array of slugs depended on.
15
 */
16
function get_dependencies() {
17
	$base = dirname( __DIR__ );
18
	$l    = strlen( $base );
19
20
	// Collect all project slugs.
21
	$output = array(
22
		'monorepo' => array(),
23
	);
24
	foreach ( glob( "$base/projects/*/*/composer.json" ) as $file ) {
25
		$output[ substr( $file, $l + 10, -14 ) ] = array();
26
	}
27
28
	// Collect package name→slug mappings.
29
	$package_map = array();
30 View Code Duplication
	foreach ( glob( "$base/projects/packages/*/composer.json" ) as $file ) {
31
		$slug = substr( $file, $l + 10, -14 );
32
		if ( ! isset( $output[ $slug ] ) ) {
33
			// Not an actual project (should never happen here, but...).
34
			continue;
35
		}
36
37
		$json = json_decode( file_get_contents( $file ), true );
38
		if ( isset( $json['name'] ) ) {
39
			$package_map[ $json['name'] ] = $slug;
40
		}
41
	}
42
43
	// Collect js-package name→slug mappings.
44
	$js_package_map = array();
45 View Code Duplication
	foreach ( glob( "$base/projects/js-packages/*/package.json" ) as $file ) {
46
		$slug = substr( $file, $l + 10, -13 );
47
		if ( ! isset( $output[ $slug ] ) ) {
48
			// Not an actual project.
49
			continue;
50
		}
51
52
		$json = json_decode( file_get_contents( $file ), true );
53
		if ( isset( $json['name'] ) ) {
54
			$js_package_map[ $json['name'] ] = $slug;
55
		}
56
	}
57
58
	// Collect dependencies.
59
	foreach ( $output as $slug => &$deps ) {
60
		$path = 'monorepo' === $slug ? $base : "$base/projects/$slug";
61
62
		// Collect composer require, require-dev, and .extra.dependencies.
63
		$json = json_decode( file_get_contents( "$path/composer.json" ), true );
64
		foreach ( $package_map as $package => $pkgslug ) {
65
			if ( isset( $json['require'][ $package ] ) || isset( $json['require-dev'][ $package ] ) ) {
66
				$deps[] = $pkgslug;
67
			}
68
		}
69
		if ( isset( $json['extra']['dependencies'] ) ) {
70
			$deps = array_merge( $deps, $json['extra']['dependencies'] );
71
		}
72
73
		// Collect yarn dependencies and devDependencies.
74
		if ( file_exists( "$path/package.json" ) ) {
75
			$json = json_decode( file_get_contents( "$path/package.json" ), true );
76
			foreach ( $js_package_map as $package => $pkgslug ) {
77
				if ( isset( $json['dependencies'][ $package ] ) || isset( $json['devDependencies'][ $package ] ) ) {
78
					$deps[] = $pkgslug;
79
				}
80
			}
81
		}
82
83
		// Finalize.
84
		$deps = array_unique( $deps );
85
		sort( $deps );
86
	}
87
88
	ksort( $output );
89
	return $output;
90
}
91
92
echo json_encode( get_dependencies(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . "\n";
93