| Conditions | 8 |
| Paths | 12 |
| Total Lines | 58 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 12 | public static function cleanEnvironment(array $variables) |
||
| 13 | { |
||
| 14 | // Create all blank vars |
||
| 15 | foreach (['_REQUEST', '_GET', '_POST', '_SESSION', '_SERVER', '_COOKIE', '_ENV', '_FILES'] as $key) { |
||
| 16 | if (!isset($variables[$key])) { |
||
| 17 | $variables[$key] = []; |
||
| 18 | }; |
||
| 19 | } |
||
| 20 | |||
| 21 | // We update the $_SERVER variable to contain data consistent with the rest of the application. |
||
| 22 | $variables['_SERVER'] = array_merge(array( |
||
| 23 | 'SERVER_PROTOCOL' => 'HTTP/1.1', |
||
| 24 | 'HTTP_ACCEPT' => 'text/plain;q=0.5', |
||
| 25 | 'HTTP_ACCEPT_LANGUAGE' => '*;q=0.5', |
||
| 26 | 'HTTP_ACCEPT_ENCODING' => '', |
||
| 27 | 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1;q=0.5', |
||
| 28 | 'SERVER_SIGNATURE' => 'Command-line PHP/' . phpversion(), |
||
| 29 | 'SERVER_SOFTWARE' => 'PHP/' . phpversion(), |
||
| 30 | 'SERVER_ADDR' => '127.0.0.1', |
||
| 31 | 'REMOTE_ADDR' => '127.0.0.1', |
||
| 32 | 'REQUEST_METHOD' => 'GET', |
||
| 33 | 'HTTP_USER_AGENT' => 'CLI', |
||
| 34 | ), $variables['_SERVER']); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Process arguments and load them into the $_GET and $_REQUEST arrays |
||
| 38 | * For example, |
||
| 39 | * sake my/url somearg otherarg key=val --otherkey=val third=val&fourth=val |
||
| 40 | * |
||
| 41 | * Will result in the following get data: |
||
| 42 | * args => array('somearg', 'otherarg'), |
||
| 43 | * key => val |
||
| 44 | * otherkey => val |
||
| 45 | * third => val |
||
| 46 | * fourth => val |
||
| 47 | */ |
||
| 48 | if (isset($variables['_SERVER']['argv'][2])) { |
||
| 49 | $args = array_slice($variables['_SERVER']['argv'], 2); |
||
| 50 | foreach ($args as $arg) { |
||
| 51 | if (strpos($arg, '=') == false) { |
||
|
|
|||
| 52 | $variables['_GET']['args'][] = $arg; |
||
| 53 | } else { |
||
| 54 | $newItems = array(); |
||
| 55 | parse_str((substr($arg, 0, 2) == '--') ? substr($arg, 2) : $arg, $newItems); |
||
| 56 | $variables['_GET'] = array_merge($variables['_GET'], $newItems); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | $_REQUEST = array_merge($_REQUEST, $variables['_GET']); |
||
| 60 | } |
||
| 61 | |||
| 62 | // Set 'url' GET parameter |
||
| 63 | if (isset($variables['_SERVER']['argv'][1])) { |
||
| 64 | $variables['_GET']['url'] = $variables['_SERVER']['argv'][1]; |
||
| 65 | $variables['_SERVER']['REQUEST_URI'] = $variables['_SERVER']['argv'][1]; |
||
| 66 | } |
||
| 67 | |||
| 68 | // Parse rest of variables as standard |
||
| 69 | return parent::cleanEnvironment($variables); |
||
| 70 | } |
||
| 87 |