Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php declare(strict_types=1); |
||
| 15 | final class MiddlewareRunner |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private $options; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var MiddlewareInterface[] |
||
| 24 | */ |
||
| 25 | private $middlewares; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var AnnotationReader |
||
| 29 | */ |
||
| 30 | private $annotationReader; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * MiddlewareRunner constructor. |
||
| 34 | * @param array $options |
||
| 35 | * @param MiddlewareInterface[] $middlewares |
||
| 36 | */ |
||
| 37 | 1 | public function __construct(array $options, MiddlewareInterface ...$middlewares) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Sort the middlewares by priority |
||
| 46 | * |
||
| 47 | * @param string $method |
||
| 48 | * @param MiddlewareInterface[] $middlewares |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | 1 | protected function orderMiddlewares(string $method, MiddlewareInterface ...$middlewares): array |
|
| 59 | |||
| 60 | 1 | private function getPriority(string $method, MiddlewareInterface $middleware): int |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @param RequestInterface $request |
||
| 76 | * @return CancellablePromiseInterface |
||
| 77 | */ |
||
| 78 | 1 | public function pre( |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param ResponseInterface $response |
||
| 98 | * @return CancellablePromiseInterface |
||
| 99 | */ |
||
| 100 | 1 | View Code Duplication | public function post( |
| 118 | |||
| 119 | /** |
||
| 120 | * @param Throwable $throwable |
||
| 121 | * @return CancellablePromiseInterface |
||
| 122 | */ |
||
| 123 | 1 | View Code Duplication | public function error( |
| 142 | } |
||
| 143 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..