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 |
||
17 | class DocumentManager implements DocumentManagerInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var EventDispatcherInterface |
||
21 | */ |
||
22 | private $eventDispatcher; |
||
23 | |||
24 | /** |
||
25 | * @var array Cached options resolver instances |
||
26 | */ |
||
27 | private $optionsResolvers = []; |
||
28 | |||
29 | /** |
||
30 | * @param EventDispatcherInterface $eventDispatcher |
||
31 | */ |
||
32 | public function __construct(EventDispatcherInterface $eventDispatcher) |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | View Code Duplication | public function find($identifier, $locale = null, array $options = []) |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function create($alias) |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | View Code Duplication | public function persist($document, $locale = null, array $options = []) |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function remove($document) |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function move($document, $destId) |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function copy($document, $destPath) |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function reorder($document, $destId, $after = false) |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function refresh($document) |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function flush() |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function clear() |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function createQuery($query, $locale = null, array $options = []) |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function createQueryBuilder() |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | private function getOptionsResolver($eventName) |
||
178 | } |
||
179 |
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.