| Conditions | 13 |
| Paths | 8 |
| Total Lines | 58 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 65 | public function addRoute(string $prefix, $route): void |
||
| 66 | { |
||
| 67 | [$prefix, $staticPrefix] = $this->getCommonPrefix($prefix, $prefix); |
||
| 68 | |||
| 69 | for ($i = \count($this->items) - 1; 0 <= $i; --$i) { |
||
| 70 | $item = $this->items[$i]; |
||
| 71 | |||
| 72 | [$commonPrefix, $commonStaticPrefix] = $this->getCommonPrefix($prefix, $this->prefixes[$i]); |
||
| 73 | |||
| 74 | if ($this->prefix === $commonPrefix) { |
||
| 75 | // the new route and a previous one have no common prefix, let's see if they are exclusive to each others |
||
| 76 | |||
| 77 | if ($this->prefix !== $staticPrefix && $this->prefix !== $this->staticPrefixes[$i]) { |
||
| 78 | // the new route and the previous one have exclusive static prefixes |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($this->prefix === $staticPrefix && $this->prefix === $this->staticPrefixes[$i]) { |
||
| 83 | // the new route and the previous one have no static prefix |
||
| 84 | break; |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($this->prefixes[$i] !== $this->staticPrefixes[$i] && $this->prefix === $this->staticPrefixes[$i]) { |
||
| 88 | // the previous route is non-static and has no static prefix |
||
| 89 | break; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($prefix !== $staticPrefix && $this->prefix === $staticPrefix) { |
||
| 93 | // the new route is non-static and has no static prefix |
||
| 94 | break; |
||
| 95 | } |
||
| 96 | |||
| 97 | continue; |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($item instanceof self && $this->prefixes[$i] === $commonPrefix) { |
||
| 101 | // the new route is a child of a previous one, let's nest it |
||
| 102 | $item->addRoute($prefix, $route); |
||
| 103 | } else { |
||
| 104 | // the new route and a previous one have a common prefix, let's merge them |
||
| 105 | $child = new self($commonPrefix); |
||
| 106 | [$child->prefixes[0], $child->staticPrefixes[0]] = $child->getCommonPrefix($this->prefixes[$i], $this->prefixes[$i]); |
||
| 107 | [$child->prefixes[1], $child->staticPrefixes[1]] = $child->getCommonPrefix($prefix, $prefix); |
||
| 108 | $child->items = [$this->items[$i], $route]; |
||
| 109 | |||
| 110 | $this->staticPrefixes[$i] = $commonStaticPrefix; |
||
| 111 | $this->prefixes[$i] = $commonPrefix; |
||
| 112 | $this->items[$i] = $child; |
||
| 113 | } |
||
| 114 | |||
| 115 | return; |
||
| 116 | } |
||
| 117 | |||
| 118 | // No optimised case was found, in this case we simple add the route for possible |
||
| 119 | // grouping when new routes are added. |
||
| 120 | $this->staticPrefixes[] = $staticPrefix; |
||
| 121 | $this->prefixes[] = $prefix; |
||
| 122 | $this->items[] = $route; |
||
| 123 | } |
||
| 200 |