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 |
||
10 | trait ModelWriting |
||
11 | { |
||
12 | use ModelCreating, ModelEditting, ModelDeleting; |
||
13 | |||
14 | /** |
||
15 | * Used by beforeSave() to ensure child classes call parent::beforeSave(). |
||
16 | * |
||
17 | * @var bool |
||
18 | */ |
||
19 | protected $brokenBeforeSave = false; |
||
20 | |||
21 | /** |
||
22 | * Used by afterSave() to ensure child classes call parent::afterSave(). |
||
23 | * |
||
24 | * @var bool |
||
25 | */ |
||
26 | protected $brokenAfterSave = false; |
||
27 | |||
28 | /** |
||
29 | * Relations to Update during Save and |
||
30 | * the appropriate method to fire the update with. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $doSaveRelations = ['BelongsTo' => 'associate']; |
||
35 | |||
36 | /** |
||
37 | * Relations to Update after Save and |
||
38 | * the appropriate method to fire the update with. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $afterSaveRelations = ['BelongsToMany' => 'sync']; |
||
43 | |||
44 | /** |
||
45 | * Trait Requires hasSetMutator method. |
||
46 | * |
||
47 | * @param string $key |
||
48 | * |
||
49 | * @return |
||
50 | */ |
||
51 | abstract public function hasSetMutator($key); |
||
52 | |||
53 | /** |
||
54 | * Trait Requires setAttribute method. |
||
55 | * |
||
56 | * @param string $key |
||
57 | * @param string $value |
||
58 | * |
||
59 | * @return |
||
60 | */ |
||
61 | abstract public function setAttribute($key, $value); |
||
62 | |||
63 | /** |
||
64 | * Trait Requires Find Method (usually provided by ModelQuerying). |
||
65 | * |
||
66 | * @param int $modelitem_id |
||
67 | * |
||
68 | * @return |
||
69 | */ |
||
70 | abstract protected function find($modelitem_id); |
||
71 | |||
72 | /** |
||
73 | * Method fired before the Save action is undertaken. |
||
74 | * |
||
75 | * @return |
||
76 | */ |
||
77 | protected function beforeSave() |
||
83 | |||
84 | /** |
||
85 | * Save Action. |
||
86 | * |
||
87 | * Fires off beforeSave(), doSave() and afterSave() |
||
88 | * |
||
89 | * @return |
||
90 | */ |
||
91 | public function save() |
||
107 | |||
108 | /** |
||
109 | * The actual Save action, which does all of hte pre-processing |
||
110 | * required before we are able to perform the save() function. |
||
111 | * |
||
112 | * @return |
||
113 | */ |
||
114 | private function doSave() |
||
139 | |||
140 | /** |
||
141 | * Method fired after the Save action is complete. |
||
142 | * |
||
143 | * @return |
||
144 | */ |
||
145 | protected function afterSave() |
||
159 | |||
160 | /** |
||
161 | * Method fired to Save Relations. |
||
162 | * |
||
163 | * @param string $action The current action (either doSave or afterSave) |
||
164 | * @param string $key |
||
165 | * @param string $value |
||
166 | * |
||
167 | * @return |
||
168 | */ |
||
169 | private function saveRelation($action, $key, $value) |
||
182 | } |
||
183 |
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: