| Conditions | 22 |
| Paths | 3260 |
| Total Lines | 135 |
| Code Lines | 76 |
| Lines | 37 |
| Ratio | 27.41 % |
| 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 |
||
| 158 | public function runMulti( array $reqs ) { |
||
| 159 | foreach ( $reqs as $index => &$req ) { |
||
| 160 | View Code Duplication | if ( isset( $req[0] ) ) { |
|
| 161 | $req['method'] = $req[0]; // short-form |
||
| 162 | unset( $req[0] ); |
||
| 163 | } |
||
| 164 | View Code Duplication | if ( isset( $req[1] ) ) { |
|
| 165 | $req['url'] = $req[1]; // short-form |
||
| 166 | unset( $req[1] ); |
||
| 167 | } |
||
| 168 | $req['chain'] = []; // chain or list of replaced requests |
||
| 169 | } |
||
| 170 | unset( $req ); // don't assign over this by accident |
||
| 171 | |||
| 172 | $curUniqueId = 0; |
||
| 173 | $armoredIndexMap = []; // (original index => new index) |
||
| 174 | |||
| 175 | $doneReqs = []; // (index => request) |
||
| 176 | $executeReqs = []; // (index => request) |
||
| 177 | $replaceReqsByService = []; // (prefix => index => request) |
||
| 178 | $origPending = []; // (index => 1) for original requests |
||
| 179 | |||
| 180 | foreach ( $reqs as $origIndex => $req ) { |
||
| 181 | // Re-index keys to consecutive integers (they will be swapped back later) |
||
| 182 | $index = $curUniqueId++; |
||
| 183 | $armoredIndexMap[$origIndex] = $index; |
||
| 184 | $origPending[$index] = 1; |
||
| 185 | if ( preg_match( '#^(http|ftp)s?://#', $req['url'] ) ) { |
||
| 186 | // Absolute FTP/HTTP(S) URL, run it as normal |
||
| 187 | $executeReqs[$index] = $req; |
||
| 188 | } else { |
||
| 189 | // Must be a virtual HTTP URL; resolve it |
||
| 190 | list( $prefix, $service ) = $this->getMountAndService( $req['url'] ); |
||
| 191 | if ( !$service ) { |
||
| 192 | throw new UnexpectedValueException( "Path '{$req['url']}' has no service." ); |
||
| 193 | } |
||
| 194 | // Set the URL to the mount-relative portion |
||
| 195 | $req['url'] = substr( $req['url'], strlen( $prefix ) ); |
||
| 196 | $replaceReqsByService[$prefix][$index] = $req; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | // Function to get IDs that won't collide with keys in $armoredIndexMap |
||
| 201 | $idFunc = function() use ( &$curUniqueId ) { |
||
| 202 | return $curUniqueId++; |
||
| 203 | }; |
||
| 204 | |||
| 205 | $rounds = 0; |
||
| 206 | do { |
||
| 207 | if ( ++$rounds > 5 ) { // sanity |
||
| 208 | throw new Exception( "Too many replacement rounds detected. Aborting." ); |
||
| 209 | } |
||
| 210 | // Track requests executed this round that have a prefix/service. |
||
| 211 | // Note that this also includes requests where 'response' was forced. |
||
| 212 | $checkReqIndexesByPrefix = []; |
||
| 213 | // Resolve the virtual URLs valid and qualified HTTP(S) URLs |
||
| 214 | // and add any required authentication headers for the backend. |
||
| 215 | // Services can also replace requests with new ones, either to |
||
| 216 | // defer the original or to set a proxy response to the original. |
||
| 217 | $newReplaceReqsByService = []; |
||
| 218 | foreach ( $replaceReqsByService as $prefix => $servReqs ) { |
||
| 219 | $service = $this->instances[$prefix]; |
||
| 220 | foreach ( $service->onRequests( $servReqs, $idFunc ) as $index => $req ) { |
||
| 221 | // Services use unique IDs for replacement requests |
||
| 222 | View Code Duplication | if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) { |
|
|
|
|||
| 223 | // A current or original request which was not modified |
||
| 224 | } else { |
||
| 225 | // Replacement request that will convert to original requests |
||
| 226 | $newReplaceReqsByService[$prefix][$index] = $req; |
||
| 227 | } |
||
| 228 | View Code Duplication | if ( isset( $req['response'] ) ) { |
|
| 229 | // Replacement requests with pre-set responses should not execute |
||
| 230 | unset( $executeReqs[$index] ); |
||
| 231 | unset( $origPending[$index] ); |
||
| 232 | $doneReqs[$index] = $req; |
||
| 233 | } else { |
||
| 234 | // Original or mangled request included |
||
| 235 | $executeReqs[$index] = $req; |
||
| 236 | } |
||
| 237 | $checkReqIndexesByPrefix[$prefix][$index] = 1; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | // Update index of requests to inspect for replacement |
||
| 241 | $replaceReqsByService = $newReplaceReqsByService; |
||
| 242 | // Run the actual work HTTP requests |
||
| 243 | foreach ( $this->http->runMulti( $executeReqs ) as $index => $ranReq ) { |
||
| 244 | $doneReqs[$index] = $ranReq; |
||
| 245 | unset( $origPending[$index] ); |
||
| 246 | } |
||
| 247 | $executeReqs = []; |
||
| 248 | // Services can also replace requests with new ones, either to |
||
| 249 | // defer the original or to set a proxy response to the original. |
||
| 250 | // Any replacement requests executed above will need to be replaced |
||
| 251 | // with new requests (eventually the original). The responses can be |
||
| 252 | // forced by setting 'response' rather than actually be sent over the wire. |
||
| 253 | $newReplaceReqsByService = []; |
||
| 254 | foreach ( $checkReqIndexesByPrefix as $prefix => $servReqIndexes ) { |
||
| 255 | $service = $this->instances[$prefix]; |
||
| 256 | // $doneReqs actually has the requests (with 'response' set) |
||
| 257 | $servReqs = array_intersect_key( $doneReqs, $servReqIndexes ); |
||
| 258 | foreach ( $service->onResponses( $servReqs, $idFunc ) as $index => $req ) { |
||
| 259 | // Services use unique IDs for replacement requests |
||
| 260 | View Code Duplication | if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) { |
|
| 261 | // A current or original request which was not modified |
||
| 262 | } else { |
||
| 263 | // Replacement requests with pre-set responses should not execute |
||
| 264 | $newReplaceReqsByService[$prefix][$index] = $req; |
||
| 265 | } |
||
| 266 | View Code Duplication | if ( isset( $req['response'] ) ) { |
|
| 267 | // Replacement requests with pre-set responses should not execute |
||
| 268 | unset( $origPending[$index] ); |
||
| 269 | $doneReqs[$index] = $req; |
||
| 270 | } else { |
||
| 271 | // Update the request in case it was mangled |
||
| 272 | $executeReqs[$index] = $req; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | // Update index of requests to inspect for replacement |
||
| 277 | $replaceReqsByService = $newReplaceReqsByService; |
||
| 278 | } while ( count( $origPending ) ); |
||
| 279 | |||
| 280 | $responses = []; |
||
| 281 | // Update $reqs to include 'response' and normalized request 'headers'. |
||
| 282 | // This maintains the original order of $reqs. |
||
| 283 | foreach ( $reqs as $origIndex => $req ) { |
||
| 284 | $index = $armoredIndexMap[$origIndex]; |
||
| 285 | if ( !isset( $doneReqs[$index] ) ) { |
||
| 286 | throw new UnexpectedValueException( "Response for request '$index' is NULL." ); |
||
| 287 | } |
||
| 288 | $responses[$origIndex] = $doneReqs[$index]['response']; |
||
| 289 | } |
||
| 290 | |||
| 291 | return $responses; |
||
| 292 | } |
||
| 293 | } |
||
| 294 |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.