| Conditions | 8 |
| Paths | 3 |
| Total Lines | 51 |
| 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 |
||
| 49 | public function handle() |
||
| 50 | { |
||
| 51 | $routes = collect($this->router->getRoutes())->filter(function (Route $route) { |
||
| 52 | $uri = $route->uri(); |
||
| 53 | // built-in, parameterized and no-GET are ignored |
||
| 54 | return Str::startsWith($uri, 'admin/') |
||
| 55 | && !Str::startsWith($uri, 'admin/auth/') |
||
| 56 | && !Str::endsWith($uri, '/create') |
||
| 57 | && !Str::contains($uri, '{') |
||
| 58 | && in_array('GET', $route->methods()); |
||
| 59 | }) |
||
| 60 | ->map(function (Route $route) { |
||
| 61 | $uri = substr($route->uri(), strlen('admin/')); |
||
| 62 | |||
| 63 | return [ |
||
| 64 | 'title' => Str::ucfirst( |
||
| 65 | Str::snake(str_replace('/', ' ', $uri), ' ') |
||
| 66 | ), |
||
| 67 | 'uri' => $uri, |
||
| 68 | ]; |
||
| 69 | }) |
||
| 70 | ->pluck('title', 'uri'); |
||
| 71 | |||
| 72 | $menus = Menu::all()->pluck('title', 'uri'); |
||
| 73 | // exclude exist ones |
||
| 74 | $news = $routes->diffKeys($menus)->map(function ($item, $key) { |
||
| 75 | return [ |
||
| 76 | 'title' => $item, |
||
| 77 | 'uri' => $key, |
||
| 78 | 'order' => 10, |
||
| 79 | 'icon' => 'fa-list', |
||
| 80 | ]; |
||
| 81 | })->values()->toArray(); |
||
| 82 | |||
| 83 | if (!$news) { |
||
|
|
|||
| 84 | $this->error('No newly registered routes found.'); |
||
| 85 | } else { |
||
| 86 | if ($this->hasOption('dry-run') && $this->option('dry-run')) { |
||
| 87 | $this->line('<info>The following menu items will be created</info>: '); |
||
| 88 | $this->table(['Title', 'Uri'], array_map(function ($item) { |
||
| 89 | return [ |
||
| 90 | $item['title'], |
||
| 91 | $item['uri'], |
||
| 92 | ]; |
||
| 93 | }, $news)); |
||
| 94 | } else { |
||
| 95 | Menu::insert($news); |
||
| 96 | $this->line('<info>Done!</info>'); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.