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 |
||
5 | class SubsetContainer extends \SplHeap { |
||
6 | |||
7 | /** |
||
8 | * @var BasePartitionAlgorithm |
||
9 | */ |
||
10 | protected $algo; |
||
11 | |||
12 | /** |
||
13 | * Override compare method. |
||
14 | * |
||
15 | * @param Subset $a |
||
16 | * @param Subset $b |
||
17 | * |
||
18 | * @return int |
||
19 | */ |
||
20 | public function compare($a, $b) { |
||
21 | $al = $a->getAlgo()->getSubsetWeight($a); |
||
22 | $bl = $b->getAlgo()->getSubsetWeight($b); |
||
23 | |||
24 | if ($al == $bl) { |
||
25 | return 0; |
||
26 | } |
||
27 | return ($al > $bl) ? -1 : +1; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param $partition |
||
32 | */ |
||
33 | public function setPartition($partition) { |
||
34 | for($i=0; $i<$partition; $i++) { |
||
35 | $subset = new Subset(); |
||
36 | $subset->setAlgo($this->getAlgo()); |
||
37 | $this->insert($subset); |
||
38 | } |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param SubsetItem[] $items |
||
43 | */ |
||
44 | public function addItemsToSubset(array $items = array()) { |
||
49 | |||
50 | /** |
||
51 | * @param \drupol\phpartition\SubsetItem $item |
||
52 | */ |
||
53 | public function addItemToSubset(SubsetItem $item) { |
||
59 | |||
60 | /** |
||
61 | * @return Subset[] |
||
62 | */ |
||
63 | View Code Duplication | public function getSubsets() { |
|
73 | |||
74 | /** |
||
75 | * @return array |
||
76 | */ |
||
77 | View Code Duplication | public function getSubsetsAndItemsAsArray() { |
|
86 | |||
87 | /** |
||
88 | * @param \drupol\phpartition\BasePartitionAlgorithm $algo |
||
89 | */ |
||
90 | public function setAlgo(BasePartitionAlgorithm $algo) { |
||
93 | |||
94 | /** |
||
95 | * @return BasePartitionAlgorithm |
||
96 | */ |
||
97 | public function getAlgo() { |
||
100 | |||
101 | } |
||
102 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.