Conditions | 9 |
Paths | 15 |
Total Lines | 55 |
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 |
||
143 | private function buildLink(HasLinks $model, string $routePart, Router $router) |
||
144 | { |
||
145 | $routeStub = $model->getRouteName(); |
||
146 | |||
147 | if ($routeStub === null) { |
||
148 | return false; |
||
149 | } |
||
150 | |||
151 | if (!property_exists($this, 'primaryKey')) { |
||
152 | return false; |
||
153 | } |
||
154 | |||
155 | // Make the route name, and check if it exists. |
||
156 | $routeName = "$routeStub.$routePart"; |
||
157 | |||
158 | if (!$router->has($routeName)) { |
||
159 | return false; |
||
160 | } |
||
161 | |||
162 | // Get any params needed to build the URL. |
||
163 | $params = []; |
||
164 | switch ($routePart) { |
||
165 | case 'destroy': |
||
166 | case 'update': |
||
167 | case 'show': |
||
168 | $params = [$this->getRouteKey() => $this->primaryKey]; |
||
169 | break; |
||
170 | } |
||
171 | |||
172 | // Get the route. |
||
173 | $route = $router->getRoutes()->getByName($routeName); |
||
174 | |||
175 | if (!$route) { |
||
176 | return false; |
||
177 | } |
||
178 | |||
179 | // Get the methods applicable to the route, ignoring HEAD and PATCH. |
||
180 | $methods = collect($route->methods()); |
||
181 | $methods = $methods->filter(static function ($item) { |
||
182 | return !in_array($item, ['HEAD', 'PATCH']); |
||
183 | })->map(static function ($str) { |
||
184 | return strtolower($str); |
||
185 | }); |
||
186 | |||
187 | // If there's only 1, return just that, otherwise, return an array. |
||
188 | if ($methods->count() === 1) { |
||
189 | $methods = $methods->first(); |
||
190 | } |
||
191 | |||
192 | // Add! |
||
193 | return [ |
||
194 | 'method' => $methods, |
||
195 | 'href' => route($routeName, $params, false) |
||
196 | ]; |
||
197 | } |
||
198 | |||
212 |