| Conditions | 29 |
| Paths | 542 |
| Total Lines | 155 |
| Code Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 97 |
| CRAP Score | 29.0009 |
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 |
||
| 56 | 34 | public function dispatch(Request $request, array &$args) { |
|
| 57 | 34 | $controller = new $args['controller'](); |
|
| 58 | 34 | $method = strtolower($args['method']); |
|
| 59 | 34 | $pathArgs = $args['pathArgs']; |
|
| 60 | |||
| 61 | // See if the initialize method can take any of the parameters. |
||
| 62 | 34 | $initialize = false; |
|
| 63 | 34 | $initArgs = []; |
|
| 64 | 34 | $initParams = []; |
|
| 65 | 34 | $actionIndex = 0; |
|
| 66 | 34 | if (method_exists($controller, 'initialize')) { |
|
| 67 | 28 | $initialize = true; |
|
| 68 | 28 | $initMethod = new \ReflectionMethod($controller, 'initialize'); |
|
| 69 | |||
| 70 | // Walk through the initialize() arguments and supply all of the ones that are required. |
||
| 71 | 28 | foreach ($initMethod->getParameters() as $initParam) { |
|
| 72 | 28 | $i = $initParam->getPosition(); |
|
| 73 | 28 | $initArgs[$i] = null; |
|
| 74 | 28 | $initParams[$i] = $initParam->getName(); |
|
| 75 | |||
| 76 | 28 | if ($initParam->isDefaultValueAvailable()) { |
|
| 77 | 23 | $initArgs[$i] = $initParam->getDefaultValue(); |
|
| 78 | 28 | } elseif (!isset($pathArgs[$i])) { |
|
| 79 | 1 | throw new NotFoundException('Page', "Missing argument $i for {$args['controller']}::initialize()."); |
|
| 80 | 4 | } elseif ($this->failsCondition($initParams[$i], $pathArgs[$i])) { |
|
| 81 | // This isn't a valid value for a required parameter. |
||
| 82 | 1 | throw new NotFoundException('Page', "Invalid argument '{$pathArgs[$i]}' for {$initParams[$i]}."); |
|
| 83 | } else { |
||
| 84 | 3 | $initArgs[$i] = $pathArgs[$i]; |
|
| 85 | 3 | $actionIndex = $i + 1; |
|
| 86 | } |
||
| 87 | 26 | } |
|
| 88 | 26 | } |
|
| 89 | |||
| 90 | 32 | $action = ''; |
|
| 91 | 32 | $initComplete = false; |
|
| 92 | 32 | for ($i = $actionIndex; $i < count($pathArgs); $i++) { |
|
|
|
|||
| 93 | 22 | $pathArg = $pathArgs[$i]; |
|
| 94 | |||
| 95 | 22 | $action = $this->actionExists($controller, $pathArg, $method, true); |
|
| 96 | 22 | if ($action) { |
|
| 97 | // We found an action and can move on to the next step. |
||
| 98 | 4 | $actionIndex = $i + 1; |
|
| 99 | 4 | break; |
|
| 100 | } else { |
||
| 101 | // This is a method argument. See whether to add it to the initialize method or not. |
||
| 102 | 18 | if (!$initComplete && $actionIndex < count($initArgs)) { |
|
| 103 | // Make sure the argument is valid. |
||
| 104 | 14 | if ($this->failsCondition($initParams[$actionIndex], $pathArg)) { |
|
| 105 | // The argument doesn't validate against its condition so this can't be an init argument. |
||
| 106 | 3 | $initComplete = true; |
|
| 107 | 3 | } else { |
|
| 108 | // The argument can be added to initialize(). |
||
| 109 | 11 | $initArgs[$actionIndex] = $pathArg; |
|
| 110 | 11 | $actionIndex++; |
|
| 111 | } |
||
| 112 | 14 | } |
|
| 113 | } |
||
| 114 | 18 | } |
|
| 115 | |||
| 116 | 32 | if (!$action) { |
|
| 117 | // There is no specific action at this point so we have to check for a resource action. |
||
| 118 | 28 | if ($actionIndex === 0) { |
|
| 119 | 14 | $actions = ['get' => 'index', 'post' => 'post', 'options' => 'options']; |
|
| 120 | 14 | } else { |
|
| 121 | 14 | $actions = ['get' => 'get', 'patch' => 'patch', 'put' => 'put', 'delete' => 'delete']; |
|
| 122 | } |
||
| 123 | |||
| 124 | 28 | if (!isset($actions[$method])) { |
|
| 125 | 5 | if ($actionIndex < count($pathArgs)) { |
|
| 126 | // There are more path args left to go then just throw a 404. |
||
| 127 | 2 | throw new NotFoundException(); |
|
| 128 | } else { |
||
| 129 | // The http method isn't allowed. |
||
| 130 | 3 | $allowed = array_keys($actions); |
|
| 131 | 3 | throw new MethodNotAllowedException($method, $allowed); |
|
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | 23 | $action = $actions[$method]; |
|
| 136 | 23 | if (!$this->actionExists($controller, $action)) { |
|
| 137 | // If there are more path args left to go then just throw a 404. |
||
| 138 | 4 | if ($actionIndex < count($pathArgs)) { |
|
| 139 | 1 | throw new NotFoundException(); |
|
| 140 | } |
||
| 141 | |||
| 142 | // Check to see what actions are allowed. |
||
| 143 | 3 | unset($actions[$method]); |
|
| 144 | 3 | $allowed = []; |
|
| 145 | 3 | foreach ($actions as $otherMethod => $otherAction) { |
|
| 146 | 3 | if ($this->actionExists($controller, $otherAction)) { |
|
| 147 | 3 | $allowed[] = strtoupper($otherMethod); |
|
| 148 | 3 | } |
|
| 149 | 3 | } |
|
| 150 | |||
| 151 | 3 | if (!empty($allowed)) { |
|
| 152 | // Other methods are allowed. Show them. |
||
| 153 | 3 | throw new MethodNotAllowedException($method, $allowed); |
|
| 154 | } else { |
||
| 155 | // The action does not exist at all. |
||
| 156 | throw new NotFoundException(); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | 19 | } |
|
| 160 | |||
| 161 | // Make sure the number of action arguments match the action method. |
||
| 162 | 23 | $actionMethod = new \ReflectionMethod($controller, $action); |
|
| 163 | 23 | $action = $actionMethod->getName(); // make correct case. |
|
| 164 | 23 | $actionParams = $actionMethod->getParameters(); |
|
| 165 | 23 | $actionArgs = array_slice($pathArgs, $actionIndex); |
|
| 166 | |||
| 167 | 23 | if (count($actionArgs) > count($actionParams)) { |
|
| 168 | // Double check to see if the first argument might be a method, but one that isn't allowed. |
||
| 169 | 6 | $allowed = $this->allowedMethods($controller, $actionArgs[0]); |
|
| 170 | 6 | if (count($allowed) > 0) { |
|
| 171 | // At least one method was allowed for this action so throw an exception. |
||
| 172 | 1 | throw new MethodNotAllowedException($method, $allowed); |
|
| 173 | } |
||
| 174 | |||
| 175 | // Too many arguments were passed. |
||
| 176 | 5 | throw new NotFoundException(); |
|
| 177 | } |
||
| 178 | // Fill in missing default parameters. |
||
| 179 | 17 | foreach ($actionParams as $param) { |
|
| 180 | 10 | $i = $param->getPosition(); |
|
| 181 | 10 | $paramName = $param->getName(); |
|
| 182 | |||
| 183 | 10 | if ($this->isMapped($paramName)) { |
|
| 184 | // The parameter is mapped to a specific request item. |
||
| 185 | 2 | array_splice($actionArgs, $i, 0, [$this->mappedData($paramName, $request)]); |
|
| 186 | 10 | } elseif (!isset($actionArgs[$i]) || !$actionArgs[$i]) { |
|
| 187 | 4 | if ($param->isDefaultValueAvailable()) { |
|
| 188 | 3 | $actionArgs[$i] = $param->getDefaultValue(); |
|
| 189 | 3 | } else { |
|
| 190 | 1 | throw new NotFoundException('Page', "Missing argument $i for {$args['controller']}::$action()."); |
|
| 191 | } |
||
| 192 | 7 | } elseif ($this->failsCondition($paramName, $actionArgs[$i])) { |
|
| 193 | 1 | throw new NotFoundException('Page', "Invalid argument '{$actionArgs[$i]}' for {$paramName}."); |
|
| 194 | } |
||
| 195 | 15 | } |
|
| 196 | |||
| 197 | 15 | $args = array_replace($args, [ |
|
| 198 | 15 | 'init' => $initialize, |
|
| 199 | 15 | 'initArgs' => $initArgs, |
|
| 200 | 15 | 'action' => $action, |
|
| 201 | 'actionArgs' => $actionArgs |
||
| 202 | 15 | ]); |
|
| 203 | |||
| 204 | 15 | if ($initialize) { |
|
| 205 | 15 | Event::callUserFuncArray([$controller, 'initialize'], $initArgs); |
|
| 206 | 15 | } |
|
| 207 | |||
| 208 | 15 | $result = Event::callUserFuncArray([$controller, $action], $actionArgs); |
|
| 209 | 15 | return $result; |
|
| 210 | } |
||
| 211 | |||
| 382 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: