Conditions | 7 |
Paths | 19 |
Total Lines | 77 |
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 |
||
58 | protected static function getDetails(ReflectionMethod $method, string $model) |
||
59 | { |
||
60 | $usePivot = Config::get('schematics.use-pivot'); |
||
61 | try { |
||
62 | $class = app($model); |
||
63 | $invocation = $method->invoke($class); |
||
64 | |||
65 | if ($invocation instanceof Relation) { |
||
66 | if ($usePivot && get_class($invocation) === "Illuminate\Database\Eloquent\Relations\BelongsToMany"){ |
||
67 | $pivotModel = PivotService::getPivotModel($method); |
||
68 | if (!$pivotModel){ |
||
69 | $pivotModel = PivotService::getPivotModelFallback($class, $invocation, $method, self::$models); |
||
70 | } |
||
71 | if ($pivotModel){ |
||
72 | $dst = $invocation->getRelated(); |
||
73 | //TODO: neater way to create these objects |
||
74 | $result = (object)[ |
||
75 | 'model' => $model, |
||
76 | 'table' => $class->getTable(), |
||
77 | 'type' => 'BelongsToMany', |
||
78 | |||
79 | 'relation' => (object)[ |
||
80 | 'model' => get_class($pivotModel), |
||
81 | 'table' => $pivotModel->getTable(), |
||
82 | 'pivotsTo' => get_class($dst), |
||
83 | ], |
||
84 | 'method' => (object)[ |
||
85 | 'name' => $method->getName(), |
||
86 | 'file' => $method->getFileName(), |
||
87 | 'line' => $method->getStartLine(), |
||
88 | ], |
||
89 | ]; |
||
90 | $pivotsTo = (object)[ |
||
91 | 'model' => $model, |
||
92 | 'table' => $class->getTable(), |
||
93 | 'type' => 'BelongsToMany', |
||
94 | |||
95 | 'relation' => (object)[ |
||
96 | 'model' => get_class($dst), |
||
97 | 'table' => $dst->getTable(), |
||
98 | 'pivotsFrom' => $model |
||
99 | ], |
||
100 | 'method' => (object)[ |
||
101 | 'model' => $model, //To make it clear in GUI that function does not come from pivot model, this should be shown in GUI |
||
102 | 'name' => $method->getName(), |
||
103 | 'file' => $method->getFileName(), |
||
104 | 'line' => $method->getStartLine(), |
||
105 | ], |
||
106 | ]; |
||
107 | $result->pivotsTo = $pivotsTo; |
||
108 | } else { |
||
109 | return NULL; |
||
110 | } |
||
111 | } else { |
||
112 | $related = $invocation->getRelated(); |
||
113 | $result = (object)[ |
||
114 | 'model' => $model, |
||
115 | 'table' => $class->getTable(), |
||
116 | 'type' => (new ReflectionClass($invocation))->getShortName(), |
||
117 | 'relation' => (object)[ |
||
118 | 'model' => get_class($related), |
||
119 | 'table' => $related->getTable(), |
||
120 | ], |
||
121 | 'method' => (object)[ |
||
122 | 'name' => $method->getName(), |
||
123 | 'file' => $method->getFileName(), |
||
124 | 'line' => $method->getStartLine(), |
||
125 | ], |
||
126 | ]; |
||
127 | } |
||
128 | return $result; |
||
129 | } |
||
130 | } |
||
131 | catch (\Throwable $e) { |
||
132 | return null; |
||
133 | } |
||
134 | } |
||
135 | } |