| Conditions | 10 |
| Paths | 24 |
| Total Lines | 37 |
| Code Lines | 22 |
| 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 |
||
| 32 | public function bulkSerialise(Model $model) |
||
| 33 | { |
||
| 34 | $class = get_class($model); |
||
| 35 | // dig up metadata |
||
| 36 | if (!isset(self::$metadataCache[$class])) { |
||
| 37 | self::$metadataCache[$class] = $model->fetchMetadata(); |
||
| 38 | } |
||
| 39 | $meta = self::$metadataCache[$class]; |
||
| 40 | /** @var string[] $keys */ |
||
| 41 | $keys = array_keys($meta); |
||
| 42 | // dig up getter list - we only care about the mutators that end up in metadata |
||
| 43 | if (!isset(self::$mutatorCache[$class])) { |
||
| 44 | $getterz = []; |
||
| 45 | /** @var \DateTime[] $datez */ |
||
| 46 | $datez = $model->getDates(); |
||
| 47 | /** @var string[] $castz */ |
||
| 48 | $castz = $model->retrieveCasts(); |
||
| 49 | foreach ($keys as $key) { |
||
| 50 | if ($model->hasGetMutator($key) || in_array($key, $datez) || array_key_exists($key, $castz)) { |
||
| 51 | $getterz[] = $key; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | self::$mutatorCache[$class] = $getterz; |
||
| 55 | } |
||
| 56 | $getterz = self::$mutatorCache[$class]; |
||
| 57 | $modelAttrib = $model->getAttributes(); |
||
| 58 | $result = array_intersect_key($modelAttrib, $meta); |
||
| 59 | foreach ($keys as $key) { |
||
| 60 | if (!isset($result[$key])) { |
||
| 61 | $result[$key] = null; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | foreach ($getterz as $getter) { |
||
| 65 | $result[$getter] = $model->{$getter}; |
||
| 66 | } |
||
| 67 | |||
| 68 | return $result; |
||
| 69 | } |
||
| 71 |