Conditions | 6 |
Paths | 14 |
Total Lines | 56 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 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 |
||
34 | public function getByName($query) |
||
35 | { |
||
36 | /** @var \Composer\Repository\RepositoryInterface[] $repositories */ |
||
37 | $repositories = array( |
||
38 | new \Composer\Repository\ArrayRepository(array($this->rootPackage)), |
||
39 | $this->packageRepository |
||
40 | ); |
||
41 | |||
42 | $matchesGroups = array(); |
||
43 | |||
44 | foreach ($repositories as $repositoryId => $item) { |
||
45 | $matchesGroups[$repositoryId] = $item->search($query); |
||
46 | } |
||
47 | |||
48 | if (!array_filter($matchesGroups)) { |
||
49 | throw new PackageResolverException( |
||
50 | sprintf('No packages for query %s', $query) |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | $matches = array_reduce($matchesGroups, 'array_merge', array()); |
||
55 | |||
56 | $exactMatches = array_filter($matches, function (array $match) use ($query) { |
||
57 | return $match['name'] === $query; |
||
58 | }); |
||
59 | |||
60 | if (!empty($exactMatches)) { |
||
61 | $matches = $exactMatches; |
||
62 | } |
||
63 | |||
64 | if (count($matches) > 1) { |
||
65 | $exception = new PackageResolverException( |
||
66 | sprintf('Multiple packages found for query %s:', $query) |
||
67 | ); |
||
68 | |||
69 | $exception->setExtraInfo(array_map(function ($match) { |
||
70 | return $match['name']; |
||
71 | }, $matches)); |
||
72 | |||
73 | throw $exception; |
||
74 | } |
||
75 | |||
76 | $repositoryKey = key(array_filter($matchesGroups)); |
||
77 | |||
78 | $repository = $repositories[$repositoryKey]; |
||
79 | $firstMatch = reset($matches); |
||
80 | |||
81 | $package = $repository->findPackage($firstMatch['name'], '*'); |
||
82 | |||
83 | if (!$package) { |
||
84 | throw new PackageResolverException( |
||
85 | sprintf('No packages for query %s', $query) |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | return $package; |
||
90 | } |
||
92 |