Passed
Push — master ( 3a5a7b...e59e29 )
by Jean-Christophe
13:15
created

UIntrospection::closure_dump()   B

Complexity

Conditions 9
Paths 52

Size

Total Lines 39
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 9.3399

Importance

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