Completed
Pull Request — master (#16)
by
unknown
01:12
created

RouteTranslationsListCommand::getLocaleRoutes()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace RichanFongdasen\I18n\Commands;
4
5
use Illuminate\Contracts\Console\Kernel;
6
use Illuminate\Foundation\Console\RouteListCommand;
7
use RichanFongdasen\I18n\Traits\TranslatedRouteCommandContext;
8
use Symfony\Component\Console\Input\InputArgument;
9
10
class RouteTranslationsListCommand extends RouteListCommand
11
{
12
    use TranslatedRouteCommandContext;
13
14
    /**
15
     * @var string
16
     */
17
    protected $name = 'route:trans:list';
18
19
    /**
20
     * @var string
21
     */
22
    protected $description = 'List all registered routes for specific locales';
23
24
    /**
25
     * Execute the console command.
26
     */
27
    public function handle()
28
    {
29
        $locale = $this->argument('locale');
30
31
        if (!$this->isSupportedLocale($locale)) {
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $this->argument('locale') on line 29 can also be of type array or null; however, RichanFongdasen\I18n\Tra...xt::isSupportedLocale() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32
            return $this->error("Unsupported locale: '{$locale}'.");
33
        }
34
35
        $this->displayRoutes($this->getLocaleRoutes($locale));
36
    }
37
38
    /**
39
     * Compile the locale routes into a displayable format.
40
     *
41
     * @return array
42
     */
43
    protected function getLocaleRoutes($locale)
44
    {
45
        $routes = $this->getFreshApplicationRoutes($locale);
46
47
        $routes = collect($routes)->map(function ($route) {
48
            return $this->getRouteInformation($route);
49
        })->filter()->all();
50
51
        if ($sort = $this->option('sort')) {
52
            $routes = $this->sortRoutes($sort, $routes);
53
        }
54
55
        if ($this->option('reverse')) {
56
            $routes = array_reverse($routes);
57
        }
58
59
        return $this->pluckColumns($routes);
60
    }
61
62
    /**
63
     * Boot a fresh copy of the application and get the routes.
64
     *
65
     * @param string $locale
66
     *
67
     * @return \Illuminate\Routing\RouteCollection
68
     */
69
    protected function getFreshApplicationRoutes($locale)
70
    {
71
        $key = $this->getLocaleEnvKey();
72
        putenv("{$key}={$locale}");
73
        $app = require $this->getBootstrapPath().'/app.php';
74
        $app->make(Kernel::class)->bootstrap();
75
        $routes = $app['router']->getRoutes();
76
        putenv("{$key}");
77
78
        return $routes;
79
    }
80
81
    /**
82
     * Get the console command arguments.
83
     *
84
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[][].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
85
     */
86
    protected function getArguments()
87
    {
88
        return [
89
            ['locale', InputArgument::REQUIRED, 'The locale to list routes for.'],
90
        ];
91
    }
92
}
93