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 |
||
21 | class MethodNode |
||
22 | { |
||
23 | private $name; |
||
24 | private $code; |
||
25 | private $visibility = 'public'; |
||
26 | private $static = false; |
||
27 | private $returnsReference = false; |
||
28 | private $returnType; |
||
29 | |||
30 | /** |
||
31 | * @var ArgumentNode[] |
||
32 | */ |
||
33 | private $arguments = array(); |
||
34 | |||
35 | /** |
||
36 | * @param string $name |
||
37 | * @param string $code |
||
38 | */ |
||
39 | public function __construct($name, $code = null) |
||
44 | |||
45 | public function getVisibility() |
||
49 | |||
50 | /** |
||
51 | * @param string $visibility |
||
52 | */ |
||
53 | View Code Duplication | public function setVisibility($visibility) |
|
65 | |||
66 | public function isStatic() |
||
70 | |||
71 | public function setStatic($static = true) |
||
75 | |||
76 | public function returnsReference() |
||
80 | |||
81 | public function setReturnsReference() |
||
85 | |||
86 | public function getName() |
||
90 | |||
91 | public function addArgument(ArgumentNode $argument) |
||
95 | |||
96 | /** |
||
97 | * @return ArgumentNode[] |
||
98 | */ |
||
99 | public function getArguments() |
||
103 | |||
104 | public function hasReturnType() |
||
108 | |||
109 | /** |
||
110 | * @param string $type |
||
111 | */ |
||
112 | public function setReturnType($type = null) |
||
145 | |||
146 | public function getReturnType() |
||
150 | |||
151 | /** |
||
152 | * @param string $code |
||
153 | */ |
||
154 | public function setCode($code) |
||
158 | |||
159 | public function getCode() |
||
168 | |||
169 | public function useParentCode() |
||
177 | |||
178 | private function generateArgument(ArgumentNode $arg) |
||
188 | } |
||
189 |
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.