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 | class QueryImpl implements Query { |
||
14 | /** |
||
15 | * @var Graph |
||
16 | */ |
||
17 | protected $graph; |
||
18 | |||
19 | /** |
||
20 | * @var array[] |
||
21 | */ |
||
22 | protected $steps; |
||
23 | |||
24 | /** |
||
25 | * @var PredicateFactory |
||
26 | */ |
||
27 | protected $predicate_factory; |
||
28 | |||
29 | 61 | public function __construct(Graph $graph) { |
|
34 | |||
35 | /** |
||
36 | * @inheritdocs |
||
37 | */ |
||
38 | 41 | public function predicate_factory() { |
|
41 | |||
42 | /** |
||
43 | * @inheritdocs |
||
44 | */ |
||
45 | 34 | public function expand(\Closure $expander) { |
|
51 | |||
52 | /** |
||
53 | * @inheritdocs |
||
54 | */ |
||
55 | 60 | public function extract(\Closure $extractor) { |
|
61 | |||
62 | /** |
||
63 | * @inheritdocs |
||
64 | */ |
||
65 | 54 | public function filter(Predicate $predicate) { |
|
71 | |||
72 | /** |
||
73 | * @inheritdocs |
||
74 | */ |
||
75 | 60 | public function run($result) { |
|
98 | |||
99 | /** |
||
100 | * @return Iterator <[Node,mixed]> |
||
101 | */ |
||
102 | 60 | protected function switch_run_command(\Iterator $nodes, $step) { |
|
117 | |||
118 | /** |
||
119 | * @return Iterator <[Node,mixed]> |
||
120 | */ |
||
121 | 34 | protected function run_expand(\Iterator $nodes, \Closure $clsr) { |
|
131 | |||
132 | /** |
||
133 | * @return Iterator <[Node,mixed]> |
||
134 | */ |
||
135 | 60 | protected function run_extract(\Iterator $nodes, \Closure $clsr) { |
|
146 | |||
147 | /** |
||
148 | * @return Iterator <[Node,mixed]> |
||
149 | */ |
||
150 | 54 | protected function run_filter(\Iterator $nodes, Predicate $predicate) { |
|
161 | |||
162 | /** |
||
163 | * @return Iterator <[Node,mixed]> |
||
164 | */ |
||
165 | 60 | protected function add_result(\Iterator $nodes, &$result) { |
|
172 | |||
173 | // Convenience Functions |
||
174 | |||
175 | /** |
||
176 | * @inheritdocs |
||
177 | */ |
||
178 | 13 | public function filter_by_types(array $types) { |
|
191 | |||
192 | /** |
||
193 | * @inheritdocs |
||
194 | */ |
||
195 | 29 | public function expand_relations(array $types) { |
|
204 | |||
205 | /** |
||
206 | * @inheritdocs |
||
207 | */ |
||
208 | public function expand_target() { |
||
213 | } |
||
214 |
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.