Failed Conditions
Push — experimental/3.1 ( cb54a4...028283 )
by Kiyotaka
44:57
created

RouterCommand::execute()   D

Complexity

Conditions 13
Paths 324

Size

Total Lines 67
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 40
nc 324
nop 2
dl 0
loc 67
ccs 0
cts 41
cp 0
crap 182
rs 4.3252
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Command;
25
26
use Symfony\Component\Console\Helper\Table;
27
use Symfony\Component\Console\Input\InputArgument;
28
use Symfony\Component\Console\Input\InputInterface;
29
use Symfony\Component\Console\Input\InputOption;
30
use Symfony\Component\Console\Output\OutputInterface;
31
use Symfony\Component\Routing\Route;
32
33
34
class RouterCommand extends \Knp\Command\Command
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
    protected function configure()
37
    {
38
        $this
39
            ->setName('router:debug')
40
            ->setDefinition(
41
                [
42
                    new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
43
                ]
44
            )
45
            ->addOption(
46
                'sort',
47
                null,
48
                InputOption::VALUE_OPTIONAL,
49
                '[null/ASC/DESC]. If argument orderby set, Default is ASC.'
50
            )
51
            ->addOption(
52
                'orderby',
53
                null,
54
                InputOption::VALUE_OPTIONAL,
55
                '[null/name/path]. If argument sort set, Default is name.'
56
            )
57
            ->setDescription('Displays current routes for an application')
58
            ->setHelp(
59
                <<<EOF
60
The <info>%command.name%</info> displays the configured routes:
61
<info>php %command.full_name%</info>
62
EOF
63
            );
64
    }
65
66
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68
        $app = $this->getSilexApplication();
69
70
        $filter = $input->getArgument('name');
71
        $sort = $input->getOption('sort');
72
        $orderby = $input->getOption('orderby');
73
74
        $table = new Table($output);
75
        $table->setHeaders(['Name', 'Path', 'Method', 'Controller']);
76
77
        $routes = $app['routes']->all();
78
79
        // 引数で並び替える。
80
        if (!empty($sort)) {
81
            $orderby = !empty($orderby) ? $orderby : "name";
82
        }
83
        if (!empty($orderby)) {
84
            $sort = !empty($sort) ? $sort : "ASC";
85
        }
86
87
        if (strtoupper($orderby) === "NAME") {
88
            if (strtoupper($sort) === "DESC") {
89
                krsort($routes);
90
            } else {
91
                ksort($routes);
92
            }
93
        } else {
94
            if (strtoupper($orderby) === "PATH") {
95
                uasort(
96
                    $routes,
97
                    function ($a, $b) {
98
                        return strcmp($a->getPath(), $b->getPath());
99
                    }
100
                );
101
            }
102
        }
103
104
        // filterで指定した条件以外を除外
105
        foreach ($routes as $name => $route) {
106
            if (!empty($filter) && !preg_match("/$filter/", $name)) {
107
                unset($routes[$name]);
108
                continue;
109
            }
110
        }
111
112
        foreach ($routes as $name => $route) {
113
            /** @var Route $route */
114
            $path = $route->getPath();
115
            $methods = $route->getMethods();
116
            $methods = empty($methods)
117
                ? 'ANY'
118
                : implode(',', $methods);
119
            $controller = $route->getDefault('_controller');
120
121
            $table->addRow(
122
                [
123
                    $name,
124
                    $path,
125
                    $methods,
126
                    $controller,
127
                ]
128
            );
129
        }
130
131
        $table->render($output);
0 ignored issues
show
Unused Code introduced by
The call to Table::render() has too many arguments starting with $output.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
132
    }
133
134
}
135