Conditions | 11 |
Paths | 28 |
Total Lines | 46 |
Code Lines | 26 |
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 |
||
63 | public static function getRelationshipAsPropertyName( |
||
64 | $value, |
||
65 | $className, |
||
66 | ReflectionClass $reflection, |
||
67 | Driver $serializer |
||
68 | ) { |
||
69 | $methods = []; |
||
70 | foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
||
71 | if (\ltrim($method->class, '\\') === \ltrim($className, '\\')) { |
||
72 | $name = $method->name; |
||
73 | $reflectionMethod = $reflection->getMethod($name); |
||
74 | |||
75 | // Eloquent relations do not include parameters, so we'll be filtering based on this criteria. |
||
76 | if (0 == $reflectionMethod->getNumberOfParameters()) { |
||
77 | try { |
||
78 | if (self::isAllowedEloquentModelFunction($name)) { |
||
79 | $returned = $reflectionMethod->invoke($value); |
||
80 | //All operations (eg: boolean operations) are now filtered out. |
||
81 | if (\is_object($returned)) { |
||
82 | |||
83 | // Only keep those methods as properties if these are returning Eloquent relations. |
||
84 | // But do not run the operation as it is an expensive operation. |
||
85 | if (self::isAnEloquentRelation($returned)) { |
||
86 | $items = []; |
||
87 | foreach ($returned->getResults() as $model) { |
||
88 | if (\is_object($model)) { |
||
89 | $items[] = self::getModelData($serializer, $model); |
||
90 | } |
||
91 | } |
||
92 | if (!empty($items)) { |
||
93 | $methods[$name] = [ |
||
94 | Serializer::MAP_TYPE => 'array', |
||
95 | Serializer::SCALAR_VALUE => $items, |
||
96 | ]; |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | } catch (ErrorException $e) { |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | return $methods; |
||
108 | } |
||
109 | |||
145 |