Conditions | 16 |
Paths | 450 |
Total Lines | 63 |
Lines | 12 |
Ratio | 19.05 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | #!/usr/bin/env php |
||
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 | |||
81 |