| Conditions | 14 |
| Paths | 13 |
| Total Lines | 69 |
| Code Lines | 53 |
| Lines | 12 |
| Ratio | 17.39 % |
| 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 |
||
| 130 | public static function constructClassInstance( $clazz, $args ) { |
||
| 131 | // $args should be a non-associative array; show nice error if that's not the case |
||
| 132 | if ( $args && array_keys( $args ) !== range( 0, count( $args ) - 1 ) ) { |
||
| 133 | throw new InvalidArgumentException( __METHOD__ . ': $args cannot be an associative array' ); |
||
| 134 | } |
||
| 135 | |||
| 136 | // TODO: when PHP min version supported is >=5.6.0 replace this |
||
| 137 | // with `return new $clazz( ... $args );`. |
||
| 138 | $obj = null; |
||
|
|
|||
| 139 | switch ( count( $args ) ) { |
||
| 140 | case 0: |
||
| 141 | $obj = new $clazz(); |
||
| 142 | break; |
||
| 143 | case 1: |
||
| 144 | $obj = new $clazz( $args[0] ); |
||
| 145 | break; |
||
| 146 | case 2: |
||
| 147 | $obj = new $clazz( $args[0], $args[1] ); |
||
| 148 | break; |
||
| 149 | case 3: |
||
| 150 | $obj = new $clazz( $args[0], $args[1], $args[2] ); |
||
| 151 | break; |
||
| 152 | case 4: |
||
| 153 | $obj = new $clazz( $args[0], $args[1], $args[2], $args[3] ); |
||
| 154 | break; |
||
| 155 | case 5: |
||
| 156 | $obj = new $clazz( |
||
| 157 | $args[0], $args[1], $args[2], $args[3], $args[4] |
||
| 158 | ); |
||
| 159 | break; |
||
| 160 | case 6: |
||
| 161 | $obj = new $clazz( |
||
| 162 | $args[0], $args[1], $args[2], $args[3], $args[4], |
||
| 163 | $args[5] |
||
| 164 | ); |
||
| 165 | break; |
||
| 166 | case 7: |
||
| 167 | $obj = new $clazz( |
||
| 168 | $args[0], $args[1], $args[2], $args[3], $args[4], |
||
| 169 | $args[5], $args[6] |
||
| 170 | ); |
||
| 171 | break; |
||
| 172 | case 8: |
||
| 173 | $obj = new $clazz( |
||
| 174 | $args[0], $args[1], $args[2], $args[3], $args[4], |
||
| 175 | $args[5], $args[6], $args[7] |
||
| 176 | ); |
||
| 177 | break; |
||
| 178 | View Code Duplication | case 9: |
|
| 179 | $obj = new $clazz( |
||
| 180 | $args[0], $args[1], $args[2], $args[3], $args[4], |
||
| 181 | $args[5], $args[6], $args[7], $args[8] |
||
| 182 | ); |
||
| 183 | break; |
||
| 184 | View Code Duplication | case 10: |
|
| 185 | $obj = new $clazz( |
||
| 186 | $args[0], $args[1], $args[2], $args[3], $args[4], |
||
| 187 | $args[5], $args[6], $args[7], $args[8], $args[9] |
||
| 188 | ); |
||
| 189 | break; |
||
| 190 | default: |
||
| 191 | // Fall back to using ReflectionClass and curse the developer |
||
| 192 | // who decided that 11+ args was a reasonable method |
||
| 193 | // signature. |
||
| 194 | $ref = new ReflectionClass( $clazz ); |
||
| 195 | $obj = $ref->newInstanceArgs( $args ); |
||
| 196 | } |
||
| 197 | return $obj; |
||
| 198 | } |
||
| 199 | } |
||
| 200 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.