| Total Complexity | 46 |
| Total Lines | 557 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 31 | class Route implements RouteContract |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @param non-empty-string $path The path |
||
| 35 | * @param non-empty-string $name The name |
||
| 36 | * @param non-empty-string|null $regex The regex |
||
| 37 | * @param RequestMethod[] $requestMethods The request methods |
||
| 38 | * @param ParameterContract[] $parameters The parameters |
||
| 39 | * @param class-string<RouteMatchedMiddlewareContract>[] $routeMatchedMiddleware The route matched middleware |
||
| 40 | * @param class-string<RouteDispatchedMiddlewareContract>[] $routeDispatchedMiddleware The route dispatched middleware |
||
| 41 | * @param class-string<ThrowableCaughtMiddlewareContract>[] $throwableCaughtMiddleware The throwable caught middleware |
||
| 42 | * @param class-string<SendingResponseMiddlewareContract>[] $sendingResponseMiddleware The sending response middleware |
||
| 43 | * @param class-string<TerminatedMiddlewareContract>[] $terminatedMiddleware The terminated middleware |
||
| 44 | * @param class-string<RequestStructContract>|null $requestStruct The request struct |
||
| 45 | * @param class-string<ResponseStructContract>|null $responseStruct The response struct |
||
| 46 | */ |
||
| 47 | public function __construct( |
||
| 48 | protected string $path, |
||
| 49 | protected string $name, |
||
| 50 | protected MethodDispatchContract $dispatch, |
||
| 51 | protected array $requestMethods = [RequestMethod::HEAD, RequestMethod::GET], |
||
| 52 | protected string|null $regex = null, |
||
| 53 | protected array $parameters = [], |
||
| 54 | protected array $routeMatchedMiddleware = [], |
||
| 55 | protected array $routeDispatchedMiddleware = [], |
||
| 56 | protected array $throwableCaughtMiddleware = [], |
||
| 57 | protected array $sendingResponseMiddleware = [], |
||
| 58 | protected array $terminatedMiddleware = [], |
||
| 59 | protected string|null $requestStruct = null, |
||
| 60 | protected string|null $responseStruct = null, |
||
| 61 | ) { |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @inheritDoc |
||
| 66 | */ |
||
| 67 | #[Override] |
||
| 68 | public function getPath(): string |
||
| 69 | { |
||
| 70 | return $this->path; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @inheritDoc |
||
| 75 | */ |
||
| 76 | #[Override] |
||
| 77 | public function withPath(string $path): static |
||
| 78 | { |
||
| 79 | $new = clone $this; |
||
| 80 | |||
| 81 | $new->path = $this->getFilteredPath($path); |
||
| 82 | |||
| 83 | return $new; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritDoc |
||
| 88 | */ |
||
| 89 | #[Override] |
||
| 90 | public function withAddedPath(string $path): static |
||
| 91 | { |
||
| 92 | $new = clone $this; |
||
| 93 | |||
| 94 | $filteredExistingPath = $this->getFilteredPath($this->path); |
||
| 95 | $filteredPath = $this->getFilteredPath($path); |
||
| 96 | |||
| 97 | $new->path = $this->getFilteredPath($filteredExistingPath . $filteredPath); |
||
| 98 | |||
| 99 | return $new; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @inheritDoc |
||
| 104 | */ |
||
| 105 | #[Override] |
||
| 106 | public function getName(): string |
||
| 107 | { |
||
| 108 | return $this->name; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @inheritDoc |
||
| 113 | */ |
||
| 114 | #[Override] |
||
| 115 | public function withName(string $name): static |
||
| 116 | { |
||
| 117 | $new = clone $this; |
||
| 118 | |||
| 119 | $new->name = $name; |
||
| 120 | |||
| 121 | return $new; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @inheritDoc |
||
| 126 | */ |
||
| 127 | #[Override] |
||
| 128 | public function withAddedName(string $name): static |
||
| 129 | { |
||
| 130 | $new = clone $this; |
||
| 131 | |||
| 132 | $new->name = $this->name . $name; |
||
| 133 | |||
| 134 | return $new; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @inheritDoc |
||
| 139 | */ |
||
| 140 | #[Override] |
||
| 141 | public function getDispatch(): MethodDispatchContract |
||
| 142 | { |
||
| 143 | return $this->dispatch; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @inheritDoc |
||
| 148 | */ |
||
| 149 | #[Override] |
||
| 150 | public function withDispatch(MethodDispatchContract $dispatch): static |
||
| 151 | { |
||
| 152 | $new = clone $this; |
||
| 153 | |||
| 154 | $new->dispatch = $dispatch; |
||
| 155 | |||
| 156 | return $new; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @inheritDoc |
||
| 161 | * |
||
| 162 | * @return RequestMethod[] |
||
| 163 | */ |
||
| 164 | #[Override] |
||
| 165 | public function getRequestMethods(): array |
||
| 166 | { |
||
| 167 | return $this->requestMethods; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @inheritDoc |
||
| 172 | */ |
||
| 173 | #[Override] |
||
| 174 | public function hasRequestMethod(RequestMethod $requestMethod): bool |
||
| 175 | { |
||
| 176 | return in_array($requestMethod, $this->requestMethods, true); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritDoc |
||
| 181 | */ |
||
| 182 | #[Override] |
||
| 183 | public function withRequestMethod(RequestMethod $requestMethod): static |
||
| 184 | { |
||
| 185 | $new = clone $this; |
||
| 186 | |||
| 187 | $new->requestMethods = [$requestMethod]; |
||
| 188 | |||
| 189 | return $new; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @inheritDoc |
||
| 194 | */ |
||
| 195 | #[Override] |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @inheritDoc |
||
| 207 | */ |
||
| 208 | #[Override] |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @inheritDoc |
||
| 222 | */ |
||
| 223 | #[Override] |
||
| 224 | public function withAddedRequestMethods(RequestMethod ...$requestMethods): static |
||
| 225 | { |
||
| 226 | $new = clone $this; |
||
| 227 | |||
| 228 | foreach ($requestMethods as $requestMethod) { |
||
| 229 | if (! in_array($requestMethod, $this->requestMethods, true)) { |
||
| 230 | $new->requestMethods[] = $requestMethod; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | return $new; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @inheritDoc |
||
| 239 | */ |
||
| 240 | #[Override] |
||
| 241 | public function getRegex(): string|null |
||
| 242 | { |
||
| 243 | return $this->regex; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @inheritDoc |
||
| 248 | */ |
||
| 249 | #[Override] |
||
| 250 | public function withRegex(string|null $regex = null): static |
||
| 251 | { |
||
| 252 | $new = clone $this; |
||
| 253 | |||
| 254 | $new->regex = $regex; |
||
| 255 | |||
| 256 | return $new; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @inheritDoc |
||
| 261 | * |
||
| 262 | * @return array<array-key, ParameterContract> |
||
| 263 | */ |
||
| 264 | #[Override] |
||
| 265 | public function getParameters(): array |
||
| 266 | { |
||
| 267 | return $this->parameters; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @inheritDoc |
||
| 272 | */ |
||
| 273 | #[Override] |
||
| 274 | public function withParameter(ParameterContract $parameter): static |
||
| 275 | { |
||
| 276 | $new = clone $this; |
||
| 277 | |||
| 278 | $new->parameters = [$parameter]; |
||
| 279 | |||
| 280 | return $new; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @inheritDoc |
||
| 285 | */ |
||
| 286 | #[Override] |
||
| 287 | public function withParameters(ParameterContract ...$parameters): static |
||
| 288 | { |
||
| 289 | $new = clone $this; |
||
| 290 | |||
| 291 | $new->parameters = $parameters; |
||
| 292 | |||
| 293 | return $new; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @inheritDoc |
||
| 298 | */ |
||
| 299 | #[Override] |
||
| 300 | public function withAddedParameter(ParameterContract $parameter): static |
||
| 301 | { |
||
| 302 | $new = clone $this; |
||
| 303 | |||
| 304 | $new->parameters[] = $parameter; |
||
| 305 | |||
| 306 | return $new; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @inheritDoc |
||
| 311 | */ |
||
| 312 | #[Override] |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @inheritDoc |
||
| 324 | * |
||
| 325 | * @return class-string<RouteMatchedMiddlewareContract>[] |
||
| 326 | */ |
||
| 327 | #[Override] |
||
| 328 | public function getRouteMatchedMiddleware(): array |
||
| 329 | { |
||
| 330 | return $this->routeMatchedMiddleware; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @inheritDoc |
||
| 335 | * |
||
| 336 | * @param class-string<RouteMatchedMiddlewareContract> ...$middleware The middleware |
||
| 337 | */ |
||
| 338 | #[Override] |
||
| 339 | public function withRouteMatchedMiddleware(string ...$middleware): static |
||
| 340 | { |
||
| 341 | $new = clone $this; |
||
| 342 | |||
| 343 | $new->routeMatchedMiddleware = $middleware; |
||
| 344 | |||
| 345 | return $new; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @inheritDoc |
||
| 350 | * |
||
| 351 | * @param class-string<RouteMatchedMiddlewareContract> ...$middleware The middleware |
||
| 352 | */ |
||
| 353 | #[Override] |
||
| 354 | public function withAddedRouteMatchedMiddleware(string ...$middleware): static |
||
| 355 | { |
||
| 356 | $new = clone $this; |
||
| 357 | |||
| 358 | $new->routeMatchedMiddleware = array_merge($this->routeMatchedMiddleware, $middleware); |
||
| 359 | |||
| 360 | return $new; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @inheritDoc |
||
| 365 | * |
||
| 366 | * @return class-string<RouteDispatchedMiddlewareContract>[] |
||
| 367 | */ |
||
| 368 | #[Override] |
||
| 369 | public function getRouteDispatchedMiddleware(): array |
||
| 370 | { |
||
| 371 | return $this->routeDispatchedMiddleware; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @inheritDoc |
||
| 376 | * |
||
| 377 | * @param class-string<RouteDispatchedMiddlewareContract> ...$middleware The middleware |
||
| 378 | */ |
||
| 379 | #[Override] |
||
| 380 | public function withRouteDispatchedMiddleware(string ...$middleware): static |
||
| 381 | { |
||
| 382 | $new = clone $this; |
||
| 383 | |||
| 384 | $new->routeDispatchedMiddleware = $middleware; |
||
| 385 | |||
| 386 | return $new; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @inheritDoc |
||
| 391 | * |
||
| 392 | * @param class-string<RouteDispatchedMiddlewareContract> ...$middleware The middleware |
||
| 393 | */ |
||
| 394 | #[Override] |
||
| 395 | public function withAddedRouteDispatchedMiddleware(string ...$middleware): static |
||
| 396 | { |
||
| 397 | $new = clone $this; |
||
| 398 | |||
| 399 | $new->routeDispatchedMiddleware = array_merge($this->routeDispatchedMiddleware, $middleware); |
||
| 400 | |||
| 401 | return $new; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @inheritDoc |
||
| 406 | * |
||
| 407 | * @return class-string<ThrowableCaughtMiddlewareContract>[] |
||
| 408 | */ |
||
| 409 | #[Override] |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @inheritDoc |
||
| 417 | * |
||
| 418 | * @param class-string<ThrowableCaughtMiddlewareContract> ...$middleware The middleware |
||
| 419 | */ |
||
| 420 | #[Override] |
||
| 421 | public function withThrowableCaughtMiddleware(string ...$middleware): static |
||
| 422 | { |
||
| 423 | $new = clone $this; |
||
| 424 | |||
| 425 | $new->throwableCaughtMiddleware = $middleware; |
||
| 426 | |||
| 427 | return $new; |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @inheritDoc |
||
| 432 | * |
||
| 433 | * @param class-string<ThrowableCaughtMiddlewareContract> ...$middleware The middleware |
||
| 434 | */ |
||
| 435 | #[Override] |
||
| 436 | public function withAddedThrowableCaughtMiddleware(string ...$middleware): static |
||
| 437 | { |
||
| 438 | $new = clone $this; |
||
| 439 | |||
| 440 | $new->throwableCaughtMiddleware = array_merge($this->throwableCaughtMiddleware, $middleware); |
||
| 441 | |||
| 442 | return $new; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @inheritDoc |
||
| 447 | * |
||
| 448 | * @return class-string<SendingResponseMiddlewareContract>[] |
||
| 449 | */ |
||
| 450 | #[Override] |
||
| 451 | public function getSendingResponseMiddleware(): array |
||
| 452 | { |
||
| 453 | return $this->sendingResponseMiddleware; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @inheritDoc |
||
| 458 | * |
||
| 459 | * @param class-string<SendingResponseMiddlewareContract> ...$middleware The middleware |
||
| 460 | */ |
||
| 461 | #[Override] |
||
| 462 | public function withSendingResponseMiddleware(string ...$middleware): static |
||
| 463 | { |
||
| 464 | $new = clone $this; |
||
| 465 | |||
| 466 | $new->sendingResponseMiddleware = $middleware; |
||
| 467 | |||
| 468 | return $new; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @inheritDoc |
||
| 473 | * |
||
| 474 | * @param class-string<SendingResponseMiddlewareContract> ...$middleware The middleware |
||
| 475 | */ |
||
| 476 | #[Override] |
||
| 477 | public function withAddedSendingResponseMiddleware(string ...$middleware): static |
||
| 478 | { |
||
| 479 | $new = clone $this; |
||
| 480 | |||
| 481 | $new->sendingResponseMiddleware = array_merge($this->sendingResponseMiddleware, $middleware); |
||
| 482 | |||
| 483 | return $new; |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @inheritDoc |
||
| 488 | * |
||
| 489 | * @return class-string<TerminatedMiddlewareContract>[] |
||
| 490 | */ |
||
| 491 | #[Override] |
||
| 492 | public function getTerminatedMiddleware(): array |
||
| 493 | { |
||
| 494 | return $this->terminatedMiddleware; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @inheritDoc |
||
| 499 | * |
||
| 500 | * @param class-string<TerminatedMiddlewareContract> ...$middleware The middleware |
||
| 501 | */ |
||
| 502 | #[Override] |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @inheritDoc |
||
| 514 | * |
||
| 515 | * @param class-string<TerminatedMiddlewareContract> ...$middleware The middleware |
||
| 516 | */ |
||
| 517 | #[Override] |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @inheritDoc |
||
| 529 | */ |
||
| 530 | #[Override] |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @inheritDoc |
||
| 538 | */ |
||
| 539 | #[Override] |
||
| 540 | public function withRequestStruct(string|null $requestStruct = null): static |
||
| 541 | { |
||
| 542 | $new = clone $this; |
||
| 543 | |||
| 544 | $new->requestStruct = $requestStruct; |
||
| 545 | |||
| 546 | return $new; |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @inheritDoc |
||
| 551 | */ |
||
| 552 | #[Override] |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @inheritDoc |
||
| 560 | */ |
||
| 561 | #[Override] |
||
| 562 | public function withResponseStruct(string|null $responseStruct = null): static |
||
| 563 | { |
||
| 564 | $new = clone $this; |
||
| 565 | |||
| 566 | $new->responseStruct = $responseStruct; |
||
| 567 | |||
| 568 | return $new; |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Validate a path. |
||
| 573 | * |
||
| 574 | * @param non-empty-string $path The path |
||
| 575 | * |
||
| 576 | * @return non-empty-string |
||
| 577 | */ |
||
| 578 | protected function getFilteredPath(string $path): string |
||
| 579 | { |
||
| 590 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths