Conditions | 17 |
Paths | 241 |
Total Lines | 69 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 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 |
||
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 | if( $this->parseCommands($this->commands) === false ){ |
||
132 | output()->sendError(404); |
||
133 | } |
||
233 | } |
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: