RouterCommand::execute()   F
last analyzed

Complexity

Conditions 22
Paths 4320

Size

Total Lines 101
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 506

Importance

Changes 0
Metric Value
cc 22
eloc 62
nc 4320
nop 2
dl 0
loc 101
ccs 0
cts 62
cp 0
crap 506
rs 2
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\Application;
27
use Symfony\Component\Console\Input\InputArgument;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Output\OutputInterface;
31
use Symfony\Component\Console\Helper\TableHelper;
32
33
34
class RouterCommand extends \Knp\Command\Command
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
37
    protected $app;
38
39
    protected function configure() {
40
        $this
41
            ->setName('router:debug')
42
            ->setDefinition(array(
43
                new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
44
            ))
45
            ->addOption('sort', null, InputOption::VALUE_OPTIONAL, '[null/ASC/DESC]. If argument orderby set, Default is ASC.')
46
            ->addOption('orderby', null, InputOption::VALUE_OPTIONAL, '[null/name/path]. If argument sort set, Default is name.')
47
            ->setDescription('Displays current routes for an application')
48
            ->setHelp(<<<EOF
49
The <info>%command.name%</info> displays the configured routes:
50
  <info>php %command.full_name%</info>
51
EOF
52
            );
53
    }
54
55
    protected function execute(InputInterface $input, OutputInterface $output) {
56
57
        $this->app = $this->getSilexApplication();
58
        $this->app->initialize();
59
        $this->app->boot();
60
61
        $console = new Application();
62
63
        $filtername = $input->getArgument('name');
64
        $sort = $input->getOption('sort');
65
        $orderby = $input->getOption('orderby');
66
67
        $table = $console->getHelperSet()->get('table');
68
        $table->setHeaders(array('Name', 'Path', 'Pattern'));
69
        $table->setLayout(TableHelper::LAYOUT_DEFAULT);
70
71
        $controllers    = $this->app['controllers'];
72
        $collection     = $controllers->flush();
73
74
        foreach ($collection as $name => $route) {
75
            if (!empty($filtername) && !preg_match("/$filtername/", $name)) {
76
                continue;
77
            }
78
79
            $requirements = array();
80
            foreach ($route->getRequirements() as $key => $requirement) {
81
                // $requirements[] = $key . ' => ' . $requirement;
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
                $requirements[] = $requirement;
83
            }
84
85
            $table->addRow(array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
86
                $name,
87
                $route->getPath(),
88
                join(', ', $requirements)
89
            ));
90
        }
91
92
93
        $routes = $this->app['routes']->all();
94
95
        // 引数で並び替える。
96
        if (!empty($sort)) {
97
            $orderby = (!empty($orderby)) ? $orderby : "name";
98
        }
99
        if (!empty($orderby)) {
100
            $sort = (!empty($sort)) ? $sort : "ASC";
101
        }
102
103
        if (strtoupper($orderby) === "NAME") {
104
            if (strtoupper($sort) === "DESC") {
105
                krsort($routes);
106
            } else {
107
                ksort($routes);
108
            }
109
        } else if (strtoupper($orderby) === "PATH") {
110
            uasort($routes, function($a, $b) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
111
                return strcmp($a->getPattern(), $b->getPattern());
112
            });
113
        }
114
115
        $maxName = 4;
116
        $maxMethod = 6;
117
        foreach ($routes as $name => $route) {
118
            if (!empty($filtername) && !preg_match("/$filtername/", $name)) {
119
                unset($routes[$name]);
120
                continue;
121
            }
122
123
            $requirements = $route->getRequirements();
124
            $method = isset($requirements['_method'])
125
                ? strtoupper(is_array($requirements['_method'])
126
                    ? implode(', ', $requirements['_method']) : $requirements['_method']
127
                )
128
                : 'ANY';
129
130
            if (strlen($name) > $maxName) {
131
                $maxName = strlen($name);
132
            }
133
134
            if (strlen($method) > $maxMethod) {
135
                $maxMethod = strlen($method);
136
            }
137
        }
138
139
        foreach ($routes as $name => $route) {
140
            $requirements = $route->getRequirements();
141
            $method = isset($requirements['_method'])
142
                ? strtoupper(is_array($requirements['_method'])
143
                    ? implode(', ', $requirements['_method']) : $requirements['_method']
144
                )
145
                : 'ANY';
146
147
            $table->addRow(array(
148
                $name,
149
                $route->getPattern(),
150
                $method,
151
            ));
152
        }
153
154
        $table->render($output);
155
    }
156
157
}
158