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 |
||
8 | View Code Duplication | final class PrePersistEvent extends Event |
|
|
|||
9 | { |
||
10 | /** |
||
11 | * @var PagerInterface |
||
12 | */ |
||
13 | private $pager; |
||
14 | |||
15 | /** |
||
16 | * @var ObjectPersisterInterface |
||
17 | */ |
||
18 | private $objectPersister; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $options; |
||
24 | |||
25 | 15 | public function __construct(PagerInterface $pager, ObjectPersisterInterface $objectPersister, array $options) |
|
31 | |||
32 | /** |
||
33 | * @return PagerInterface |
||
34 | */ |
||
35 | 10 | public function getPager() |
|
39 | |||
40 | /** |
||
41 | * @param PagerInterface $pager |
||
42 | */ |
||
43 | 1 | public function setPager($pager) |
|
47 | |||
48 | /** |
||
49 | * @return array |
||
50 | */ |
||
51 | 10 | public function getOptions() |
|
55 | |||
56 | /** |
||
57 | * @param array $options |
||
58 | */ |
||
59 | 1 | public function setOptions(array $options) |
|
63 | |||
64 | /** |
||
65 | * @return ObjectPersisterInterface |
||
66 | */ |
||
67 | 3 | public function getObjectPersister() |
|
71 | |||
72 | /** |
||
73 | * @param ObjectPersisterInterface $objectPersister |
||
74 | */ |
||
75 | 1 | public function setObjectPersister(ObjectPersisterInterface $objectPersister) |
|
79 | } |
||
80 |
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.