Passed
Push — master ( 3a5a7b...e59e29 )
by Jean-Christophe
13:15
created

ControllerParser::createRouteMethod()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 11
crap 3.0416

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Ubiquity\cache\parser;
4
5
use Ubiquity\orm\parser\Reflexion;
6
use Ubiquity\utils\base\UString;
7
use Ubiquity\annotations\router\RouteAnnotation;
8
use Ubiquity\cache\ClassUtils;
9
10
/**
11
 * Scans a controller to detect routes defined by annotations.
12
 * Ubiquity\cache\parser$ControllerParser
13
 * This class is part of Ubiquity
14
 *
15
 * @author jcheron <[email protected]>
16
 * @version 1.0.7
17
 *
18
 */
19
class ControllerParser {
20
	use ControllerParserPathTrait;
21
	private $controllerClass;
22
	private $mainRouteClass;
23
	private $routesMethods = [ ];
24
	private $rest = false;
25
	private static $excludeds = [ "__construct","isValid","initialize","finalize","onInvalidControl","loadView","forward","redirectToRoute" ];
26
27 17
	public function parse($controllerClass) {
28 17
		$automated = false;
29 17
		$inherited = false;
30 17
		$this->controllerClass = $controllerClass;
31 17
		$restAnnotsClass = [ ];
32 17
		$reflect = new \ReflectionClass ( $controllerClass );
33 17
		if (! $reflect->isAbstract () && $reflect->isSubclassOf ( "Ubiquity\controllers\Controller" )) {
34
			try {
35 17
				$annotsClass = Reflexion::getAnnotationClass ( $controllerClass, "@route" );
36 17
				$restAnnotsClass = Reflexion::getAnnotationClass ( $controllerClass, "@rest" );
37
			} catch ( \Exception $e ) {
38
				// When controllerClass generates an exception
39
			}
40 17
			$this->rest = \sizeof ( $restAnnotsClass ) > 0;
41 17
			if (isset ( $annotsClass ) && \sizeof ( $annotsClass ) > 0) {
42 17
				$this->mainRouteClass = $annotsClass [0];
43 17
				$inherited = $this->mainRouteClass->inherited;
44 17
				$automated = $this->mainRouteClass->automated;
45
			}
46 17
			$methods = Reflexion::getMethods ( $controllerClass, \ReflectionMethod::IS_PUBLIC );
47 17
			$this->parseMethods ( $methods, $controllerClass, $inherited, $automated );
48
		}
49 17
	}
50
51 17
	private function parseMethods($methods, $controllerClass, $inherited, $automated) {
52 17
		foreach ( $methods as $method ) {
53 17
			if ($method->getDeclaringClass ()->getName () === $controllerClass || $inherited) {
54
				try {
55 17
					$annots = Reflexion::getAnnotationsMethod ( $controllerClass, $method->name, [ "@route","@get","@post" ] );
56 17
					if (sizeof ( $annots ) > 0) {
57 17
						foreach ( $annots as $annot ) {
58 17
							$this->parseAnnot ( $annot, $method );
59
						}
60 17
						$this->routesMethods [$method->name] = [ "annotations" => $annots,"method" => $method ];
61
					} else {
62 17
						if ($automated) {
63 17
							if ($method->class !== 'Ubiquity\\controllers\\Controller' && \array_search ( $method->name, self::$excludeds ) === false && ! UString::startswith ( $method->name, "_" ))
64 17
								$this->routesMethods [$method->name] = [ "annotations" => $this->generateRouteAnnotationFromMethod ( $method ),"method" => $method ];
65
						}
66
					}
67 1
				} catch ( \Exception $e ) {
68
					// When controllerClass generates an exception
69
				}
70
			}
71
		}
72 17
	}
73
74 17
	private function parseAnnot(&$annot, $method) {
75 17
		if (UString::isNull ( $annot->path )) {
76 17
			$newAnnot = $this->generateRouteAnnotationFromMethod ( $method );
77 17
			$annot->path = $newAnnot [0]->path;
78
		} else {
79 17
			$annot->path = $this->parseMethodPath ( $method, $annot->path );
80
		}
81 17
	}
82
83 17
	private function generateRouteAnnotationFromMethod(\ReflectionMethod $method) {
84 17
		$annot = new RouteAnnotation ();
85 17
		$annot->path = self::getPathFromMethod ( $method );
86 17
		return [ $annot ];
87
	}
88
89 17
	public function asArray() {
90 17
		$result = [ ];
91 17
		$prefix = "";
92 17
		$httpMethods = false;
93 17
		if ($this->mainRouteClass) {
94 17
			if (isset ( $this->mainRouteClass->path ))
95 17
				$prefix = $this->mainRouteClass->path;
96 17
			if (isset ( $this->mainRouteClass->methods )) {
97
				$httpMethods = $this->mainRouteClass->methods;
98
				if ($httpMethods !== null) {
99
					if (\is_string ( $httpMethods ))
100
						$httpMethods = [ $httpMethods ];
101
				}
102
			}
103
		}
104 17
		foreach ( $this->routesMethods as $method => $arrayAnnotsMethod ) {
105 17
			$routeAnnotations = $arrayAnnotsMethod ["annotations"];
106
107 17
			foreach ( $routeAnnotations as $routeAnnotation ) {
108 17
				$params = [ "path" => $routeAnnotation->path,"methods" => $routeAnnotation->methods,"name" => $routeAnnotation->name,"cache" => $routeAnnotation->cache,"duration" => $routeAnnotation->duration,"requirements" => $routeAnnotation->requirements,"priority" => $routeAnnotation->priority ];
109 17
				self::parseRouteArray ( $result, $this->controllerClass, $params, $arrayAnnotsMethod ["method"], $method, $prefix, $httpMethods );
110
			}
111
		}
112 17
		return $result;
113
	}
114
115 37
	public static function parseRouteArray(&$result, $controllerClass, $routeArray, \ReflectionMethod $method, $methodName, $prefix = "", $httpMethods = NULL) {
116 37
		if (! isset ( $routeArray ["path"] )) {
117
			$routeArray ["path"] = self::getPathFromMethod ( $method );
118
		}
119 37
		$pathParameters = self::addParamsPath ( $routeArray ["path"], $method, $routeArray ["requirements"] );
120 37
		$name = $routeArray ["name"];
121 37
		if (! isset ( $name )) {
122 17
			$name = UString::cleanAttribute ( ClassUtils::getClassSimpleName ( $controllerClass ) . "_" . $methodName );
123
		}
124 37
		$cache = $routeArray ["cache"];
125 37
		$duration = $routeArray ["duration"];
126 37
		$path = $pathParameters ["path"];
127 37
		$parameters = $pathParameters ["parameters"];
128 37
		$priority = $routeArray ["priority"];
129 37
		$callback = $routeArray ["callback"] ?? null;
130 37
		$path = self::cleanpath ( $prefix, $path );
131 37
		if (isset ( $routeArray ["methods"] ) && \is_array ( $routeArray ["methods"] )) {
132 17
			self::createRouteMethod ( $result, $controllerClass, $path, $routeArray ["methods"], $methodName, $parameters, $name, $cache, $duration, $priority, $callback );
133 37
		} elseif (\is_array ( $httpMethods )) {
134
			self::createRouteMethod ( $result, $controllerClass, $path, $httpMethods, $methodName, $parameters, $name, $cache, $duration, $priority, $callback );
135
		} else {
136 37
			$v = [ "controller" => $controllerClass,"action" => $methodName,"parameters" => $parameters,"name" => $name,"cache" => $cache,"duration" => $duration,"priority" => $priority ];
137 37
			if (isset ( $callback )) {
138 1
				$v ['callback'] = $callback;
139
			}
140 37
			$result [$path] = $v;
141
		}
142 37
	}
143
144 17
	private static function createRouteMethod(&$result, $controllerClass, $path, $httpMethods, $method, $parameters, $name, $cache, $duration, $priority, $callback = null) {
145 17
		foreach ( $httpMethods as $httpMethod ) {
146 17
			$v = [ "controller" => $controllerClass,"action" => $method,"parameters" => $parameters,"name" => $name,"cache" => $cache,"duration" => $duration,"priority" => $priority ];
147 17
			if (isset ( $callback )) {
148
				$v ['callback'] = $callback;
149
			}
150 17
			$result [$path] [$httpMethod] = $v;
151
		}
152 17
	}
153
154 17
	public function isRest() {
155 17
		return $this->rest;
156
	}
157
}
158