| Conditions | 10 |
| Paths | 24 |
| Total Lines | 35 |
| Code Lines | 23 |
| 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 |
||
| 27 | public function bulkSerialise($model) |
||
| 28 | { |
||
| 29 | $class = get_class($model); |
||
| 30 | assert($model instanceof Model, $class); |
||
| 31 | // dig up metadata |
||
| 32 | if (!isset(static::$metadataCache[$class])) { |
||
|
|
|||
| 33 | static::$metadataCache[$class] = $model->metadata(); |
||
| 34 | } |
||
| 35 | $meta = static::$metadataCache[$class]; |
||
| 36 | $keys = array_keys($meta); |
||
| 37 | // dig up getter list - we only care about the mutators that end up in metadata |
||
| 38 | if (!isset(static::$mutatorCache[$class])) { |
||
| 39 | $getterz = []; |
||
| 40 | $datez = $model->getDates(); |
||
| 41 | $castz = $model->getCasts(); |
||
| 42 | foreach ($keys as $key) { |
||
| 43 | if ($model->hasGetMutator($key) || in_array($key, $datez) || array_key_exists($key, $castz)) { |
||
| 44 | $getterz[] = $key; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | static::$mutatorCache[$class] = $getterz; |
||
| 48 | } |
||
| 49 | $getterz = static::$mutatorCache[$class]; |
||
| 50 | $result = array_intersect_key($model->getAttributes(), $meta); |
||
| 51 | foreach ($keys as $key) { |
||
| 52 | if (!isset($result[$key])) { |
||
| 53 | $result[$key] = null; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | foreach ($getterz as $getter) { |
||
| 57 | $result[$getter] = $model->$getter; |
||
| 58 | } |
||
| 59 | |||
| 60 | return $result; |
||
| 61 | } |
||
| 62 | |||
| 69 |
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
SomeClassto useselfinstead: