Conditions | 13 |
Paths | 20 |
Total Lines | 57 |
Code Lines | 32 |
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 |
||
64 | public static function getRelationshipAsPropertyName( |
||
65 | $value, |
||
66 | $className, |
||
67 | ReflectionClass $reflection, |
||
68 | Driver $serializer |
||
69 | ) { |
||
70 | $methods = []; |
||
71 | foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
||
72 | if (ltrim($method->class, '\\') !== ltrim($className, '\\')) { |
||
73 | continue; |
||
74 | } |
||
75 | |||
76 | $name = $method->name; |
||
77 | $reflectionMethod = $reflection->getMethod($name); |
||
78 | |||
79 | if (!self::isAllowedEloquentModelFunction($name) || $reflectionMethod->getNumberOfParameters() > 0) { |
||
80 | continue; |
||
81 | } |
||
82 | |||
83 | if (in_array($name, $value->getHidden(), true)) { |
||
84 | continue; |
||
85 | } |
||
86 | |||
87 | try { |
||
88 | $returned = $reflectionMethod->invoke($value); |
||
89 | |||
90 | if (!(\is_object($returned) && self::isAnEloquentRelation($returned))) { |
||
91 | continue; |
||
92 | } |
||
93 | |||
94 | $relationData = $returned->getResults(); |
||
95 | |||
96 | if ($relationData instanceof Traversable) { |
||
97 | //Something traversable with Models |
||
98 | $items = []; |
||
99 | |||
100 | foreach ($relationData as $model) { |
||
101 | if ($model instanceof Model) { |
||
102 | $items[] = self::getModelData($serializer, $model); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | $methods[$name] = [ |
||
107 | Serializer::MAP_TYPE => 'array', |
||
108 | Serializer::SCALAR_VALUE => $items, |
||
109 | ]; |
||
110 | } elseif ($relationData instanceof Model) { |
||
111 | //Single element returned. |
||
112 | $methods[$name] = self::getModelData($serializer, $relationData); |
||
113 | } |
||
114 | |||
115 | } catch (ErrorException $e) { |
||
116 | } |
||
117 | } |
||
118 | |||
119 | return $methods; |
||
120 | } |
||
121 | |||
157 |