Conditions | 10 |
Paths | 2 |
Total Lines | 48 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
141 | private static function addEventListenersForHelpersHighloadblockTables(Capsule $capsule) |
||
142 | { |
||
143 | $dispatcher = $capsule->getEventDispatcher(); |
||
144 | if (!$dispatcher) { |
||
145 | return; |
||
146 | } |
||
147 | |||
148 | $dispatcher->listen(['eloquent.created: *'], function($event, $payload) { |
||
149 | // $model = $payload[0]; |
||
150 | // dump(func_get_args()); |
||
151 | }); |
||
152 | |||
153 | $dispatcher->listen(['eloquent.updated: *', 'eloquent.created: *'], function($event, $payload) { |
||
154 | /** @var EloquentModel $model */ |
||
155 | $model = $payload[0]; |
||
156 | if (empty($model->multipleHighloadBlockFields)) { |
||
157 | return; |
||
158 | } |
||
159 | |||
160 | $dirty = $model->getDirty(); |
||
161 | foreach ($model->multipleHighloadBlockFields as $multipleHighloadBlockField) { |
||
162 | if (isset($dirty[$multipleHighloadBlockField]) && !empty($model['ID'])) { |
||
163 | $tableName = $model->getTable().'_'.strtolower($multipleHighloadBlockField); |
||
164 | |||
165 | if (substr($event, 0, 16) === 'eloquent.updated') { |
||
166 | DB::table($tableName)->where('ID', $model['ID'])->delete(); |
||
167 | } |
||
168 | |||
169 | $unserializedValues = unserialize($dirty[$multipleHighloadBlockField]); |
||
170 | if (!$unserializedValues) { |
||
171 | continue; |
||
172 | } |
||
173 | |||
174 | $newRows = []; |
||
175 | foreach ($unserializedValues as $unserializedValue) { |
||
176 | $newRows[] = [ |
||
177 | 'ID' => $model['ID'], |
||
178 | 'VALUE' => $unserializedValue, |
||
179 | ]; |
||
180 | } |
||
181 | |||
182 | if ($newRows) { |
||
183 | DB::table($tableName)->insert($newRows); |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | }); |
||
188 | } |
||
189 | } |
||
190 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: