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 | * @var string |
||
| 34 | */ |
||
| 35 | private $id; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * MiddlewareRunner constructor. |
||
| 39 | * @param array $options |
||
| 40 | * @param MiddlewareInterface[] $middlewares |
||
| 41 | */ |
||
| 42 | public function __construct(array $options, MiddlewareInterface ...$middlewares) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Sort the middlewares by priority |
||
| 53 | * |
||
| 54 | * @param string $method |
||
| 55 | * @param MiddlewareInterface[] $middlewares |
||
| 56 | * @return array |
||
| 57 | */ |
||
| 58 | protected function orderMiddlewares(string $method, MiddlewareInterface ...$middlewares): array |
||
| 66 | |||
| 67 | private function getPriority(string $method, MiddlewareInterface $middleware): int |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param RequestInterface $request |
||
| 84 | * @return CancellablePromiseInterface |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function pre( |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param ResponseInterface $response |
||
| 106 | * @return CancellablePromiseInterface |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function post( |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param Throwable $throwable |
||
| 128 | * @return CancellablePromiseInterface |
||
| 129 | */ |
||
| 130 | View Code Duplication | public function error( |
|
| 147 | } |
||
| 148 |
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..