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 |
||
13 | View Code Duplication | class LastOf implements Selector |
|
|
|||
14 | { |
||
15 | /** |
||
16 | * @var Direction |
||
17 | */ |
||
18 | private $direction; |
||
19 | |||
20 | /** |
||
21 | * @var Evaluator |
||
22 | */ |
||
23 | private $evaluator; |
||
24 | |||
25 | /** |
||
26 | * @param Direction $direction |
||
27 | * @param Evaluator $evaluator |
||
28 | */ |
||
29 | 22 | public function __construct(Direction $direction, Evaluator $evaluator) |
|
34 | |||
35 | /** |
||
36 | * @param string $date |
||
37 | * @param Period $period |
||
38 | * @return string |
||
39 | */ |
||
40 | 22 | public function select($date, Period $period) |
|
57 | |||
58 | /** |
||
59 | * @param \DateTime $startDate |
||
60 | * @param \DateTime $endDate |
||
61 | * @return null|string |
||
62 | * @throws \LogicException |
||
63 | */ |
||
64 | 22 | private function getLastInPeriod(\DateTime $startDate, \DateTime $endDate) |
|
81 | } |
||
82 |
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.