| Total Complexity | 42 |
| Total Lines | 286 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Route often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Route, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | final class Route implements RouteInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array<string, mixed> |
||
| 11 | */ |
||
| 12 | private $annotations; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $name = ''; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var bool |
||
| 21 | */ |
||
| 22 | private $routable = false; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array<string> |
||
| 26 | */ |
||
| 27 | private $httpMethods = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $path = ''; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array<string, mixed> |
||
| 36 | */ |
||
| 37 | private $attributes = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $serviceId = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $serviceMethod = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array<string> |
||
| 51 | */ |
||
| 52 | private $middlewareServiceIds = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param array<object> $annotations |
||
| 56 | */ |
||
| 57 | public function __construct(string $serviceId, string $serviceMethod, array $annotations) |
||
| 58 | { |
||
| 59 | if ($serviceId) { |
||
| 60 | $this->name = $serviceId; |
||
| 61 | $this->serviceId = $serviceId; |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($serviceMethod) { |
||
| 65 | $this->name .= ":$serviceMethod"; |
||
| 66 | $this->serviceMethod = $serviceMethod; |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->annotations = $annotations; |
||
| 70 | } |
||
| 71 | |||
| 72 | public function __sleep(): array |
||
| 73 | { |
||
| 74 | return [ |
||
| 75 | 'name', |
||
| 76 | 'routable', |
||
| 77 | 'httpMethods', |
||
| 78 | 'path', |
||
| 79 | 'attributes', |
||
| 80 | 'serviceId', |
||
| 81 | 'serviceMethod', |
||
| 82 | 'middlewareServiceIds', |
||
| 83 | ]; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function getName(): string |
||
| 87 | { |
||
| 88 | return $this->name; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function withName(string $name): RouteInterface |
||
| 92 | { |
||
| 93 | $new = clone $this; |
||
| 94 | $new->setName($name); |
||
| 95 | return $new; |
||
| 96 | } |
||
| 97 | |||
| 98 | private function setName(string $name): void |
||
| 99 | { |
||
| 100 | $this->name = $name; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function isRoutable(): bool |
||
| 104 | { |
||
| 105 | return $this->routable; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function withRoutable(bool $routable): RouteInterface |
||
| 113 | } |
||
| 114 | |||
| 115 | private function setRoutable(bool $routable): void |
||
| 116 | { |
||
| 117 | $this->routable = $routable; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function getHttpMethods(): array |
||
| 121 | { |
||
| 122 | return array_values($this->httpMethods); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function withHttpMethod(string $httpMethod): RouteInterface |
||
| 130 | } |
||
| 131 | |||
| 132 | public function withoutHttpMethod(string $httpMethod): RouteInterface |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param array<string> $httpMethods |
||
| 149 | */ |
||
| 150 | private function setHttpMethods(array $httpMethods): void |
||
| 151 | { |
||
| 152 | $this->httpMethods = array_change_key_case($httpMethods, CASE_UPPER); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getPath(): string |
||
| 156 | { |
||
| 157 | return $this->path; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function withPath(string $path): RouteInterface |
||
| 161 | { |
||
| 162 | $new = clone $this; |
||
| 163 | $new->setPath($path); |
||
| 164 | return $new; |
||
| 165 | } |
||
| 166 | |||
| 167 | private function setPath(string $path): void |
||
| 168 | { |
||
| 169 | $this->path = $path; |
||
| 170 | } |
||
| 171 | |||
| 172 | public function hasAttribute(string $name): bool |
||
| 175 | } |
||
| 176 | |||
| 177 | public function getAttribute(string $name) |
||
| 178 | { |
||
| 179 | return $this->attributes[$name] ?? null; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function getAttributes(): array |
||
| 183 | { |
||
| 184 | return $this->attributes; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function withAttribute(string $name, $value): RouteInterface |
||
| 188 | { |
||
| 189 | $new = clone $this; |
||
| 190 | $new->setAttribute($name, $value); |
||
| 191 | return $new; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param mixed $value |
||
| 196 | */ |
||
| 197 | private function setAttribute(string $name, $value): void |
||
| 198 | { |
||
| 199 | $this->attributes[$name] = $value; |
||
| 200 | } |
||
| 201 | |||
| 202 | public function getServiceId(): string |
||
| 203 | { |
||
| 204 | return $this->serviceId; |
||
| 205 | } |
||
| 206 | |||
| 207 | public function withServiceId(string $serviceId): RouteInterface |
||
| 208 | { |
||
| 209 | $new = clone $this; |
||
| 210 | $new->setServiceId($serviceId); |
||
| 211 | return $new; |
||
| 212 | } |
||
| 213 | |||
| 214 | private function setServiceId(string $serviceId): void |
||
| 215 | { |
||
| 216 | $this->serviceId = $serviceId; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function getServiceMethod(): string |
||
| 220 | { |
||
| 221 | return $this->serviceMethod; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function withServiceMethod(string $serviceMethod): RouteInterface |
||
| 225 | { |
||
| 226 | $new = clone $this; |
||
| 227 | $new->setServiceMethod($serviceMethod); |
||
| 228 | return $new; |
||
| 229 | } |
||
| 230 | |||
| 231 | private function setServiceMethod(string $serviceMethod): void |
||
| 232 | { |
||
| 233 | $this->serviceMethod = $serviceMethod; |
||
| 234 | } |
||
| 235 | |||
| 236 | public function getMiddlewareServiceIds(): array |
||
| 237 | { |
||
| 238 | return $this->middlewareServiceIds; |
||
| 239 | } |
||
| 240 | |||
| 241 | public function withMiddleware(string $serviceId): RouteInterface |
||
| 242 | { |
||
| 243 | $new = clone $this; |
||
| 244 | $new->setMiddlewareServiceIds(array_merge($this->middlewareServiceIds, [$serviceId])); |
||
| 245 | return $new; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param array<string> $serviceIds |
||
| 250 | */ |
||
| 251 | private function setMiddlewareServiceIds(array $serviceIds): void |
||
| 252 | { |
||
| 253 | $this->middlewareServiceIds = $serviceIds; |
||
| 254 | } |
||
| 255 | |||
| 256 | public function hasAnnotation(string $annotationId): bool |
||
| 257 | { |
||
| 258 | foreach ($this->annotations as $annotation) { |
||
| 259 | if ($annotation instanceof $annotationId) { |
||
| 260 | return true; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | return false; |
||
| 265 | } |
||
| 266 | |||
| 267 | public function getAnnotation(string $annotationId): ?object |
||
| 276 | } |
||
| 277 | |||
| 278 | public function getAnnotations(string $annotationId = ''): array |
||
| 279 | { |
||
| 280 | if (!$annotationId) { |
||
| 281 | return $this->annotations; |
||
| 282 | } |
||
| 283 | |||
| 284 | $annotations = []; |
||
| 285 | |||
| 286 | foreach ($this->annotations as $annotation) { |
||
| 287 | if ($annotation instanceof $annotationId) { |
||
| 293 | } |
||
| 294 | } |
||
| 295 |