| Conditions | 10 |
| Paths | 21 |
| Total Lines | 54 |
| Code Lines | 32 |
| 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 |
||
| 104 | public function generate(bool $isStatic = false): array |
||
| 105 | { |
||
| 106 | $scheme = $this->scheme; |
||
| 107 | $hosts = ['*' => '*']; |
||
| 108 | $constraints = $this->constraints; |
||
| 109 | |||
| 110 | if (!$isStatic && !$this->template) { |
||
| 111 | throw new \RuntimeException('Missing path template.'); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (null !== $this->host) { |
||
| 115 | $pattern = $this->host; |
||
| 116 | |||
| 117 | for ($i = 1; $i <= $this->nbHosts; $i++) { |
||
| 118 | $host = Text::insert($pattern, $constraints, ['before' => '{%', 'after' => '%}']); |
||
| 119 | |||
| 120 | $hosts["subdomain{$i}.domain."] = \sprintf('subdomain%s.domain.%s', $i, $host); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | $ids = []; |
||
| 125 | $id = 1; |
||
| 126 | |||
| 127 | foreach ($hosts as $domain => $host) { |
||
| 128 | $this->hosts[] = $domain; |
||
| 129 | $ids[$domain] = [ |
||
| 130 | RequestMethodInterface::METHOD_GET => [], |
||
| 131 | RequestMethodInterface::METHOD_POST => [], |
||
| 132 | RequestMethodInterface::METHOD_PUT => [], |
||
| 133 | RequestMethodInterface::METHOD_PATCH => [], |
||
| 134 | RequestMethodInterface::METHOD_DELETE => [], |
||
| 135 | ]; |
||
| 136 | |||
| 137 | for ($i = 0; $i < $this->nbRoutes; $i++) { |
||
| 138 | $path = \sprintf('/controller%s/action%1$s/', $id); |
||
| 139 | $pattern = $path . Text::insert($this->template, $constraints, ['before' => '{%', 'after' => '%}']); |
||
| 140 | $methods = $this->isolated ? [$this->methods[$i % 5]] : $this->methods; |
||
| 141 | $name = '_' . $id; |
||
| 142 | |||
| 143 | if ($isStatic) { |
||
| 144 | $pattern = $this->template ?? $path; |
||
| 145 | } |
||
| 146 | |||
| 147 | $routes[] = \compact('name', 'scheme', 'host', 'pattern', 'methods', 'constraints'); |
||
| 148 | |||
| 149 | foreach ($methods as $method) { |
||
| 150 | $ids[$domain][$method][] = $path; |
||
| 151 | } |
||
| 152 | |||
| 153 | $id++; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | return [$ids, $routes]; |
||
|
|
|||
| 158 | } |
||
| 160 |