| Conditions | 6 |
| Paths | 2 |
| Total Lines | 52 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 30 |
| CRAP Score | 6 |
| 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 | <?php |
||
| 27 | 2 | public function discover(string $type = null): array |
|
| 28 | { |
||
| 29 | 2 | $files = $this->filesystem->listContents('vendor', true); |
|
| 30 | |||
| 31 | // Find all composer.json files |
||
| 32 | // Then find suitable assets |
||
| 33 | 2 | $assets = []; |
|
| 34 | |||
| 35 | 2 | collect($files) |
|
| 36 | ->filter(function (array $file) { |
||
| 37 | 2 | return $file['type'] === 'file' && $file['basename'] === 'composer.json'; |
|
| 38 | 2 | }) |
|
| 39 | ->transform(function ($file) { |
||
| 40 | 2 | $contents = $this->filesystem->get($file['path'])->read(); |
|
| 41 | 2 | $manifest = json_decode($contents, true); |
|
| 42 | |||
| 43 | 2 | if (isset($manifest['extra'], $manifest['extra']['fondbot'])) { |
|
| 44 | 2 | return $manifest['extra']['fondbot']; |
|
| 45 | } |
||
| 46 | |||
| 47 | 2 | return null; |
|
| 48 | 2 | }) |
|
| 49 | ->filter(function ($item) use ($type) { |
||
| 50 | 2 | if ($item === null) { |
|
| 51 | 2 | return false; |
|
| 52 | } |
||
| 53 | |||
| 54 | // Filter by type |
||
| 55 | 2 | if ($type !== null) { |
|
| 56 | 1 | return key($item) === $type; |
|
| 57 | } |
||
| 58 | |||
| 59 | 1 | return true; |
|
| 60 | 2 | }) |
|
| 61 | ->each(function ($item) use (&$assets) { |
||
| 62 | 2 | $type = key($item); |
|
| 63 | |||
| 64 | 2 | $assets[$type][] = $item[$type]; |
|
| 65 | 2 | }); |
|
| 66 | |||
| 67 | 2 | if ($type !== null) { |
|
| 68 | 1 | $assets = collect($assets) |
|
| 69 | 1 | ->filter(function ($_, $key) use ($type) { |
|
| 70 | 1 | return $key === $type; |
|
| 71 | 1 | }) |
|
| 72 | 1 | ->values() |
|
| 73 | 1 | ->flatten() |
|
| 74 | 1 | ->toArray(); |
|
| 75 | } |
||
| 76 | |||
| 77 | 2 | return $assets; |
|
| 78 | } |
||
| 79 | } |
||
| 80 |