Passed
Pull Request — master (#59)
by
unknown
17:09
created

UIntrospection::getLoadedViews()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 11
c 3
b 0
f 0
dl 0
loc 15
ccs 11
cts 12
cp 0.9167
rs 9.2222
cc 6
nc 8
nop 2
crap 6.0208
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.5
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 10
	public static function closure_dump(\Closure $c) {
58 10
		$str = 'function (';
59 10
		$r = new \ReflectionFunction ( $c );
60 10
		$params = array ();
61 10
		foreach ( $r->getParameters () as $p ) {
62 10
			$s = '';
63 10
			if ($p->isArray ()) {
64
				$s .= 'array ';
65 10
			} else if ($p->getClass ()) {
66
				$s .= $p->getClass ()->name . ' ';
67
			}
68 10
			if ($p->isPassedByReference ()) {
69
				$s .= '&';
70
			}
71 10
			$s .= '$' . $p->name;
72 10
			if ($p->isOptional ()) {
73
				$s .= ' = ' . \var_export ( $p->getDefaultValue (), TRUE );
74
			}
75 10
			$params [] = $s;
76
		}
77 10
		$str .= \implode ( ', ', $params );
78 10
		$str .= ')';
79 10
		$lines = file ( $r->getFileName () );
80 10
		$sLine = $r->getStartLine ();
81 10
		$eLine = $r->getEndLine ();
82 10
		if ($eLine === $sLine) {
83
			$str .= \strstr ( \strstr ( $lines [$sLine - 1], "{" ), "}", true ) . "}";
84
		} else {
85 10
			$str .= \strrchr ( $lines [$sLine - 1], "{" );
86 10
			for($l = $sLine; $l < $eLine - 1; $l ++) {
87 10
				$str .= $lines [$l];
88
			}
89 10
			$str .= \strstr ( $lines [$eLine - 1], "}", true ) . "}";
90
		}
91 10
		$vars = $r->getStaticVariables ();
92 10
		foreach ( $vars as $k => $v ) {
93 1
			$str = \str_replace ( '$' . $k, \var_export ( $v, true ), $str );
94
		}
95 10
		return $str;
96
	}
97
98
	public static function getChildClasses($baseClass) {
99
		$children = [ ];
100
		foreach ( \get_declared_classes () as $class ) {
101
			$rClass = new \ReflectionClass ( $class );
102
			if ($rClass->isSubclassOf ( $baseClass )) {
103
				$children [] = $class;
104
			}
105
		}
106
		return $children;
107
	}
108
}
109