| Conditions | 18 |
| Paths | 800 |
| Total Lines | 75 |
| Lines | 24 |
| Ratio | 32 % |
| 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 | $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 | |||
| 93 |