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 ModelSoftDeleting |
||
12 | { |
||
13 | /** |
||
14 | * Shows that this is a soft deleting model. |
||
15 | * |
||
16 | * @var bool |
||
17 | */ |
||
18 | public $softDeletingModel = true; |
||
19 | |||
20 | /** |
||
21 | * Overrides the ModelQuerying provided method |
||
22 | * with one which searches withTrashed scope. |
||
23 | * |
||
24 | * @param int $modelitem_id |
||
25 | * |
||
26 | * @return |
||
27 | */ |
||
28 | public function find($modelitem_id) |
||
32 | |||
33 | /** |
||
34 | * Finds model and includes withTrashed() scope in query. |
||
35 | * |
||
36 | * @param int $modelitem_id |
||
37 | * |
||
38 | * @return |
||
39 | */ |
||
40 | public function findWithTrashed($modelitem_id) |
||
46 | |||
47 | /** |
||
48 | * Finds model and includes onlyTrashed() scope in query. |
||
49 | * |
||
50 | * @param int $modelitem_id |
||
51 | * |
||
52 | * @return |
||
53 | */ |
||
54 | public function findOnlyTrashed($modelitem_id) |
||
60 | |||
61 | /** |
||
62 | * Method fired before the Delete action is undertaken. |
||
63 | * |
||
64 | * @return |
||
65 | */ |
||
66 | protected function beforeDelete() |
||
72 | |||
73 | /** |
||
74 | * Delete Action. |
||
75 | * |
||
76 | * Fires off beforeDelete(), doDelete() and afterDelete() |
||
77 | * |
||
78 | * @param int $modelitem_id |
||
79 | * |
||
80 | * @return |
||
81 | */ |
||
82 | View Code Duplication | public function delete($modelitem_id) |
|
100 | |||
101 | /** |
||
102 | * The actual delete action. |
||
103 | * |
||
104 | * @return |
||
105 | */ |
||
106 | private function doDelete() |
||
118 | |||
119 | /** |
||
120 | * Method fired after the Delete action is complete. |
||
121 | * |
||
122 | * @return |
||
123 | */ |
||
124 | protected function afterDelete() |
||
130 | } |
||
131 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: