Completed
Push — master ( 707e60...bec0f5 )
by Jean-Christophe
01:22
created

ClassUtils::getClassNameFromPhpCode()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace micro\cache;
4
5
class ClassUtils {
6
7
	/**
8
	 * get the full name (name \ namespace) of a class from its file path
9
	 * result example: (string) "I\Am\The\Namespace\Of\This\Class"
10
	 *
11
	 * @param $filePathName
12
	 *
13
	 * @return string
14
	 */
15
	public static function getClassFullNameFromFile($filePathName) {
16
		$phpCode=file_get_contents($filePathName);
17
		return self::getClassNamespaceFromPhpCode($phpCode) . '\\' . self::getClassNameFromPhpCode($phpCode);
18
	}
19
20
	public static function cleanClassname($classname) {
21
		return \str_replace("\\", "\\\\", $classname);
22
	}
23
24
	/**
25
	 * build and return an object of a class from its file path
26
	 *
27
	 * @param $filePathName
28
	 *
29
	 * @return mixed
30
	 */
31
	public static function getClassObjectFromFile($filePathName) {
32
		$classString=self::getClassFullNameFromFile($filePathName);
33
		$object=new $classString();
34
		return $object;
35
	}
36
37
	/**
38
	 * get the class namespace form file path using token
39
	 *
40
	 * @param $filePathName
41
	 *
42
	 * @return null|string
43
	 */
44
	public static function getClassNamespaceFromFile($filePathName) {
45
		$phpCode=file_get_contents($filePathName);
46
		return self::getClassNamespaceFromPhpCode($phpCode);
47
	}
48
49
	private static function getClassNamespaceFromPhpCode($phpCode) {
50
		$tokens=token_get_all($phpCode);
51
		$count=count($tokens);
52
		$i=0;
53
		$namespace='';
54
		$namespace_ok=false;
55
		while ( $i < $count ) {
56
			$token=$tokens[$i];
57
			if (is_array($token) && $token[0] === T_NAMESPACE) {
58
				// Found namespace declaration
59
				while ( ++$i < $count ) {
60
					if ($tokens[$i] === ';') {
61
						$namespace_ok=true;
62
						$namespace=trim($namespace);
63
						break;
64
					}
65
					$namespace.=is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
66
				}
67
				break;
68
			}
69
			$i++;
70
		}
71
		if (!$namespace_ok) {
72
			return null;
73
		} else {
74
			return $namespace;
75
		}
76
	}
77
78
	/**
79
	 * get the class name form file path using token
80
	 *
81
	 * @param $filePathName
82
	 *
83
	 * @return mixed
84
	 */
85
	public static function getClassNameFromFile($filePathName) {
86
		$phpCode=file_get_contents($filePathName);
87
		return self::getClassNameFromPhpCode($phpCode);
88
	}
89
90
	private static function getClassNameFromPhpCode($phpCode){
91
		$classes=array ();
92
		$tokens=token_get_all($phpCode);
93
		$count=count($tokens);
94
		for($i=2; $i < $count; $i++) {
95
			if ($tokens[$i - 2][0] == T_CLASS && $tokens[$i - 1][0] == T_WHITESPACE && $tokens[$i][0] == T_STRING) {
96
97
				$class_name=$tokens[$i][1];
98
				$classes[]=$class_name;
99
			}
100
		}
101
102
		return $classes[0];
103
	}
104
}
105