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

getFreshApplicationRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
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
    /**
26
     * Execute the console command.
27
     */
28 View Code Duplication
    public function handle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        if (count($this->routes) == 0) {
0 ignored issues
show
Bug introduced by
The property routes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
            $this->error("Your application doesn't have any routes.");
32
            return;
33
        }
34
35
        $locale = $this->argument('locale');
36
37
        if ( ! $this->isSupportedLocale($locale)) {
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $this->argument('locale') on line 35 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...
38
            $this->error("Unsupported locale: '{$locale}'.");
39
            return;
40
        }
41
42
        $this->routes = $this->getFreshApplicationRoutes($locale);
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $this->argument('locale') on line 35 can also be of type array or null; however, RichanFongdasen\I18n\Com...reshApplicationRoutes() 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...
43
44
        $this->displayRoutes($this->getRoutes());
45
    }
46
47
    /**
48
     * Boot a fresh copy of the application and get the routes.
49
     *
50
     * @param string $locale
51
     * @return \Illuminate\Routing\RouteCollection
52
     */
53
    protected function getFreshApplicationRoutes($locale)
54
    {
55
        $app = require $this->getBootstrapPath() . '/app.php';
56
57
        $default = config('i18n.fallback_language');
58
59
        config(['i18n.fallback_language' => $locale]);
60
61
        $app->make(Kernel::class)->bootstrap();
62
63
        config(['i18n.fallback_language' => $default]);
64
65
        return $app['router']->getRoutes();
66
    }
67
68
    /**
69
     * Get the console command arguments.
70
     *
71
     * @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...
72
     */
73
    protected function getArguments()
74
    {
75
        return [
76
            ['locale', InputArgument::REQUIRED, 'The locale to list routes for.'],
77
        ];
78
    }
79
}