Passed
Push — master ( 83996b...17a193 )
by Jean-Christophe
09:22
created

RouterModifierTrait::addCallableRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 8
dl 0
loc 4
ccs 4
cts 4
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
/**
9
 * Ubiquity\controllers\traits$RouterModifierTrait
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.1
14
 *
15
 */
16
trait RouterModifierTrait {
17
18
	/**
19
	 *
20
	 * @param string $path
21
	 * @param string $controller
22
	 * @param string $action
23
	 * @param array|null $methods
24
	 * @param string $name
25
	 * @param boolean $cache
26
	 * @param int $duration
27
	 * @param array $requirements
28
	 */
29 36
	public static function addRoute($path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
30 36
		self::addRouteToRoutes ( self::$routes, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority );
31 36
	}
32
33 36
	public static function addRouteToRoutes(&$routesArray, $path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0, $callback = null) {
34 36
		if (\class_exists ( $controller )) {
35 36
			$method = new \ReflectionMethod ( $controller, $action );
36 36
			self::_addRoute ( $method, $routesArray, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority, $callback );
37
		}
38 36
	}
39
40 36
	private static function _addRoute(\ReflectionMethod $method, &$routesArray, $path, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0, $callback = null) {
41 36
		$result = [ ];
42 36
		ControllerParser::parseRouteArray ( $result, $controller, [ "path" => $path,"methods" => $methods,"name" => $name,"cache" => $cache,"duration" => $duration,"requirements" => $requirements,"priority" => $priority,"callback" => $callback ], $method, $action );
43 36
		foreach ( $result as $k => $v ) {
44 36
			$routesArray [$k] = $v;
45
		}
46 36
	}
47
48 36
	public static function addCallableRoute($path, $callable, $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
49 36
		if (is_callable ( $callable )) {
50 36
			$reflectionFunction = new \ReflectionFunction ( $callable );
51 36
			self::_addCallableRoute ( $reflectionFunction, self::$routes, $path, $callable, $methods, $name, $cache, $duration, $requirements, $priority );
52
		}
53 36
	}
54
55 36
	public static function get($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
56 36
		self::addCallableRoute ( $path, $callable, [ 'get' ], $name, $cache, $duration, $requirements, $priority );
57 36
	}
58
59
	public static function post($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
60
		self::addCallableRoute ( $path, $callable, [ 'post' ], $name, $cache, $duration, $requirements, $priority );
61
	}
62
63
	public static function delete($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
64
		self::addCallableRoute ( $path, $callable, [ 'delete' ], $name, $cache, $duration, $requirements, $priority );
65
	}
66
67
	public static function put($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
68
		self::addCallableRoute ( $path, $callable, [ 'put' ], $name, $cache, $duration, $requirements, $priority );
69
	}
70
71
	public static function patch($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
72
		self::addCallableRoute ( $path, $callable, [ 'patch' ], $name, $cache, $duration, $requirements, $priority );
73
	}
74
75
	public static function options($path, $callable, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
76
		self::addCallableRoute ( $path, $callable, [ 'options' ], $name, $cache, $duration, $requirements, $priority );
77
	}
78
79 36
	private static function _addCallableRoute(\ReflectionFunction $reflectionFunction, &$routesArray, $path, $callable, $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
80 36
		$result = [ ];
81 36
		CallableParser::parseRouteArray ( $result, $callable, [ "path" => $path,"methods" => $methods,"name" => $name,"cache" => $cache,"duration" => $duration,"requirements" => $requirements,"priority" => $priority ], $reflectionFunction );
82 36
		foreach ( $result as $k => $v ) {
83 36
			$routesArray [$k] = $v;
84
		}
85 36
	}
86
87
	public static function addRoutesToRoutes(&$routesArray, $paths, $controller, $action = "index", $methods = null, $name = "", $cache = false, $duration = null, $requirements = [], $priority = 0) {
88
		if (\class_exists ( $controller )) {
89
			$method = new \ReflectionMethod ( $controller, $action );
90
			foreach ( $paths as $path ) {
91
				self::_addRoute ( $method, $routesArray, $path, $controller, $action, $methods, $name, $cache, $duration, $requirements, $priority );
92
			}
93
		}
94
	}
95
}
96
97