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 |
||
7 | class Listing implements \Iterator |
||
8 | { |
||
9 | /** |
||
10 | * @var TypeInterface[] |
||
11 | */ |
||
12 | protected $types; |
||
13 | |||
14 | /** |
||
15 | * @var \ReflectionClass |
||
16 | */ |
||
17 | protected $reflectionClass; |
||
18 | |||
19 | /** |
||
20 | * @var Field[] |
||
21 | */ |
||
22 | protected $fields; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $fieldPointer = 0; |
||
28 | |||
29 | /** |
||
30 | * @param TypeInterface[] $types |
||
31 | * @param string $class |
||
32 | */ |
||
33 | View Code Duplication | public function __construct(array $types, $class) |
|
47 | |||
48 | /** |
||
49 | * @param string $name |
||
50 | * @param string $type |
||
51 | * @param array $options |
||
52 | * |
||
53 | * @return $this |
||
54 | */ |
||
55 | public function add($name, $type = 'string', array $options = array()) |
||
84 | |||
85 | /** |
||
86 | * @param string $type |
||
87 | * |
||
88 | * @return TypeInterface |
||
89 | * |
||
90 | * @throws \InvalidArgumentException |
||
91 | */ |
||
92 | protected function getType($type) |
||
100 | |||
101 | /** |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function current() |
||
108 | |||
109 | /** |
||
110 | */ |
||
111 | public function next() |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | public function key() |
||
123 | |||
124 | /** |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function valid() |
||
131 | |||
132 | /** |
||
133 | */ |
||
134 | public function rewind() |
||
138 | } |
||
139 |
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.