| Total Complexity | 64 |
| Total Lines | 207 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ControllerParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ControllerParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ControllerParser { |
||
| 17 | use ControllerParserPathTrait; |
||
| 18 | |||
| 19 | private $controllerClass; |
||
| 20 | private $mainRouteClass; |
||
| 21 | private $routesMethods=[ ]; |
||
| 22 | private $rest=false; |
||
| 23 | private static $excludeds=[ "__construct","isValid","initialize","finalize","onInvalidControl","loadView","forward","redirectToRoute" ]; |
||
| 24 | |||
| 25 | public function parse($controllerClass) { |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | private function parseMethods($methods,$controllerClass,$inherited,$automated){ |
||
| 67 | //When controllerClass generates an exception |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | private function parseAnnot(&$annot,$method){ |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | private function generateRouteAnnotationFromMethod(\ReflectionMethod $method) { |
||
| 86 | } |
||
| 87 | |||
| 88 | public function asArray() { |
||
| 89 | $result=[ ]; |
||
| 90 | $prefix=""; |
||
| 91 | $httpMethods=false; |
||
| 92 | if ($this->mainRouteClass) { |
||
| 93 | if (isset($this->mainRouteClass->path)) |
||
| 94 | $prefix=$this->mainRouteClass->path; |
||
| 95 | if (isset($this->mainRouteClass->methods)) { |
||
| 96 | $httpMethods=$this->mainRouteClass->methods; |
||
| 97 | if ($httpMethods !== null) { |
||
| 98 | if (\is_string($httpMethods)) |
||
| 99 | $httpMethods=[ $httpMethods ]; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | foreach ( $this->routesMethods as $method => $arrayAnnotsMethod ) { |
||
| 104 | $routeAnnotations=$arrayAnnotsMethod["annotations"]; |
||
| 105 | |||
| 106 | foreach ( $routeAnnotations as $routeAnnotation ) { |
||
| 107 | $params=[ "path" => $routeAnnotation->path,"methods" => $routeAnnotation->methods,"name" => $routeAnnotation->name,"cache" => $routeAnnotation->cache,"duration" => $routeAnnotation->duration,"requirements" => $routeAnnotation->requirements,"priority"=>$routeAnnotation->priority ]; |
||
| 108 | self::parseRouteArray($result, $this->controllerClass, $params, $arrayAnnotsMethod["method"], $method, $prefix, $httpMethods); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | uasort($result, function ($item1, $item2) { |
||
| 112 | return UArray::getRecursive($item2,"priority",0) <=> UArray::getRecursive($item1,"priority",0); |
||
| 113 | }); |
||
| 114 | UArray::removeRecursive($result,"priority"); |
||
| 115 | self::minifyRoutes($result); |
||
| 116 | return $result; |
||
| 117 | } |
||
| 118 | |||
| 119 | public static function minifyRoutes(&$routes){ |
||
| 120 | foreach ($routes as &$route){ |
||
| 121 | self::minifyRoute($route); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | private static function minifyRoute(&$route){ |
||
| 126 | if(isset($route['name']) && !is_string($route['name'])){ |
||
| 127 | unset($route['name']); |
||
| 128 | } |
||
| 129 | if(isset($route['parameters']) && sizeof($route['parameters'])==0){ |
||
| 130 | unset($route['parameters']); |
||
| 131 | } |
||
| 132 | if((isset($route['cache']) && $route['cache']==false) || (array_key_exists('cache', $route) && $route['cache']==null)){ |
||
| 133 | unset($route['cache']); |
||
| 134 | unset($route['duration']); |
||
| 135 | } |
||
| 136 | if((isset($route['duration']) && !is_numeric($route['duration'])) || (array_key_exists('duration', $route) && $route['duration']==null)){ |
||
| 137 | unset($route['duration']); |
||
| 138 | } |
||
| 139 | if(isset($route['priority'])){ |
||
| 140 | unset($route['priority']); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | public static function parseRouteArray(&$result, $controllerClass, $routeArray, \ReflectionMethod $method, $methodName, $prefix="", $httpMethods=NULL) { |
||
| 145 | if (!isset($routeArray["path"])) { |
||
| 146 | $routeArray["path"]=self::getPathFromMethod($method); |
||
| 147 | } |
||
| 148 | $pathParameters=self::addParamsPath($routeArray["path"], $method, $routeArray["requirements"]); |
||
| 149 | $name=$routeArray["name"]; |
||
| 150 | if (!isset($name)) { |
||
| 151 | $name=UString::cleanAttribute(ClassUtils::getClassSimpleName($controllerClass) . "_" . $methodName); |
||
| 152 | } |
||
| 153 | $cache=$routeArray["cache"]; |
||
| 154 | $duration=$routeArray["duration"]; |
||
| 155 | $path=$pathParameters["path"]; |
||
| 156 | $parameters=$pathParameters["parameters"]; |
||
| 157 | $priority=$routeArray["priority"]; |
||
| 158 | $path=self::cleanpath($prefix, $path); |
||
| 159 | if (isset($routeArray["methods"]) && \is_array($routeArray["methods"])) { |
||
| 160 | self::createRouteMethod($result, $controllerClass, $path, $routeArray["methods"], $methodName, $parameters, $name, $cache, $duration,$priority); |
||
| 161 | } elseif (\is_array($httpMethods)) { |
||
| 162 | self::createRouteMethod($result, $controllerClass, $path, $httpMethods, $methodName, $parameters, $name, $cache, $duration,$priority); |
||
| 163 | } else { |
||
| 164 | $result[$path]=[ "controller" => $controllerClass,"action" => $methodName,"parameters" => $parameters,"name" => $name,"cache" => $cache,"duration" => $duration,"priority"=>$priority]; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | public static function addParamsPath($path, \ReflectionMethod $method, $requirements) { |
||
| 169 | $parameters=[ ]; |
||
| 170 | $hasOptional=false; |
||
| 171 | preg_match_all('@\{(\.\.\.|\~)?(.+?)\}@s', $path, $matches); |
||
| 172 | if (isset($matches[2]) && \sizeof($matches[2]) > 0) { |
||
| 173 | $path=\preg_quote($path); |
||
| 174 | $params=Reflexion::getMethodParameters($method); |
||
| 175 | $index=0; |
||
| 176 | foreach ( $matches[2] as $paramMatch ) { |
||
| 177 | $find=\array_search($paramMatch, $params); |
||
| 178 | if ($find !== false) { |
||
| 179 | $requirement='.+?'; |
||
| 180 | if (isset($requirements[$paramMatch])) { |
||
| 181 | $requirement=$requirements[$paramMatch]; |
||
| 182 | } |
||
| 183 | self::scanParam($parameters, $hasOptional, $matches, $index, $paramMatch, $find, $path, $requirement); |
||
| 184 | } else { |
||
| 185 | throw new \Exception("{$paramMatch} is not a parameter of the method " . $method->name); |
||
| 186 | } |
||
| 187 | $index++; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | if ($hasOptional) |
||
| 191 | $path.="/(.*?)"; |
||
| 192 | return [ "path" => $path,"parameters" => $parameters ]; |
||
| 193 | } |
||
| 194 | |||
| 195 | private static function scanParam(&$parameters, &$hasOptional, $matches, $index, $paramMatch, $find, &$path, $requirement) { |
||
| 196 | $toReplace=true; |
||
| 197 | if (isset($matches[1][$index])) { |
||
| 198 | if ($matches[1][$index] === "...") { |
||
| 199 | $parameters[]="*"; |
||
| 200 | $path=\str_replace("\{\.\.\." . $paramMatch . "\}", "(.*?)", $path); |
||
| 201 | $toReplace=false; |
||
| 202 | } elseif ($matches[1][$index] === "~") { |
||
| 203 | $parameters[]="~" . $find; |
||
| 204 | $path=\str_replace("\{~" . $paramMatch . "\}", "", $path); |
||
| 205 | $hasOptional=true; |
||
| 206 | $toReplace=false; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | if($toReplace) { |
||
| 210 | $parameters[]=$find; |
||
| 211 | $path=\str_replace("\{" . $paramMatch . "\}", "({$requirement})", $path); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | private static function createRouteMethod(&$result, $controllerClass, $path, $httpMethods, $method, $parameters, $name, $cache, $duration,$priority) { |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | public function isRest() { |
||
| 223 | } |
||
| 224 | } |
||
| 225 |