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 |
||
20 | class ArrayObject extends AbstractObject implements IteratorAggregate, Countable |
||
21 | { |
||
22 | /** |
||
23 | * @var ObjectInterface[] |
||
24 | */ |
||
25 | private $items = []; |
||
26 | |||
27 | /** |
||
28 | * @param ObjectInterface[] $items |
||
29 | */ |
||
30 | public function __construct(array $items = []) |
||
36 | |||
37 | /** |
||
38 | * @param ObjectInterface $object |
||
39 | */ |
||
40 | public function append(ObjectInterface $object) |
||
44 | |||
45 | /** |
||
46 | * @param int $index |
||
47 | * @param ObjectInterface $object |
||
48 | */ |
||
49 | public function insert($index, ObjectInterface $object) |
||
65 | |||
66 | /** |
||
67 | * @param int $index |
||
68 | */ |
||
69 | View Code Duplication | public function remove($index) |
|
80 | |||
81 | /** |
||
82 | * @param int $index |
||
83 | * @return ObjectInterface |
||
84 | */ |
||
85 | View Code Duplication | public function get($index) |
|
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function writeToStream(SplFileObject $fileObject, $encryptionKey) |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function getIterator() |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function count($mode = 'COUNT_NORMAL') |
||
133 | } |
||
134 |
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.