| Conditions | 9 |
| Paths | 10 |
| Total Lines | 77 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 namespace Comodojo\Dispatcher\Router; |
||
| 35 | public function read($folders = array(), &$value = array(), $regex = '') { |
||
| 36 | |||
| 37 | // if the first 'folder' is empty is removed |
||
| 38 | while (!empty($folders) && empty($folders[0])) { |
||
| 39 | |||
| 40 | array_shift($folders); |
||
| 41 | |||
| 42 | } |
||
| 43 | |||
| 44 | // if the 'folder' array is empty, the route has been fully analyzed |
||
| 45 | // this is the exit condition from the recursive loop. |
||
| 46 | if (empty($folders)) { |
||
| 47 | |||
| 48 | return '^'.$regex.'[\/]?$'; |
||
| 49 | |||
| 50 | } else { |
||
| 51 | |||
| 52 | // The first element of the array 'folders' is taken in order to be analyzed |
||
| 53 | $folder = array_shift($folders); |
||
| 54 | |||
| 55 | // All the parameters of the route must be json strings |
||
| 56 | $decoded = json_decode($folder, true); |
||
| 57 | |||
| 58 | if (!is_null($decoded) && is_array($decoded)) { |
||
| 59 | |||
| 60 | $param_regex = ''; |
||
| 61 | |||
| 62 | $param_required = false; |
||
| 63 | |||
| 64 | /* All the folders can include more than one parameter |
||
| 65 | * Eg: /service_name/{'param1': 'regex1', 'param2': 'regex2'}/ |
||
| 66 | * /calendar/{'ux_timestamp*': '\d{10}', 'microseconds': '\d{4}'}/ |
||
| 67 | * |
||
| 68 | * The '*' at the end of the paramerter name implies that the parameter is required |
||
| 69 | * This example can be read as a calendar service that accepts both |
||
| 70 | * timestamps in unix or javascript format. |
||
| 71 | * |
||
| 72 | * This is the reason of the following 'foreach' |
||
| 73 | */ |
||
| 74 | foreach ($decoded as $key => $string) { |
||
| 75 | |||
| 76 | $this->logger->debug("PARAMETER KEY: " . $key); |
||
| 77 | |||
| 78 | $this->logger->debug("PARAMETER STRING: " . $string); |
||
| 79 | |||
| 80 | /* The key and the regex of every paramater is passed to the 'param' |
||
| 81 | * method which will build an appropriate regular expression and will understand |
||
| 82 | * if the parameter is required and will build the $value['query'] object |
||
| 83 | */ |
||
| 84 | $param_regex .= $this->param($key, $string, $param_required, $value); |
||
| 85 | |||
| 86 | $this->logger->debug("PARAMETER REGEX: " . $param_regex); |
||
| 87 | |||
| 88 | } |
||
| 89 | // Once the parameter is analyzed, the result is passed to the next iteration |
||
| 90 | return $this->read( |
||
| 91 | $folders, |
||
| 92 | $value, |
||
| 93 | $regex.'(?:\/'.$param_regex.')'. (($param_required)?'{1}':'?') |
||
| 94 | ); |
||
| 95 | |||
| 96 | } else { |
||
| 97 | // if the element is not a json string, I assume it's the service name |
||
| 98 | if (!isset($value['service'])) $value['service'] = array(); |
||
| 99 | array_push($value['service'], $folder); |
||
| 100 | |||
| 101 | return $this->read( |
||
| 102 | $folders, |
||
| 103 | $value, |
||
| 104 | $regex.'\/'.$folder |
||
| 105 | ); |
||
| 106 | |||
| 107 | } |
||
| 108 | |||
| 109 | } |
||
| 110 | |||
| 111 | } |
||
| 112 | |||
| 163 |