Completed
Push — master ( d4e951...14a860 )
by Jean-Christophe
04:01
created

RouterModifierTrait::_addRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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

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\controllers\traits;
4
5
use Ubiquity\cache\parser\ControllerParser;
6
use Ubiquity\cache\parser\CallableParser;
7
8
trait RouterModifierTrait {
9
10
	/**
11
	 *
12
	 * @param string $path
13
	 * @param string $controller
14
	 * @param string $action
15
	 * @param array|null $methods
16
	 * @param string $name
17
	 * @param boolean $cache
18
	 * @param int $duration
19
	 * @param array $requirements
20
	 */
21 12
	public static function addRoute($path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
22 12
		self::addRouteToRoutes ( self::$routes, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority );
23 12
	}
24
25 12
	public static function addRouteToRoutes(&$routesArray, $path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
26 12
		if (\class_exists ( $controller )) {
27 12
			$method = new \ReflectionMethod ( $controller, $action );
28 12
			self::_addRoute ( $method, $routesArray, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority );
29
		}
30 12
	}
31
32 12
	private static function _addRoute(\ReflectionMethod $method, &$routesArray, $path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
33 12
		$result = [ ];
34 12
		ControllerParser::parseRouteArray ( $result, $controller, [ "path" => $path,"methods" => $methods,"name" => $name,"cache" => $cache,"duration" => $duration,"requirements" => $requirements,"priority" => $priority ], $method, $action );
35 12
		foreach ( $result as $k => $v ) {
36 12
			$routesArray [$k] = $v;
37
		}
38 12
	}
39
40 12
	public static function addCallableRoute($path, $callable, $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
41 12
		if (is_callable ( $callable )) {
42 12
			$reflectionFunction = new \ReflectionFunction ( $callable );
43 12
			self::_addCallableRoute ( $reflectionFunction, self::$routes, $path, $callable, $methods, $name, $cache, $duration, $requirements, $priority );
44
		}
45 12
	}
46
47 12
	public static function get($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
48 12
		self::addCallableRoute ( $path, $callable, [ 'get' ], $name, $cache, $duration, $requirements, $priority );
49 12
	}
50
51 12
	private static function _addCallableRoute(\ReflectionFunction $reflectionFunction, &$routesArray, $path, $callable, $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
52 12
		$result = [ ];
53 12
		CallableParser::parseRouteArray ( $result, $callable, [ "path" => $path,"methods" => $methods,"name" => $name,"cache" => $cache,"duration" => $duration,"requirements" => $requirements,"priority" => $priority ], $reflectionFunction );
54 12
		foreach ( $result as $k => $v ) {
55 12
			$routesArray [$k] = $v;
56
		}
57 12
	}
58
59
	public static function addRoutesToRoutes(&$routesArray, $paths, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
60
		if (\class_exists ( $controller )) {
61
			$method = new \ReflectionMethod ( $controller, $action );
62
			foreach ( $paths as $path ) {
63
				self::_addRoute ( $method, $routesArray, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority );
64
			}
65
		}
66
	}
67
}
68
69