Conditions | 13 |
Paths | 2 |
Total Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
156 | private static function addEventListenersForHelpersHighloadblockTables(Capsule $capsule) |
||
157 | { |
||
158 | $dispatcher = $capsule->getEventDispatcher(); |
||
159 | if (!$dispatcher) { |
||
160 | return; |
||
161 | } |
||
162 | |||
163 | $dispatcher->listen(['eloquent.deleted: *'], function($event, $payload) { |
||
164 | /** @var EloquentModel $model */ |
||
165 | $model = $payload[0]; |
||
166 | if (empty($model->multipleHighloadBlockFields)) { |
||
167 | return; |
||
168 | } |
||
169 | |||
170 | $modelTable = $model->getTable(); |
||
171 | foreach ($model->multipleHighloadBlockFields as $multipleHighloadBlockField) { |
||
172 | if (!empty($model['ID'])) { |
||
173 | $tableName = $modelTable.'_'.strtolower($multipleHighloadBlockField); |
||
174 | DB::table($tableName)->where('ID', $model['ID'])->delete(); |
||
175 | } |
||
176 | } |
||
177 | }); |
||
178 | |||
179 | $dispatcher->listen(['eloquent.updated: *', 'eloquent.created: *'], function($event, $payload) { |
||
180 | /** @var EloquentModel $model */ |
||
181 | $model = $payload[0]; |
||
182 | if (empty($model->multipleHighloadBlockFields)) { |
||
183 | return; |
||
184 | } |
||
185 | |||
186 | $dirty = $model->getDirty(); |
||
187 | $modelTable = $model->getTable(); |
||
188 | foreach ($model->multipleHighloadBlockFields as $multipleHighloadBlockField) { |
||
189 | if (isset($dirty[$multipleHighloadBlockField]) && !empty($model['ID'])) { |
||
190 | $tableName = $modelTable.'_'.strtolower($multipleHighloadBlockField); |
||
191 | |||
192 | if (substr($event, 0, 16) === 'eloquent.updated') { |
||
193 | DB::table($tableName)->where('ID', $model['ID'])->delete(); |
||
194 | } |
||
195 | |||
196 | $unserializedValues = unserialize($dirty[$multipleHighloadBlockField]); |
||
197 | if (!$unserializedValues) { |
||
198 | continue; |
||
199 | } |
||
200 | |||
201 | $newRows = []; |
||
202 | foreach ($unserializedValues as $unserializedValue) { |
||
203 | $newRows[] = [ |
||
204 | 'ID' => $model['ID'], |
||
205 | 'VALUE' => $unserializedValue, |
||
206 | ]; |
||
207 | } |
||
208 | |||
209 | if ($newRows) { |
||
210 | DB::table($tableName)->insert($newRows); |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | }); |
||
215 | } |
||
216 | } |
||
217 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: