| Conditions | 6 |
| Paths | 14 |
| Total Lines | 56 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 41 | public function getByName($query) |
||
| 42 | { |
||
| 43 | /** @var \Composer\Repository\RepositoryInterface[] $repositories */ |
||
| 44 | $repositories = array( |
||
| 45 | new \Composer\Repository\ArrayRepository(array($this->rootPackage)), |
||
| 46 | $this->packageRepository |
||
| 47 | ); |
||
| 48 | |||
| 49 | $matchesGroups = array(); |
||
| 50 | |||
| 51 | foreach ($repositories as $repositoryId => $item) { |
||
| 52 | $matchesGroups[$repositoryId] = $item->search($query); |
||
| 53 | } |
||
| 54 | |||
| 55 | if (!array_filter($matchesGroups)) { |
||
| 56 | throw new PackageResolverException( |
||
| 57 | sprintf('No packages for query %s', $query) |
||
| 58 | ); |
||
| 59 | } |
||
| 60 | |||
| 61 | $matches = array_reduce($matchesGroups, 'array_merge', array()); |
||
| 62 | |||
| 63 | $exactMatches = array_filter($matches, function (array $match) use ($query) { |
||
| 64 | return $match['name'] === $query; |
||
| 65 | }); |
||
| 66 | |||
| 67 | if ($exactMatches) { |
||
|
|
|||
| 68 | $matches = $exactMatches; |
||
| 69 | } |
||
| 70 | |||
| 71 | if (count($matches) > 1) { |
||
| 72 | $exception = new PackageResolverException( |
||
| 73 | sprintf('Multiple packages found for query %s:', $query) |
||
| 74 | ); |
||
| 75 | |||
| 76 | $exception->setExtraInfo(array_map(function ($match) { |
||
| 77 | return $match['name']; |
||
| 78 | }, $matches)); |
||
| 79 | |||
| 80 | throw $exception; |
||
| 81 | } |
||
| 82 | |||
| 83 | $repositoryKey = key(array_filter($matchesGroups)); |
||
| 84 | |||
| 85 | $repository = $repositories[$repositoryKey]; |
||
| 86 | $firstMatch = reset($matches); |
||
| 87 | |||
| 88 | $package = $repository->findPackage($firstMatch['name'], '*'); |
||
| 89 | |||
| 90 | if (!$package) { |
||
| 91 | throw new PackageResolverException( |
||
| 92 | sprintf('No packages for query %s', $query) |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $package; |
||
| 97 | } |
||
| 99 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.