Passed
Push — master ( 08d2f1...651967 )
by Jean-Christophe
06:00
created

ControllerParser::createRouteMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 10
crap 2

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