Conditions | 17 |
Paths | 12 |
Total Lines | 78 |
Code Lines | 55 |
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 |
||
90 | protected function getRelationshipsFromMethods(bool $biDir = false) |
||
91 | { |
||
92 | $biDirVal = intval($biDir); |
||
93 | $isCached = isset(static::$relationCategories[$biDirVal]) && !empty(static::$relationCategories[$biDirVal]); |
||
94 | if (!$isCached) { |
||
95 | /** @var Model $model */ |
||
96 | $model = $this; |
||
97 | $relationships = [ |
||
98 | 'HasOne' => [], |
||
99 | 'UnknownPolyMorphSide' => [], |
||
100 | 'HasMany' => [], |
||
101 | 'KnownPolyMorphSide' => [] |
||
102 | ]; |
||
103 | $methods = $this->getModelClassMethods($model); |
||
104 | foreach ($methods as $method) { |
||
105 | //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php |
||
106 | $reflection = new \ReflectionMethod($model, $method); |
||
107 | $fileName = $reflection->getFileName(); |
||
108 | |||
109 | $file = new \SplFileObject($fileName); |
||
110 | $file->seek($reflection->getStartLine()-1); |
||
111 | $code = ''; |
||
112 | while ($file->key() < $reflection->getEndLine()) { |
||
113 | $code .= $file->current(); |
||
114 | $file->next(); |
||
115 | } |
||
116 | |||
117 | $code = trim(preg_replace('/\s\s+/', '', $code)); |
||
118 | if (false === stripos($code, 'function')) { |
||
119 | $msg = 'Function definition must have keyword \'function\''; |
||
120 | throw new InvalidOperationException($msg); |
||
121 | } |
||
122 | $begin = strpos($code, 'function('); |
||
123 | $code = substr($code, /* @scrutinizer ignore-type */$begin, strrpos($code, '}')-$begin+1); |
||
124 | $lastCode = $code[strlen(/* @scrutinizer ignore-type */$code)-1]; |
||
125 | if ('}' != $lastCode) { |
||
126 | $msg = 'Final character of function definition must be closing brace'; |
||
127 | throw new InvalidOperationException($msg); |
||
128 | } |
||
129 | foreach (static::$relTypes as $relation) { |
||
130 | $search = '$this->' . $relation . '('; |
||
131 | $found = stripos(/* @scrutinizer ignore-type */$code, $search); |
||
132 | if (!$found) { |
||
133 | continue; |
||
134 | } |
||
135 | //Resolve the relation's model to a Relation object. |
||
136 | $relationObj = $model->$method(); |
||
137 | if (!($relationObj instanceof Relation)) { |
||
138 | continue; |
||
139 | } |
||
140 | $relObject = $relationObj->getRelated(); |
||
141 | $relatedModel = '\\' . get_class($relObject); |
||
142 | if (!in_array(MetadataTrait::class, class_uses($relatedModel))) { |
||
143 | continue; |
||
144 | } |
||
145 | $targObject = $biDir ? $relationObj : $relatedModel; |
||
146 | if (in_array($relation, static::$manyRelTypes)) { |
||
147 | //Collection or array of models (because Collection is Arrayable) |
||
148 | $relationships['HasMany'][$method] = $targObject; |
||
149 | } elseif ('morphTo' === $relation) { |
||
150 | // Model isn't specified because relation is polymorphic |
||
151 | $relationships['UnknownPolyMorphSide'][$method] = |
||
152 | $biDir ? $relationObj : '\Illuminate\Database\Eloquent\Model|\Eloquent'; |
||
153 | } else { |
||
154 | //Single model is returned |
||
155 | $relationships['HasOne'][$method] = $targObject; |
||
156 | } |
||
157 | if (in_array($relation, ['morphMany', 'morphOne', 'morphToMany'])) { |
||
158 | $relationships['KnownPolyMorphSide'][$method] = $targObject; |
||
159 | } |
||
160 | if (in_array($relation, ['morphedByMany'])) { |
||
161 | $relationships['UnknownPolyMorphSide'][$method] = $targObject; |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | static::$relationCategories[$biDirVal] = $relationships; |
||
166 | } |
||
167 | return static::$relationCategories[$biDirVal]; |
||
168 | } |
||
311 |