| Conditions | 10 |
| Paths | 29 |
| Total Lines | 45 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 85 | protected function getRelationshipMethodsAsPropertyName($value, $className, ReflectionClass $reflection) |
||
| 86 | { |
||
| 87 | |||
| 88 | $methods = []; |
||
| 89 | foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
||
| 90 | if (ltrim($method->class, "\\") === ltrim($className, "\\")) { |
||
| 91 | |||
| 92 | $name = $method->name; |
||
| 93 | $reflectionMethod = $reflection->getMethod($name); |
||
| 94 | |||
| 95 | // Eloquent relations do not include parameters, so we'll be filtering based on this criteria. |
||
| 96 | if (0 == $reflectionMethod->getNumberOfParameters()) { |
||
| 97 | try { |
||
| 98 | $returned = $reflectionMethod->invoke($value); |
||
| 99 | //All operations (eg: boolean operations) are now filtered out. |
||
| 100 | if (is_object($returned)) { |
||
| 101 | |||
| 102 | // Only keep those methods as properties if these are returning Eloquent relations. |
||
| 103 | // But do not run the operation as it is an expensive operation. |
||
| 104 | if (false !== strpos(get_class($returned), 'Illuminate\Database\Eloquent\Relations')) { |
||
| 105 | |||
| 106 | $items = []; |
||
| 107 | foreach ($returned->getResults() as $model) { |
||
| 108 | |||
| 109 | if (is_object($model)) { |
||
| 110 | $stdClass = (object) $model->getAttributes(); |
||
| 111 | $data = $this->serializeData($stdClass); |
||
| 112 | $data[self::CLASS_IDENTIFIER_KEY] = get_class($model); |
||
| 113 | |||
| 114 | $items[] = $data; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | if (!empty($items)) { |
||
| 118 | $methods[$name] = [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; |
||
| 119 | } |
||
| 120 | |||
| 121 | } |
||
| 122 | } |
||
| 123 | } catch (ErrorException $e) {} |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | return $methods; |
||
| 129 | } |
||
| 130 | } |
||
| 131 |