Conditions | 10 |
Paths | 22 |
Total Lines | 35 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 2 | Features | 1 |
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 |
||
32 | protected function getFinalTreeElement(array $slugs, array $elements) |
||
33 | { |
||
34 | // Will check that slugs and elements match |
||
35 | $slugsElements = array_keys($elements); |
||
36 | $sortedSlugs = $slugs; |
||
37 | sort($sortedSlugs); |
||
38 | sort($slugsElements); |
||
39 | |||
40 | if ($sortedSlugs !== $slugsElements || !count($slugs) || count($slugs) !== count($elements)) { |
||
41 | throw $this->createNotFoundException(); |
||
42 | } |
||
43 | |||
44 | /** @var Page|Category $element */ |
||
45 | $element = null; |
||
46 | /** @var Page|Category $previousElement */ |
||
47 | $previousElement = null; |
||
48 | |||
49 | foreach ($slugs as $slug) { |
||
50 | $element = isset($elements[$slug]) ? $elements[$slug] : null; |
||
51 | $match = false; |
||
52 | if ($element) { |
||
53 | // Only for the first iteration |
||
54 | $match = $previousElement |
||
55 | ? $element->getParent() && $previousElement->getSlug() === $element->getParent()->getSlug() |
||
56 | : true; |
||
57 | |||
58 | $previousElement = $element; |
||
59 | } |
||
60 | if (!$match) { |
||
61 | throw $this->createNotFoundException((new \ReflectionClass($element))->getShortName().' hierarchy not found.'); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | return $element; |
||
66 | } |
||
67 | } |
||
68 |