Total Complexity | 41 |
Total Lines | 269 |
Duplicated Lines | 0 % |
Changes | 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::parseRequest |
||
58 | * |
||
59 | * Parse server argv to determine requested commander. |
||
60 | * |
||
61 | * @return void |
||
62 | * @throws \ReflectionException |
||
63 | */ |
||
64 | public function parseRequest() |
||
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) |
||
146 | { |
||
147 | static $reflection; |
||
148 | |||
149 | if (empty($reflection)) { |
||
150 | $reflection = new \ReflectionClass($this); |
||
151 | } |
||
152 | |||
153 | foreach ($reflection->getMethods() as $method) { |
||
154 | if (strpos($method->name, 'validateCommands') !== false) { |
||
155 | if ($this->{$method->name}($commands)) { |
||
156 | break; |
||
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() |
||
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 = []) |
||
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:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: