Passed
Branch master (9957c5)
by Jean-Christophe
12:44
created

UIntrospection::getClassCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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.4
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
		$matches = [ ];
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
			return $matches [1];
32
		}
33 2
		\preg_match_all ( '@(?:.*?)\$this\-\>jquery\-\>renderView\([\'\"](.+?)[\'\"](?:.*?)@s', $code, $matches );
34 2
		if (isset ( $matches [1] )) {
35 2
			return $matches [1];
36
		}
37
		return [ ];
38
	}
39
40 2
	public static function getMethodCode(\ReflectionMethod $r, $lines) {
41 2
		$str = "";
42 2
		$count = \sizeof ( $lines );
43 2
		$sLine = $r->getStartLine ();
44 2
		$eLine = $r->getEndLine ();
45 2
		if ($sLine == $eLine)
46
			return $lines [$sLine];
47 2
		$min = \min ( $eLine, $count );
48 2
		for($l = $sLine; $l < $min; $l ++) {
49 2
			$str .= $lines [$l];
50
		}
51 2
		return $str;
52
	}
53
54 10
	public static function closure_dump(\Closure $c) {
55 10
		$str = 'function (';
56 10
		$r = new \ReflectionFunction ( $c );
57 10
		$params = array ();
58 10
		foreach ( $r->getParameters () as $p ) {
59 10
			$s = '';
60 10
			if ($p->isArray ()) {
61
				$s .= 'array ';
62 10
			} else if ($p->getClass ()) {
63
				$s .= $p->getClass ()->name . ' ';
64
			}
65 10
			if ($p->isPassedByReference ()) {
66
				$s .= '&';
67
			}
68 10
			$s .= '$' . $p->name;
69 10
			if ($p->isOptional ()) {
70
				$s .= ' = ' . \var_export ( $p->getDefaultValue (), TRUE );
71
			}
72 10
			$params [] = $s;
73
		}
74 10
		$str .= \implode ( ', ', $params );
75 10
		$str .= ')';
76 10
		$lines = file ( $r->getFileName () );
77 10
		$sLine = $r->getStartLine ();
78 10
		$eLine = $r->getEndLine ();
79 10
		if ($eLine === $sLine) {
80
			$str .= \strstr ( \strstr ( $lines [$sLine - 1], "{" ), "}", true ) . "}";
81
		} else {
82 10
			$str .= \strrchr ( $lines [$sLine - 1], "{" );
83 10
			for($l = $sLine; $l < $eLine - 1; $l ++) {
84 10
				$str .= $lines [$l];
85
			}
86 10
			$str .= \strstr ( $lines [$eLine - 1], "}", true ) . "}";
87
		}
88 10
		$vars = $r->getStaticVariables ();
89 10
		foreach ( $vars as $k => $v ) {
90 1
			$str = \str_replace ( '$' . $k, \var_export ( $v, true ), $str );
91
		}
92 10
		return $str;
93
	}
94
95
	public static function getChildClasses($baseClass) {
96
		$children = [ ];
97
		foreach ( \get_declared_classes () as $class ) {
98
			$rClass = new \ReflectionClass ( $class );
99
			if ($rClass->isSubclassOf ( $baseClass )) {
100
				$children [] = $class;
101
			}
102
		}
103
		return $children;
104
	}
105
}
106