| Conditions | 22 |
| Paths | 4320 |
| Total Lines | 101 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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; |
||
| 82 | $requirements[] = $requirement; |
||
| 83 | } |
||
| 84 | |||
| 85 | $table->addRow(array( |
||
| 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) { |
||
| 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 | |||
| 158 |