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 |
||
16 | class CompositeAction implements ActionInterface, DebugPrintTreeActionInterface, CompositeActionInterface |
||
17 | { |
||
18 | /** @var ActionInterface[] */ |
||
19 | protected $children = array(); |
||
20 | |||
21 | /** |
||
22 | * @param ActionInterface[] $children |
||
23 | */ |
||
24 | 12 | public function __construct(array $children = array()) |
|
30 | |||
31 | /** |
||
32 | * @return ActionInterface[] |
||
33 | */ |
||
34 | 6 | public function getChildren() |
|
38 | |||
39 | /** |
||
40 | * @param ActionInterface $action |
||
41 | * |
||
42 | * @return CompositeAction |
||
43 | */ |
||
44 | 11 | public function add(ActionInterface $action) |
|
50 | |||
51 | /** |
||
52 | * @param callable $callable |
||
53 | * |
||
54 | * @return ActionInterface|null |
||
55 | */ |
||
56 | 1 | public function map($callable) |
|
65 | |||
66 | /** |
||
67 | * @param ContextInterface $context |
||
68 | * |
||
69 | * @return void |
||
70 | */ |
||
71 | 6 | public function execute(ContextInterface $context) |
|
77 | |||
78 | /** |
||
79 | * @return array |
||
80 | */ |
||
81 | 2 | public function debugPrintTree() |
|
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | 1 | public function __toString() |
|
106 | } |
||
107 |
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.