Test Failed
Push — master ( 0dac24...9d4dcc )
by Jean-Christophe
02:41
created

CallableParser::createRouteMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 9
dl 0
loc 3
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\cache\parser;
4
5
use Ubiquity\utils\base\UString;
6
7
/**
8
 * Ubiquity\cache\parser$CallableParser
9
 * This class is part of Ubiquity
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.0.0
13
 *
14
 */
15
class CallableParser {
16
17
	public static function parseRouteArray(&$result, $callable, $routeArray, \ReflectionFunction $function, $prefix = "", $httpMethods = NULL) {
18
		$pathParameters = self::addParamsPath ( $routeArray ["path"], $function, $routeArray ["requirements"] );
19
		$name = $routeArray ["name"];
20
		if (! isset ( $name )) {
21
			$name = UString::cleanAttribute ( $pathParameters );
22
		}
23
		$cache = $routeArray ["cache"];
24
		$duration = $routeArray ["duration"];
25
		$path = $pathParameters ["path"];
26
		$parameters = $pathParameters ["parameters"];
27
		$priority = $routeArray ["priority"];
28
		$path = self::cleanpath ( $prefix, $path );
0 ignored issues
show
Bug introduced by
The method cleanpath() does not exist on Ubiquity\cache\parser\CallableParser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
		/** @scrutinizer ignore-call */ 
29
  $path = self::cleanpath ( $prefix, $path );

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...
29
		if (isset ( $routeArray ["methods"] ) && \is_array ( $routeArray ["methods"] )) {
30
			self::createRouteMethod ( $result, $callable, $path, $routeArray ["methods"], $parameters, $name, $cache, $duration, $priority );
31
		} elseif (\is_array ( $httpMethods )) {
32
			self::createRouteMethod ( $result, $callable, $path, $httpMethods, $parameters, $name, $cache, $duration, $priority );
33
		} else {
34
			$result [$path] = [ "controller" => $callable,"action" => "","parameters" => $parameters,"name" => $name,"cache" => $cache,"duration" => $duration,"priority" => $priority ];
35
		}
36
	}
37
38
	private static function createRouteMethod(&$result, $callable, $path, $httpMethods, $parameters, $name, $cache, $duration, $priority) {
39
		foreach ( $httpMethods as $httpMethod ) {
40
			$result [$path] [$httpMethod] = [ "controller" => $callable,"action" => "","parameters" => $parameters,"name" => $name,"cache" => $cache,"duration" => $duration,"priority" => $priority ];
41
		}
42
	}
43
44
	public static function addParamsPath($path, \ReflectionFunction $function, $requirements) {
45
		$parameters = [ ];
46
		$hasOptional = false;
47
		preg_match_all ( '@\{(\.\.\.|\~)?(.+?)\}@s', $path, $matches );
48
		if (isset ( $matches [2] ) && \sizeof ( $matches [2] ) > 0) {
49
			$path = \preg_quote ( $path );
50
			$params = $function->getParameters ();
51
			$index = 0;
52
			foreach ( $matches [2] as $paramMatch ) {
53
				$find = \array_search ( $paramMatch, $params );
54
				if ($find !== false) {
55
					$requirement = '.+?';
56
					if (isset ( $requirements [$paramMatch] )) {
57
						$requirement = $requirements [$paramMatch];
58
					}
59
					ControllerParser::scanParam ( $parameters, $hasOptional, $matches, $index, $paramMatch, $find, $path, $requirement );
60
				} else {
61
					throw new \Exception ( "{$paramMatch} is not a parameter of the function " . $function->name );
62
				}
63
				$index ++;
64
			}
65
		}
66
		if ($hasOptional)
67
			$path .= "/(.*?)";
68
		return [ "path" => $path,"parameters" => $parameters ];
69
	}
70
}
71
72