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 | class ModelAdminController extends FlareController |
||
12 | { |
||
13 | /** |
||
14 | * ModelAdmin instance which has been resolved. |
||
15 | * |
||
16 | * @var ModelAdmin |
||
17 | */ |
||
18 | protected $modelAdmin; |
||
19 | |||
20 | /** |
||
21 | * Model instance. |
||
22 | * |
||
23 | * @var Model |
||
24 | */ |
||
25 | protected $model; |
||
26 | |||
27 | /** |
||
28 | * __construct. |
||
29 | * |
||
30 | * @param AdminManager $adminManager |
||
31 | */ |
||
32 | public function __construct(AdminManager $adminManager) |
||
46 | |||
47 | /** |
||
48 | * Index page for ModelAdmin. |
||
49 | * |
||
50 | * @return \Illuminate\Http\Response |
||
51 | */ |
||
52 | public function getIndex() |
||
60 | |||
61 | /** |
||
62 | * Lists Trashed Model Items. |
||
63 | * |
||
64 | * @return \Illuminate\Http\Response |
||
65 | */ |
||
66 | View Code Duplication | public function getTrashed() |
|
78 | |||
79 | /** |
||
80 | * List All Model Items inc Trashed. |
||
81 | * |
||
82 | * @return \Illuminate\Http\Response |
||
83 | */ |
||
84 | View Code Duplication | public function getAll() |
|
96 | |||
97 | /** |
||
98 | * Create a new Model Entry from ModelAdmin Create Page. |
||
99 | * |
||
100 | * @return \Illuminate\Http\Response |
||
101 | */ |
||
102 | public function getCreate() |
||
110 | |||
111 | /** |
||
112 | * Receive new Model Entry Post Data, validate it and return user. |
||
113 | * |
||
114 | * @return \Illuminate\Http\RedirectResponse |
||
115 | */ |
||
116 | View Code Duplication | public function postCreate(ModelCreateRequest $request) |
|
126 | |||
127 | /** |
||
128 | * View a Model Entry from ModelAdmin View Page. |
||
129 | * |
||
130 | * @return \Illuminate\Http\Response |
||
131 | */ |
||
132 | public function getView($modelitemId) |
||
144 | |||
145 | /** |
||
146 | * Edit Model Entry from ModelAdmin Edit Page. |
||
147 | * |
||
148 | * @param int $modelitemId |
||
149 | * |
||
150 | * @return \Illuminate\Http\Response |
||
151 | */ |
||
152 | public function getEdit($modelitemId) |
||
162 | |||
163 | /** |
||
164 | * Receive Model Entry Update Post Data, validate it and return user. |
||
165 | * |
||
166 | * @param int $modelitemId |
||
167 | * |
||
168 | * @return \Illuminate\Http\RedirectResponse |
||
169 | */ |
||
170 | View Code Duplication | public function postEdit(ModelEditRequest $request, $modelitemId) |
|
180 | |||
181 | /** |
||
182 | * Delete Model Entry from ModelAdmin Delete Page. |
||
183 | * |
||
184 | * @param int $modelitemId |
||
185 | * |
||
186 | * @return \Illuminate\Http\Response |
||
187 | */ |
||
188 | public function getDelete($modelitemId) |
||
202 | |||
203 | /** |
||
204 | * Receive Model Entry Delete Post Data, validate it and return user. |
||
205 | * |
||
206 | * @param int $modelitemId |
||
207 | * |
||
208 | * @return \Illuminate\Http\RedirectResponse |
||
209 | */ |
||
210 | View Code Duplication | public function postDelete($modelitemId) |
|
220 | |||
221 | /** |
||
222 | * Restore a ModelItem. |
||
223 | * |
||
224 | * @param int $modelitemId |
||
225 | * |
||
226 | * @return \Illuminate\Http\Response |
||
227 | */ |
||
228 | public function getRestore($modelitemId) |
||
236 | |||
237 | /** |
||
238 | * Process Restore ModelItem Request. |
||
239 | * |
||
240 | * @param int $page_id |
||
241 | * |
||
242 | * @return \Illuminate\Http\RedirectResponse |
||
243 | */ |
||
244 | View Code Duplication | public function postRestore($modelitemId) |
|
254 | |||
255 | /** |
||
256 | * Clone a Page. |
||
257 | * |
||
258 | * @param int $modelitemId |
||
259 | * |
||
260 | * @return \Illuminate\Http\Response |
||
261 | */ |
||
262 | View Code Duplication | public function getClone($modelitemId) |
|
272 | } |
||
273 |
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.