| Total Complexity | 41 |
| Total Lines | 269 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 0 |
Complex classes like Router often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Router, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Router |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Router::$string |
||
| 29 | * |
||
| 30 | * Router request string. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $string; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Router::$commands |
||
| 38 | * |
||
| 39 | * Router request commands. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $commands = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Router::$commander |
||
| 47 | * |
||
| 48 | * Router request commander. |
||
| 49 | * |
||
| 50 | * @var Commander |
||
| 51 | */ |
||
| 52 | protected $commander; |
||
| 53 | |||
| 54 | // ----------------------------------------------------------------------- |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Router::handle |
||
| 58 | * |
||
| 59 | * Parse server argv to determine requested commander. |
||
| 60 | * |
||
| 61 | * @return void |
||
| 62 | * @throws \ReflectionException |
||
| 63 | */ |
||
| 64 | public function handle() |
||
| 65 | { |
||
| 66 | $argv = $_SERVER[ 'argv' ]; |
||
| 67 | |||
| 68 | if ($_SERVER[ 'SCRIPT_NAME' ] === $_SERVER[ 'argv' ][ 0 ]) { |
||
| 69 | array_shift($argv); |
||
| 70 | |||
| 71 | if (empty($argv)) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->string = str_replace(['/', '\\', ':'], '/', $argv[ 0 ]); |
||
| 77 | $this->commands = explode('/', $this->string); |
||
| 78 | |||
| 79 | if (strpos($this->commands[ 0 ], '--') !== false |
||
| 80 | || strpos($this->commands[ 0 ], '-') !== false |
||
| 81 | ) { |
||
| 82 | $options = $this->commands; |
||
| 83 | $this->commands = []; |
||
| 84 | } else { |
||
| 85 | $options = array_slice($argv, 1); |
||
| 86 | } |
||
| 87 | |||
| 88 | foreach ($options as $option) { |
||
| 89 | if (strpos($option, '--') !== false |
||
| 90 | || strpos($option, '-') !== false |
||
| 91 | ) { |
||
| 92 | $option = str_replace(['-', '--'], '', $option); |
||
| 93 | $option = str_replace(':', '=', $option); |
||
| 94 | $option = str_replace('"', '', $option); |
||
| 95 | $value = null; |
||
| 96 | |||
| 97 | if (strpos($option, '=') !== false) { |
||
| 98 | $optionParts = explode('=', $option); |
||
| 99 | $option = $optionParts[ 0 ]; |
||
| 100 | $value = $optionParts[ 1 ]; |
||
| 101 | } else { |
||
| 102 | $value = current($options); |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($value === 'true') { |
||
| 106 | $value = true; |
||
| 107 | } elseif ($value === 'false') { |
||
| 108 | $value = false; |
||
| 109 | } |
||
| 110 | |||
| 111 | if (strpos($value, '--') === false |
||
| 112 | || strpos($value, '-') === false |
||
| 113 | ) { |
||
| 114 | $_GET[ $option ] = $value; |
||
| 115 | } else { |
||
| 116 | $_GET[ $option ] = null; |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | $keys = array_keys($_GET); |
||
| 120 | if (count($keys)) { |
||
| 121 | $key = end($keys); |
||
| 122 | $_GET[ $key ] = $option; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | if (array_key_exists('verbose', $_GET) or array_key_exists('v', $_GET)) { |
||
| 128 | $_ENV[ 'VERBOSE' ] = true; |
||
| 129 | } |
||
| 130 | |||
| 131 | $this->parseCommands($this->commands); |
||
| 132 | } |
||
| 133 | |||
| 134 | // ------------------------------------------------------------------------ |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Router::parseSegments |
||
| 138 | * |
||
| 139 | * Parse and validate requested commands. |
||
| 140 | * |
||
| 141 | * @param array $segments |
||
| 142 | * |
||
| 143 | * @throws \ReflectionException |
||
| 144 | */ |
||
| 145 | final private function parseCommands(array $commands) |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | // ------------------------------------------------------------------------ |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Router::getCommander |
||
| 166 | * |
||
| 167 | * Gets requested commander. |
||
| 168 | * |
||
| 169 | * @return \O2System\Kernel\Cli\Router\DataStructures\Commander |
||
| 170 | */ |
||
| 171 | public function getCommander() |
||
| 172 | { |
||
| 173 | return $this->commander; |
||
| 174 | } |
||
| 175 | |||
| 176 | // ------------------------------------------------------------------------ |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Router::setCommander |
||
| 180 | * |
||
| 181 | * Sets requested commander. |
||
| 182 | * |
||
| 183 | * @param \O2System\Kernel\Cli\Router\DataStructures\Commander $commander |
||
| 184 | * @param array $uriSegments |
||
| 185 | * |
||
| 186 | * @throws \ReflectionException |
||
| 187 | */ |
||
| 188 | final protected function setCommander(Router\DataStructures\Commander $commander, array $uriSegments = []) |
||
| 189 | { |
||
| 190 | // Add Commander PSR4 Namespace |
||
| 191 | loader()->addNamespace($commander->getNamespaceName(), $commander->getFileInfo()->getPath()); |
||
| 192 | |||
| 193 | $commanderMethod = camelcase(reset($uriSegments)); |
||
| 194 | $commanderMethodParams = array_slice($uriSegments, 1); |
||
| 195 | |||
| 196 | if (null !== $commander->getRequestMethod()) { |
||
| 197 | $commander->setRequestMethodArgs($commanderMethodParams); |
||
| 198 | } elseif (count($uriSegments)) { |
||
| 199 | if ($commander->hasMethod('route')) { |
||
| 200 | $commander |
||
| 201 | ->setRequestMethod('route') |
||
| 202 | ->setRequestMethodArgs( |
||
| 203 | [ |
||
| 204 | $commanderMethod, |
||
| 205 | $commanderMethodParams, |
||
| 206 | ] |
||
| 207 | ); |
||
| 208 | } elseif ($commander->hasMethod($commanderMethod)) { |
||
| 209 | $method = $commander->getMethod($commanderMethod); |
||
| 210 | |||
| 211 | if ($method->isPublic()) { |
||
| 212 | $commander |
||
| 213 | ->setRequestMethod($commanderMethod) |
||
| 214 | ->setRequestMethodArgs($commanderMethodParams); |
||
| 215 | } elseif (is_ajax() AND $method->isProtected()) { |
||
| 216 | $commander |
||
| 217 | ->setRequestMethod($commanderMethod) |
||
| 218 | ->setRequestMethodArgs($commanderMethodParams); |
||
| 219 | } |
||
| 220 | } elseif ($commander->hasMethod('execute')) { |
||
| 221 | $execute = $commander->getMethod('execute'); |
||
| 222 | |||
| 223 | if ($execute->getNumberOfParameters() > 0) { |
||
| 224 | |||
| 225 | array_unshift($commanderMethodParams, $commanderMethod); |
||
| 226 | |||
| 227 | $commander |
||
| 228 | ->setRequestMethod('execute') |
||
| 229 | ->setRequestMethodArgs($commanderMethodParams); |
||
| 230 | } else { |
||
| 231 | output()->sendError(404); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } elseif ($commander->hasMethod('route')) { |
||
| 235 | $commander |
||
| 236 | ->setRequestMethod('route') |
||
| 237 | ->setRequestMethodArgs(['execute', []]); |
||
| 238 | } elseif ($commander->hasMethod('execute')) { |
||
| 239 | $commander |
||
| 240 | ->setRequestMethod('execute'); |
||
| 241 | } |
||
| 242 | |||
| 243 | // Set Router Commander |
||
| 244 | $this->commander = $commander; |
||
| 245 | } |
||
| 246 | |||
| 247 | // ------------------------------------------------------------------------ |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Router::validateCommandsCommander |
||
| 251 | * |
||
| 252 | * @param array $segments |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | * @throws \ReflectionException |
||
| 256 | */ |
||
| 257 | final private function validateCommandsCommander(array $segments) |
||
| 294 | } |
||
| 295 | } |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: