Completed
Push — master ( 31895c...135d28 )
by Jean-Christophe
02:15
created

ControllerParser::asArray()   D

Complexity

Conditions 16
Paths 90

Size

Total Lines 48
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 4.9765
c 0
b 0
f 0
cc 16
eloc 36
nc 90
nop 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
namespace micro\cache;
3
use micro\orm\parser\Reflexion;
4
use micro\utils\StrUtils;
5
6
class ControllerParser {
7
	private $controllerClass;
8
	private $mainRouteClass;
9
	private $routesMethods=[];
10
11
	public function parse($controllerClass){
12
		$this->controllerClass=$controllerClass;
13
		$reflect=new \ReflectionClass($controllerClass);
14
		if(!$reflect->isAbstract()){
15
			$instance=new $controllerClass();
16
			$this->mainRouteClass= Reflexion::getAnnotationClass($controllerClass, "@route");
17
			$methods=Reflexion::getMethods($instance,\ReflectionMethod::IS_PUBLIC);
18
			foreach ($methods as $method){
19
				$annots=Reflexion::getAnnotationsMethod($controllerClass, $method->getName(), "@route");
1 ignored issue
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
20
				if($annots!==false)
21
					$this->routesMethods[$method->getName()]=["annotations"=>$annots,"method"=>$method];
1 ignored issue
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
22
			}
23
		}
24
	}
25
	private function cleanpath($prefix,$path=""){
26
		if(!StrUtils::endswith($prefix, "/"))
27
			$prefix=$prefix."/";
28
		if($path!=="" && StrUtils::startswith($path, "/"))
29
			$path=\substr($path, 1);
30
		$path=$prefix.$path;
31
		if(StrUtils::endswith($path, "/"))
32
			$path=\substr($path, 0,\strlen($path)-1);
33
		return $path;
34
	}
35
	public function asArray(){
36
		$result=[];
37
		$prefix="";$httpMethods=false;
38
		if($this->mainRouteClass){
39
			if(isset($this->mainRouteClass->path))
40
				$prefix=$this->mainRouteClass->path;
41
			if(isset($this->mainRouteClass->methods)){
42
				$httpMethods=$this->mainRouteClass->methods;
43
				if($httpMethods!==null){
44
					if(\is_string($httpMethods))
45
						$httpMethods=[$httpMethods];
46
				}
47
			}
48
		}
49
50
		foreach ($this->routesMethods as $method=>$arrayAnnotsMethod){
51
			$routeAnnotations=$arrayAnnotsMethod["annotations"];
52
			foreach ($routeAnnotations as $routeAnnotation){
53
				if(isset($routeAnnotation->path)){
54
					$parameters=[];
55
					$path=$routeAnnotation->path;
56
					preg_match_all('@\{(.+?)\}@s', $path, $matches);
57
					if(isset($matches[1]) && \sizeof($matches[1])>0){
58
						$path=\preg_quote($path);
59
						$params=Reflexion::getMethodParameters($arrayAnnotsMethod["method"]);
0 ignored issues
show
Bug introduced by
The method getMethodParameters() does not seem to exist on object<micro\orm\parser\Reflexion>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
						foreach ($matches[1] as $paramMatch){
61
							$find=\array_search($paramMatch, $params);
62
							if($find!==false){
63
								$parameters[]=$find;
64
								$path=\str_replace("\{".$paramMatch."\}", "(.+?)", $path);
65
							}else{
66
								throw new \Exception("{$paramMatch} is not a parameter of the method ".$arrayAnnotsMethod["method"]->getName());
67
							}
68
						}
69
					}
70
					$path=$this->cleanpath($prefix,$path)."$";
71
					if(isset($routeAnnotation->methods) && \is_array($routeAnnotation->methods)){
72
						$this->createRouteMethod($result,$path,$routeAnnotation->methods,$method,$routeAnnotation,$parameters);
73
					}elseif(\is_array($httpMethods)){
74
						$this->createRouteMethod($result,$path,$httpMethods,$method,$routeAnnotation,$parameters);
75
					}else{
76
						$result[$path]=["controller"=>$this->controllerClass,"action"=>$method,"parameters"=>$parameters];
77
					}
78
				}
79
			}
80
		}
81
		return $result;
82
	}
83
84
	private function createRouteMethod(&$result,$path,$httpMethods,$method,$routeAnnotation,$parameters){
0 ignored issues
show
Unused Code introduced by
The parameter $routeAnnotation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
		foreach ($httpMethods as $httpMethod){
86
				$result[$path][$httpMethod]=["controller"=>$this->controllerClass,"action"=>$method,"parameters"=>$parameters];
87
		}
88
	}
89
}