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 PopoloCollection implements \Countable, \ArrayAccess, \Iterator |
||
8 | { |
||
9 | protected $properties = [ |
||
10 | 'first', |
||
11 | ]; |
||
12 | public $lookupFromKey; |
||
13 | private $objectClass; |
||
14 | private $objectArr; |
||
15 | private $position; |
||
16 | |||
17 | /** |
||
18 | * |
||
19 | */ |
||
20 | 279 | public function __construct($dataArr, $objectClass, $allPopolo) |
|
33 | |||
34 | public function __toString() |
||
38 | |||
39 | 126 | View Code Duplication | public function __get($prop) |
47 | |||
48 | 126 | private function getFirst() |
|
52 | |||
53 | 15 | public function filter($filters) |
|
70 | |||
71 | 9 | public function get($filters) |
|
84 | |||
85 | // Countable interface |
||
86 | 87 | public function count() |
|
90 | |||
91 | // ArrayAccess interface |
||
92 | public function offsetExists($offset) |
||
96 | |||
97 | // ArrayAccess interface |
||
98 | 126 | public function offsetGet($offset) |
|
102 | |||
103 | // ArrayAccess interface |
||
104 | public function offsetSet($offset, $value) |
||
108 | |||
109 | // ArrayAccess interface |
||
110 | public function offsetUnset($offset) |
||
114 | |||
115 | // Iterator interface |
||
116 | 3 | public function current() |
|
120 | |||
121 | // Iterator interface |
||
122 | public function key() |
||
126 | |||
127 | // Iterator interface |
||
128 | 3 | public function next() |
|
132 | |||
133 | // Iterator interface |
||
134 | 3 | public function rewind() |
|
138 | |||
139 | // Iterator interface |
||
140 | 3 | public function valid() |
|
144 | } |
||
145 |
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.