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 |
||
23 | class Regions extends Api |
||
24 | { |
||
25 | /** |
||
26 | * The response array. |
||
27 | * |
||
28 | * @var array|null |
||
29 | */ |
||
30 | protected $response = null; |
||
31 | |||
32 | /** |
||
33 | * The cache directory. |
||
34 | * |
||
35 | * @var string|null |
||
36 | */ |
||
37 | protected $cacheDir = __DIR__ . '/cache/'; |
||
38 | |||
39 | /** |
||
40 | * The cache file name. |
||
41 | * |
||
42 | * @var string|null |
||
43 | */ |
||
44 | protected $file = __DIR__ . '/cache/' . 'regions_'; |
||
45 | |||
46 | /** |
||
47 | * Returns a list of all the regions. |
||
48 | * |
||
49 | * @param string $lang |
||
50 | * @return Regions |
||
51 | */ |
||
52 | public function all($lang = 'A') |
||
73 | |||
74 | /** |
||
75 | * Returns a the response. |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function get() |
||
85 | |||
86 | /** |
||
87 | * Returns a certian region info by id. |
||
88 | * |
||
89 | * @param int $regionId |
||
90 | * @return array |
||
91 | */ |
||
92 | View Code Duplication | public function getId(int $regionId) |
|
100 | |||
101 | /** |
||
102 | * Returns a certian region info by id. |
||
103 | * |
||
104 | * @param int $regionId |
||
105 | * @return array |
||
106 | */ |
||
107 | public function byId(int $regionId) |
||
111 | |||
112 | /** |
||
113 | * Returns a certian region info by id. |
||
114 | * |
||
115 | * @param int $regionId |
||
116 | * @return array |
||
117 | */ |
||
118 | public function id(int $regionId) |
||
122 | |||
123 | /** |
||
124 | * Returns a certian region info by name. |
||
125 | * |
||
126 | * @param string $name |
||
127 | * @return array |
||
128 | */ |
||
129 | View Code Duplication | public function getName($name) |
|
137 | |||
138 | /** |
||
139 | * Returns a certian region info by name. |
||
140 | * |
||
141 | * @param string $name |
||
142 | * @return array |
||
143 | */ |
||
144 | public function name($name) |
||
148 | |||
149 | /** |
||
150 | * Returns a certian region info by name. |
||
151 | * |
||
152 | * @param string $name |
||
153 | * @return array |
||
154 | */ |
||
155 | public function byName($name) |
||
159 | |||
160 | /** |
||
161 | * Returns a certian region info by name. |
||
162 | * |
||
163 | * @param string $name |
||
164 | * @return array |
||
165 | */ |
||
166 | public function named($name) |
||
170 | |||
171 | /** |
||
172 | * Check if all() method was called first. |
||
173 | * |
||
174 | * @return void |
||
175 | * @throws \BadMethodCallException |
||
176 | */ |
||
177 | protected function check() |
||
183 | } |
||
184 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.