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 |
||
14 | class Runner |
||
15 | { |
||
16 | /** @var array */ |
||
17 | protected $stack = []; |
||
18 | |||
19 | /** @var BaseHandler */ |
||
20 | protected $handler; |
||
21 | |||
22 | /** @var Container */ |
||
23 | protected $container; |
||
24 | |||
25 | /** |
||
26 | * @param array $stack |
||
27 | * @param BaseHandler $handler |
||
28 | * @param Container $container |
||
29 | */ |
||
30 | 4 | public function __construct(array $stack, BaseHandler $handler, Container $container) |
|
36 | |||
37 | /** |
||
38 | * Run |
||
39 | * @param Request $request |
||
40 | * @param Response $response |
||
41 | * @return IResponse |
||
42 | * @throws UnresolvedMiddlewareException |
||
43 | */ |
||
44 | 4 | public function __invoke(Request $request, Response $response) |
|
53 | |||
54 | /** |
||
55 | * Get middleware - service from container or given handler |
||
56 | * @param string $entry |
||
57 | * @return Closure|false |
||
58 | */ |
||
59 | 4 | View Code Duplication | private function getMiddleware($entry) |
76 | } |
||
77 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.