Test Failed
Push — master ( 283784...a842fc )
by Jean-Christophe
19:02
created

UIntrospection::closure_dump()   C

Complexity

Conditions 12
Paths 196

Size

Total Lines 46
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 13.7202

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 46
ccs 27
cts 35
cp 0.7714
rs 6.1666
cc 12
nc 196
nop 1
crap 13.7202

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Ubiquity\utils\base;
3
4
/**
5
 * Ubiquity\utils\base$UIntrospection
6
 * This class is part of Ubiquity
7
 *
8
 * @author jcheron <[email protected]>
9
 * @version 1.0.8
10
 *
11
 */
12
class UIntrospection {
13
14
	public static function getClassCode($classname) {
15 2
		$r = new \ReflectionClass($classname);
16 2
		$lines = file($r->getFileName());
17 2
		return $lines;
18 2
	}
19
20
	public static function getFileName($classname) {
21 1
		$r = new \ReflectionClass($classname);
22 1
		return $r->getFileName();
23 1
	}
24
25
	public static function getLoadedViews(\ReflectionMethod $r, $lines) {
26 2
		$result = [];
27 2
		$code = self::getMethodCode($r, $lines);
28 2
		\preg_match_all('@(?:.*?)\$this\-\>loadView\([\'\"](.+?)[\'\"](?:.*?)@s', $code, $matches);
29 2
		if (isset($matches[1]) && \sizeof($matches[1]) > 0) {
30 2
			$result = array_merge($result, $matches[1]);
31 2
		}
32
		\preg_match_all('@(?:.*?)\$this\-\>jquery\-\>renderView\([\'\"](.+?)[\'\"](?:.*?)@s', $code, $matches);
33 2
		if (isset($matches[1])) {
34 2
			$result = array_merge($result, $matches[1]);
35 2
		}
36
		if (\strpos($code, '$this->loadDefaultView') !== false || strpos($code, '$this->jquery->renderDefaultView') !== false) {
37 2
			$result[] = $r->getDeclaringClass()->getShortName() . '/' . $r->getName() . '.html';
38 2
		}
39
		return $result;
40 2
	}
41
42
	public static function getMethodCode(\ReflectionMethod $r, $lines) {
43 2
		$str = "";
44 2
		$count = \sizeof($lines);
45 2
		$sLine = $r->getStartLine();
46 2
		$eLine = $r->getEndLine();
47 2
		if ($sLine == $eLine)
48 2
			return $lines[$sLine];
49
			$min = \min($eLine, $count);
50 2
			for ($l = $sLine; $l < $min; $l ++) {
51 2
				$str .= $lines[$l];
52 2
			}
53
			return $str;
54 2
	}
55
56
	public static function implementsMethod($object, $methodName, $baseDeclaringClass) {
57
		$m = new \ReflectionMethod($object, $methodName);
58
		return $m->getDeclaringClass()->getName() !== $baseDeclaringClass;
59
	}
60
61
	public static function closure_dump(\Closure $c) {
62 11
		$str = 'function (';
63 11
		$r = new \ReflectionFunction($c);
64 11
		$params = array();
65 11
		foreach ($r->getParameters() as $p) {
66 11
			$s = '';
67 11
			$type = $p->getType();
68 11
			$isArray = $type && $type->getName() === 'array';
69
			if ($isArray) {
70 11
				$s .= 'array ';
71
			} else if ($type) {
72
				$class = ! $type->isBuiltin() ? new \ReflectionClass($type->getName()) : null;
73 11
				if ($class != null) {
74
					$s .= $class . ' ';
75
				}
76 11
			}
77 11
			if ($p->isPassedByReference()) {
78 11
				$s .= '&';
79
			}
80 11
			$s .= '$' . $p->name;
81
			if ($p->isOptional()) {
82 11
				$s .= ' = ' . \var_export($p->getDefaultValue(), TRUE);
83 11
			}
84 11
			$params[] = $s;
85 11
		}
86 11
		$str .= \implode(', ', $params);
87 11
		$str .= ')';
88
		$lines = file($r->getFileName());
89
		$sLine = $r->getStartLine();
90
		$eLine = $r->getEndLine();
91 11
		if ($eLine === $sLine) {
92 11
			$match = \strstr($lines[$sLine - 1], "function");
93 11
			$str .= \strstr(\strstr($match, "{"), "}", true) . "}";
94
		} else {
95 11
			$str .= \strrchr($lines[$sLine - 1], "{");
96
			for ($l = $sLine; $l < $eLine - 1; $l ++) {
97 11
				$str .= $lines[$l];
98 11
			}
99 1
			$str .= \strstr($lines[$eLine - 1], "}", true) . "}";
100
		}
101 11
		$vars = $r->getStaticVariables();
102
103
		foreach ($vars as $k => $v) {
104
			$str = \str_replace('$' . $k, \var_export($v, true), $str);
105
		}
106
		return $str;
107
	}
108
109
	public static function getChildClasses($baseClass) {
110
		$children = [];
111
		foreach (\get_declared_classes() as $class) {
112
			$rClass = new \ReflectionClass($class);
113
			if ($rClass->isSubclassOf($baseClass)) {
114
				$children[] = $class;
115
			}
116
		}
117
		return $children;
118
	}
119
}
120