Passed
Push — master ( 3d04cc...319518 )
by Jean-Christophe
23:03 queued 11:07
created

UIntrospection::closure_dump()   B

Complexity

Conditions 9
Paths 52

Size

Total Lines 40
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 9.5338

Importance

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