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

RouteTranslationsListCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
        if (empty($this->router->getRoutes())) {
30
            return $this->error("Your application doesn't have any routes.");
31
        }
32
33
        $locale = $this->argument('locale');
34
35
        if (!$this->isSupportedLocale($locale)) {
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $this->argument('locale') on line 33 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...
36
            $this->error("Unsupported locale: '{$locale}'.");
37
38
            return;
39
        }
40
41
        $this->displayRoutes($this->getLocaleRoutes($locale));
42
    }
43
44
    /**
45
     * Compile the locale routes into a displayable format.
46
     *
47
     * @return array
48
     */
49
    protected function getLocaleRoutes($locale)
50
    {
51
        $routes = $this->getFreshApplicationRoutes($locale);
52
53
        $routes = collect($routes)->map(function ($route) {
54
            return $this->getRouteInformation($route);
55
        })->filter()->all();
56
57
        if ($sort = $this->option('sort')) {
58
            $routes = $this->sortRoutes($sort, $routes);
59
        }
60
61
        if ($this->option('reverse')) {
62
            $routes = array_reverse($routes);
63
        }
64
65
        return $this->pluckColumns($routes);
66
    }
67
68
    /**
69
     * Boot a fresh copy of the application and get the routes.
70
     *
71
     * @param string $locale
72
     *
73
     * @return \Illuminate\Routing\RouteCollection
74
     */
75
    protected function getFreshApplicationRoutes($locale)
76
    {
77
        $app = require $this->getBootstrapPath().'/app.php';
78
79
        $key = $this->getLocaleEnvKey();
80
81
        putenv("{$key}={$locale}");
82
83
        $app->make(Kernel::class)->bootstrap();
84
85
        $routes = $app['router']->getRoutes();
86
87
        putenv("{$key}");
88
89
        return $routes;
90
    }
91
92
    /**
93
     * Get the console command arguments.
94
     *
95
     * @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...
96
     */
97
    protected function getArguments()
98
    {
99
        return [
100
            ['locale', InputArgument::REQUIRED, 'The locale to list routes for.'],
101
        ];
102
    }
103
}
104