Completed
Push — update/build-sort-by-deps ( e2d820...b39fe2 )
by
unknown
58:05 queued 47:05
created

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

Complexity

Conditions 16
Paths 450

Size

Total Lines 63

Duplication

Lines 12
Ratio 19.05 %

Importance

Changes 0
Metric Value
cc 16
nc 450
nop 0
dl 12
loc 63
rs 2.1638
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
		$json = json_decode( file_get_contents( $file ), true );
32
		if ( isset( $json['name'] ) ) {
33
			$package_map[ $json['name'] ] = substr( $file, $l + 10, -14 );
34
		}
35
	}
36
37
	// Collect js-package name→slug mappings.
38
	$js_package_map = array();
39 View Code Duplication
	foreach ( glob( "$base/projects/js-packages/*/package.json" ) as $file ) {
40
		$json = json_decode( file_get_contents( $file ), true );
41
		if ( isset( $json['name'] ) ) {
42
			$js_package_map[ $json['name'] ] = substr( $file, $l + 10, -14 );
43
		}
44
	}
45
46
	// Collect dependencies.
47
	foreach ( $output as $slug => &$deps ) {
48
		$path = 'monorepo' === $slug ? $base : "$base/projects/$slug";
49
50
		// Collect composer require, require-dev, and .extra.dependencies.
51
		$json = json_decode( file_get_contents( "$path/composer.json" ), true );
52
		foreach ( $package_map as $package => $p ) {
53
			if ( isset( $json['require'][ $package ] ) || isset( $json['require-dev'][ $package ] ) ) {
54
				$deps[] = $p;
55
			}
56
		}
57
		if ( isset( $json['extra']['dependencies'] ) ) {
58
			$deps = array_merge( $deps, $json['extra']['dependencies'] );
59
		}
60
61
		// Collect yarn dependencies and devDependencies.
62
		if ( file_exists( "$path/package.json" ) ) {
63
			$json = json_decode( file_get_contents( "$path/package.json" ), true );
64
			foreach ( $js_package_map as $package => $p ) {
65
				if ( isset( $json['dependencies'][ $package ] ) || isset( $json['devDependencies'][ $package ] ) ) {
66
					$deps[] = $p;
67
				}
68
			}
69
		}
70
71
		// Finalize.
72
		$deps = array_unique( $deps );
73
		sort( $deps );
74
	}
75
76
	ksort( $output );
77
	return $output;
78
}
79
80
echo json_encode( get_dependencies(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . "\n";
81