Completed
Push — master ( fad9f9...8658c1 )
by Jean-Christophe
01:44
created

UIntrospection::getClassCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Ubiquity\utils\base;
4
5
class UIntrospection {
6
7
	public static function getClassCode($classname) {
8
		$r=new \ReflectionClass($classname);
9
		$lines=file($r->getFileName());
10
		return $lines;
11
	}
12
13
	public static function getFileName($classname) {
14
		$r=new \ReflectionClass($classname);
15
		return $r->getFileName();
16
	}
17
18
	public static function getLoadedViews(\ReflectionMethod $r, $lines) {
19
		$matches=[ ];
20
		$code=self::getMethodCode($r, $lines);
21
		\preg_match_all('@(?:.*?)\$this\-\>loadView\([\'\"](.+?)[\'\"](?:.*?)@s', $code, $matches);
22
		if (isset($matches[1])) {
23
			return $matches[1];
24
		}
25
		return [ ];
26
	}
27
28
	public static function getMethodCode(\ReflectionMethod $r, $lines) {
29
		$str="";
30
		$count=\sizeof($lines);
31
		$sLine=$r->getStartLine();
32
		$eLine=$r->getEndLine();
33
		if ($sLine == $eLine)
34
			return $lines[$sLine];
35
		$min=\min($eLine, $count);
36
		for($l=$sLine; $l < $min; $l++) {
37
			$str.=$lines[$l];
38
		}
39
		return $str;
40
	}
41
42
	public static function closure_dump(\Closure $c) {
43
		$str='function (';
44
		$r=new \ReflectionFunction($c);
45
		$params=array ();
46
		foreach ( $r->getParameters() as $p ) {
47
			$s='';
48
			if ($p->isArray()) {
49
				$s.='array ';
50
			} else if ($p->getClass()) {
51
				$s.=$p->getClass()->name . ' ';
52
			}
53
			if ($p->isPassedByReference()) {
54
				$s.='&';
55
			}
56
			$s.='$' . $p->name;
57
			if ($p->isOptional()) {
58
				$s.=' = ' . \var_export($p->getDefaultValue(), TRUE);
59
			}
60
			$params[]=$s;
61
		}
62
		$str.=\implode(', ', $params);
63
		$str.='){' . PHP_EOL;
64
		$lines=file($r->getFileName());
65
		$sLine=$r->getStartLine();
66
		$eLine=$r->getEndLine();
67
		for($l=$sLine; $l < $eLine; $l++) {
68
			$str.=$lines[$l];
69
		}
70
		return $str;
71
	}
72
}
73