Completed
Push — master ( b5f91d...d46a17 )
by Jean-Christophe
01:26
created

Introspection::closure_dump()   C

Complexity

Conditions 7
Paths 26

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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