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 |
||
34 | class DataOperations { |
||
35 | |||
36 | private $wrapper; |
||
37 | |||
38 | /** |
||
39 | * Private instance of driver adapter |
||
40 | * |
||
41 | * @var DriverAdapter |
||
42 | */ |
||
43 | private $adapter; |
||
44 | |||
45 | /** |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | private $data; |
||
50 | private $invalidFields; |
||
51 | private $hasMultipleData; |
||
52 | private $driver; |
||
53 | private $container; |
||
54 | |||
55 | const MODE_SAVE = 0; |
||
56 | const MODE_UPDATE = 1; |
||
57 | |||
58 | 35 | View Code Duplication | public function __construct(ORMContext $context, RecordWrapper $wrapper, DriverAdapter $adapter) { |
64 | |||
65 | 10 | public function doSave($hasMultipleData) { |
|
107 | |||
108 | public function doValidate() { |
||
116 | |||
117 | /** |
||
118 | * Save an individual record. |
||
119 | * |
||
120 | * @param array $record The record to be saved |
||
121 | * @param type $primaryKey The primary keys of the record |
||
122 | * @return boolean |
||
123 | */ |
||
124 | 10 | private function saveRecord(&$record, $primaryKey) { |
|
125 | $status = [ |
||
126 | 10 | 'success' => true, |
|
127 | 'pk_assigned' => null, |
||
128 | 'invalid_fields' => [] |
||
129 | ]; |
||
130 | |||
131 | // Determine if the primary key of the record is set. |
||
132 | 10 | $pkSet = $this->isPrimaryKeySet($primaryKey, $record); |
|
133 | |||
134 | // Reset the data in the model to contain only the data to be saved |
||
135 | 10 | $this->wrapper->setData($record); |
|
136 | |||
137 | // Run preUpdate or preSave callbacks on models and behaviours |
||
138 | 10 | if ($pkSet) { |
|
139 | 2 | $this->wrapper->preUpdateCallback(); |
|
140 | 2 | $record = $this->wrapper->getData(); |
|
141 | 2 | $record = reset($record) === false ? [] : reset($record); |
|
142 | } else { |
||
143 | 8 | $this->wrapper->preSaveCallback(); |
|
144 | 8 | $record = $this->wrapper->getData(); |
|
145 | 8 | $record = reset($record) === false ? [] : reset($record); |
|
146 | } |
||
147 | |||
148 | // Validate the data |
||
149 | 10 | $validity = $this->validate( |
|
150 | 10 | $record, $pkSet ? DataOperations::MODE_UPDATE : DataOperations::MODE_SAVE |
|
151 | ); |
||
152 | |||
153 | // Exit if data is invalid |
||
154 | 10 | if ($validity !== true) { |
|
155 | 4 | $status['invalid_fields'] = $validity; |
|
156 | 4 | $status['success'] = false; |
|
157 | 4 | return $status; |
|
158 | } |
||
159 | |||
160 | // Save any relationships that are attached to the data |
||
161 | 6 | $relationships = $this->wrapper->getDescription()->getRelationships(); |
|
162 | 6 | $presentRelationships = []; |
|
163 | |||
164 | 6 | foreach($relationships ?? [] as $model => $relationship) { |
|
165 | 6 | if(isset($record[$model])) { |
|
166 | $relationship->preSave($record, $record[$model]); |
||
167 | 6 | $presentRelationships[$model] = $relationship; |
|
168 | } |
||
169 | } |
||
170 | |||
171 | // Assign the data to the wrapper again |
||
172 | 6 | $this->wrapper->setData($record); |
|
173 | |||
174 | // Update or save the data and run post callbacks |
||
175 | 6 | if ($pkSet) { |
|
176 | 2 | $this->adapter->update($record); |
|
177 | 2 | $this->wrapper->postUpdateCallback(); |
|
178 | } else { |
||
179 | 4 | $this->adapter->insert($record); |
|
180 | 4 | $keyValue = $this->driver->getLastInsertId(); |
|
181 | 4 | $this->wrapper->{$primaryKey[0]} = $keyValue; |
|
182 | 4 | $this->wrapper->postSaveCallback($keyValue); |
|
183 | } |
||
184 | |||
185 | // Reset the data so it contains any modifications made by callbacks |
||
186 | 6 | $record = $this->wrapper->getData()[0]; |
|
187 | 6 | foreach($presentRelationships as $model => $relationship) { |
|
188 | $relationship->postSave($record); |
||
189 | } |
||
190 | |||
191 | 6 | return $status; |
|
192 | } |
||
193 | |||
194 | 10 | private function validate($data, $mode) { |
|
206 | |||
207 | 10 | private function isPrimaryKeySet($primaryKey, $data) { |
|
218 | |||
219 | 4 | private function assignValue(&$property, $value) { |
|
226 | |||
227 | public function getData() { |
||
230 | |||
231 | 10 | public function getInvalidFields() { |
|
234 | |||
235 | public function isItemDeletable($primaryKey, $data) { |
||
242 | |||
243 | } |
||
244 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.