Completed
Push — master ( d4e951...14a860 )
by Jean-Christophe
04:01
created

ClassUtils::getClassFullNameFromFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\cache;
4
5
use Ubiquity\utils\base\UString;
6
7
class ClassUtils {
8
9
	/**
10
	 * get the full name (name \ namespace) of a class from its file path
11
	 * result example: (string) "I\Am\The\Namespace\Of\This\Class"
12
	 *
13
	 * @param $filePathName
14
	 *
15
	 * @return string
16
	 */
17 6
	public static function getClassFullNameFromFile($filePathName,$backSlash=false) {
18 6
		$phpCode=\file_get_contents($filePathName);
19 6
		$ns=self::getClassNamespaceFromPhpCode($phpCode);
20 6
		if($backSlash && UString::isNotNull($ns)){
21
			$ns="\\".$ns;
22
		}
23 6
		return $ns. '\\' . self::getClassNameFromPhpCode($phpCode);
24
	}
25
26 1
	public static function cleanClassname($classname) {
27 1
		return \str_replace("\\", "\\\\", $classname);
28
	}
29
30
	/**
31
	 * Returns a cleanly namespace
32
	 * @param array|string $parts
33
	 * @return string
34
	 */
35 1
	public static function getNamespaceFromParts($parts){
36 1
		$resultArray=[];
37 1
		if(!\is_array($parts)){
38
			$parts=[$parts];
39
		}
40 1
		foreach ($parts as $part){
41 1
			$resultArray=\array_merge($resultArray,\explode("\\", $part));
42
		}
43 1
		$resultArray=\array_diff($resultArray, [""]);
44 1
		return \implode("\\", $resultArray);
45
	}
46
47
	/**
48
	 * build and return an object of a class from its file path
49
	 *
50
	 * @param $filePathName
51
	 *
52
	 * @return mixed
53
	 */
54
	public static function getClassObjectFromFile($filePathName) {
55
		$classString=self::getClassFullNameFromFile($filePathName);
56
		$object=new $classString();
57
		return $object;
58
	}
59
60
	/**
61
	 * get the class namespace form file path using token
62
	 *
63
	 * @param $filePathName
64
	 *
65
	 * @return null|string
66
	 */
67
	public static function getClassNamespaceFromFile($filePathName) {
68
		$phpCode=\file_get_contents($filePathName);
69
		return self::getClassNamespaceFromPhpCode($phpCode);
70
	}
71
72 6
	private static function getClassNamespaceFromPhpCode($phpCode) {
73 6
		$tokens=token_get_all($phpCode);
74 6
		$count=\count($tokens);
75 6
		$i=0;
76 6
		$namespace='';
77 6
		$namespace_ok=false;
78 6
		while ( $i < $count ) {
79 6
			$token=$tokens[$i];
80 6
			if (\is_array($token) && $token[0] === T_NAMESPACE) {
81
				// Found namespace declaration
82 6
				while ( ++$i < $count ) {
83 6
					if ($tokens[$i] === ';') {
84 6
						$namespace_ok=true;
85 6
						$namespace=\trim($namespace);
86 6
						break;
87
					}
88 6
					$namespace.=\is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
89
				}
90 6
				break;
91
			}
92 6
			$i++;
93
		}
94 6
		if (!$namespace_ok) {
95
			return null;
96
		} else {
97 6
			return $namespace;
98
		}
99
	}
100
101
	/**
102
	 * get the class name form file path using token
103
	 *
104
	 * @param $filePathName
105
	 *
106
	 * @return mixed
107
	 */
108
	public static function getClassNameFromFile($filePathName) {
109
		$phpCode=\file_get_contents($filePathName);
110
		return self::getClassNameFromPhpCode($phpCode);
111
	}
112
113 6
	private static function getClassNameFromPhpCode($phpCode){
114 6
		$classes=array ();
115 6
		$tokens=\token_get_all($phpCode);
116 6
		$count=count($tokens);
117 6
		for($i=2; $i < $count; $i++) {
118 6
			if ($tokens[$i - 2][0] == T_CLASS && $tokens[$i - 1][0] == T_WHITESPACE && $tokens[$i][0] == T_STRING) {
119 6
				$class_name=$tokens[$i][1];
120 6
				$classes[]=$class_name;
121
			}
122
		}
123 6
		if(isset($classes[0]))
124 6
			return $classes[0];
125
		return null;
126
	}
127
128 1
	public static function getClassNameWithNS($defaultNS,$name){
129 1
		if(\strpos($name, "\\")===false){
130 1
			$name=$defaultNS."\\".$name;
131
		}
132 1
		return $name;
133
	}
134
135 5
	public static function getClassSimpleName($classnameWithNamespace){
136 5
		return substr($classnameWithNamespace, strrpos($classnameWithNamespace, '\\')+1);
137
	}
138
}
139