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