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 |
||
10 | class PartitionContainer extends \SplHeap |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * The algorithm to use. |
||
15 | * |
||
16 | * @var BasePartitionAlgorithm |
||
17 | */ |
||
18 | protected $algo; |
||
19 | |||
20 | /** |
||
21 | * The number of partition to use. |
||
22 | * |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $size; |
||
26 | |||
27 | /** |
||
28 | * Override compare method. |
||
29 | * |
||
30 | * @param Partition $partitionA |
||
31 | * The first partition. |
||
32 | * @param Partition $partitionB |
||
33 | * The second partition. |
||
34 | * |
||
35 | * {@inheritdoc}. |
||
36 | */ |
||
37 | 14 | public function compare($partitionA, $partitionB) |
|
48 | |||
49 | /** |
||
50 | * Set the size of the container. |
||
51 | * |
||
52 | * @param int $size |
||
53 | * The size. |
||
54 | */ |
||
55 | 14 | public function setSize($size) |
|
65 | |||
66 | /** |
||
67 | * Return the size of the container. |
||
68 | * |
||
69 | * @return int |
||
70 | * The number of partitions the container has. |
||
71 | */ |
||
72 | 14 | public function getSize() |
|
76 | |||
77 | /** |
||
78 | * Add items to the partition. |
||
79 | * |
||
80 | * @param PartitionItem[] $items |
||
81 | * The items to add. |
||
82 | */ |
||
83 | 6 | public function addItemsToPartition(array $items = array()) |
|
89 | |||
90 | /** |
||
91 | * Add an item to the first partition. |
||
92 | * |
||
93 | * @param \drupol\phpartition\PartitionItem $item |
||
94 | * The item to add. |
||
95 | */ |
||
96 | 6 | public function addItemToPartition(PartitionItem $item) |
|
103 | |||
104 | /** |
||
105 | * Get the partition in the container. |
||
106 | * |
||
107 | * @return Partition[] |
||
108 | * An array of partitions. |
||
109 | */ |
||
110 | 14 | View Code Duplication | public function getPartitions() |
121 | |||
122 | /** |
||
123 | * Return the items from each partitions in the container. |
||
124 | * |
||
125 | * @return mixed[] |
||
126 | * The items. |
||
127 | */ |
||
128 | 14 | View Code Duplication | public function getPartitionsItemsArray() |
138 | |||
139 | /** |
||
140 | * Set the algorithm to use. |
||
141 | * |
||
142 | * @param \drupol\phpartition\BasePartitionAlgorithm $algo |
||
143 | * The algorithm. |
||
144 | */ |
||
145 | 14 | public function setAlgo(BasePartitionAlgorithm $algo) |
|
149 | |||
150 | /** |
||
151 | * Get the algorithm. |
||
152 | * |
||
153 | * @return BasePartitionAlgorithm |
||
154 | * The algorithm. |
||
155 | */ |
||
156 | 14 | public function getAlgo() |
|
160 | } |
||
161 |
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.