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 |
||
19 | class MapMapper extends AbstractCollectionMapper |
||
20 | { |
||
21 | /** |
||
22 | * @param Slumberer $slumberer |
||
23 | * @param array|\Traversable $value |
||
24 | * |
||
25 | * @return null|\stdClass We return a std class as this will ensure json-encode will create something like {"0":1} |
||
26 | */ |
||
27 | 17 | public function slumber(Slumberer $slumberer, $value) |
|
41 | |||
42 | /** |
||
43 | * @param Awaker $awaker |
||
44 | * @param array|\Traversable $value |
||
45 | * |
||
46 | * @return array|Collection |
||
47 | */ |
||
48 | 20 | public function awake(Awaker $awaker, $value) |
|
64 | |||
65 | /** |
||
66 | * @param Slumberer $slumberer |
||
67 | * @param array $value |
||
68 | * @param Mapper $nested |
||
69 | * |
||
70 | * @return \stdClass |
||
71 | */ |
||
72 | 9 | private function slumberKeepNulls(Slumberer $slumberer, $value, Mapper $nested) |
|
82 | |||
83 | /** |
||
84 | * @param Slumberer $slumberer |
||
85 | * @param array $value |
||
86 | * @param Mapper $nested |
||
87 | * |
||
88 | * @return \stdClass |
||
89 | */ |
||
90 | 3 | private function slumberFilterNulls(Slumberer $slumberer, $value, Mapper $nested) |
|
106 | |||
107 | /** |
||
108 | * @param Awaker $awaker |
||
109 | * @param array $value |
||
110 | * @param Mapper $nested |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | 8 | private function awakeKeepNulls(Awaker $awaker, $value, Mapper $nested) |
|
125 | |||
126 | /** |
||
127 | * @param Awaker $awaker |
||
128 | * @param array $value |
||
129 | * @param Mapper $nested |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | private function awakeFilterNulls(Awaker $awaker, $value, Mapper $nested) |
||
149 | } |
||
150 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.