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 |
||
11 | trait Representer |
||
12 | { |
||
13 | use ArraySerializer; |
||
14 | |||
15 | /** |
||
16 | * Object that is being represented |
||
17 | * or a collection handler |
||
18 | */ |
||
19 | protected $source; |
||
20 | /** |
||
21 | * Class name to be restored |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $targetClassName; |
||
26 | /** |
||
27 | * Strategy indicator |
||
28 | * 1 = one |
||
29 | * 2 = collection |
||
30 | * 3 = restore one |
||
31 | * 4 = restore collection |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $strategy; |
||
36 | |||
37 | 6 | public function __construct($source = null, $strategy) |
|
46 | |||
47 | public function rules() |
||
51 | |||
52 | 3 | public function setTargetClassName($name) |
|
56 | |||
57 | /** |
||
58 | * @param $name |
||
59 | * @return PropertyRule |
||
60 | */ |
||
61 | 6 | public function property($name) |
|
65 | |||
66 | /** |
||
67 | * Represent one instance |
||
68 | * |
||
69 | * @param $source |
||
70 | * @return static |
||
71 | */ |
||
72 | 4 | public static function one($source) |
|
76 | |||
77 | /** |
||
78 | * Represent collection of instances |
||
79 | * |
||
80 | * @param array $array |
||
81 | */ |
||
82 | public static function collection(array $array) |
||
86 | |||
87 | protected function getCollectionRepresentation() |
||
92 | |||
93 | 4 | View Code Duplication | protected function getOneRepresentation() |
113 | |||
114 | 4 | protected function getRepresentation() |
|
126 | |||
127 | |||
128 | /** |
||
129 | * @param $className |
||
130 | * @return static |
||
131 | */ |
||
132 | 3 | public static function restore($className) |
|
138 | |||
139 | 3 | protected function getReverseRepresentation($projection) |
|
151 | |||
152 | 3 | View Code Duplication | protected function getOneReverseRepresentation($projection) |
171 | |||
172 | protected function getCollectionReverseRepresentation($projection) |
||
176 | } |
||
177 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.