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 |
||
27 | class TemplateRoute implements |
||
28 | ConfigurableInterface, |
||
29 | LoggerAwareInterface, |
||
30 | RouteInterface |
||
31 | { |
||
32 | use ConfigurableTrait; |
||
33 | |||
34 | /** |
||
35 | * @var \Slim\App $app |
||
36 | */ |
||
37 | private $app; |
||
38 | |||
39 | /** |
||
40 | * @var LoggerInterface $logger |
||
41 | */ |
||
42 | private $logger; |
||
43 | |||
44 | /** |
||
45 | * Dependencies: |
||
46 | * - `config` |
||
47 | * - `app` |
||
48 | * |
||
49 | * @param array $data Dependencies |
||
50 | * @throws InvalidArgumentException |
||
51 | */ |
||
52 | View Code Duplication | public function __construct(array $data) |
|
67 | |||
68 | /** |
||
69 | * > LoggerAwareInterface > setLogger() |
||
70 | * |
||
71 | * Fulfills the PSR-1 style LoggerAwareInterface |
||
72 | * |
||
73 | * @param LoggerInterface $logger |
||
74 | * @return AbstractEngine Chainable |
||
75 | */ |
||
76 | public function setLogger(LoggerInterface $logger) |
||
80 | |||
81 | /** |
||
82 | * @param LoggerInterface $logger |
||
83 | * @return AbstractEngine Chainable |
||
84 | */ |
||
85 | public function set_logger(LoggerInterface $logger = null) |
||
90 | |||
91 | /** |
||
92 | * @erturn LoggerInterface |
||
93 | */ |
||
94 | public function logger() |
||
98 | |||
99 | /** |
||
100 | * ConfigurableTrait > create_config() |
||
101 | */ |
||
102 | public function create_config($data = null) |
||
106 | |||
107 | /** |
||
108 | * @return void |
||
109 | */ |
||
110 | public function __invoke(RequestInterface $request, ResponseInterface $response) |
||
126 | } |
||
127 |
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.