|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@link FileHelper_PHPClassInfo} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Application Utils |
|
6
|
|
|
* @subpackage FileHelper |
|
7
|
|
|
* @see FileHelper_PHPClassInfo |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace AppUtils; |
|
13
|
|
|
|
|
14
|
|
|
use AppUtils\FileHelper\PHPFile; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Used to retrieve information on the PHP classes contained |
|
18
|
|
|
* within the target PHP file. Does not use the reflection API. |
|
19
|
|
|
* This is meant as a quick way to check for the presence of |
|
20
|
|
|
* classes, and which classes they extend or interfaces are |
|
21
|
|
|
* implemented. |
|
22
|
|
|
* |
|
23
|
|
|
* @package Application Utils |
|
24
|
|
|
* @subpackage FileHelper |
|
25
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
26
|
|
|
* @see FileHelper::findPHPClasses() |
|
27
|
|
|
*/ |
|
28
|
|
|
class FileHelper_PHPClassInfo |
|
29
|
|
|
{ |
|
30
|
|
|
public const TYPE_CLASS = 'class'; |
|
31
|
|
|
public const TYPE_TRAIT = 'trait'; |
|
32
|
|
|
public const TYPE_INTERFACE = 'interface'; |
|
33
|
|
|
|
|
34
|
|
|
protected PHPFile $file; |
|
35
|
|
|
protected string $namespace = ''; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array<string,FileHelper_PHPClassInfo_Class> |
|
39
|
|
|
*/ |
|
40
|
|
|
protected array $classes = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string[] |
|
44
|
|
|
*/ |
|
45
|
|
|
private static array $types = array( |
|
46
|
|
|
self::TYPE_CLASS, |
|
47
|
|
|
self::TYPE_INTERFACE, |
|
48
|
|
|
self::TYPE_TRAIT |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param PHPFile $path The path to the PHP file to parse. |
|
53
|
|
|
* @throws FileHelper_Exception |
|
54
|
|
|
* @see FileHelper::findPHPClasses() |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(PHPFile $path) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->file = $path |
|
59
|
|
|
->requireExists() |
|
60
|
|
|
->requireReadable(); |
|
61
|
|
|
|
|
62
|
|
|
$this->parseFile(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The name of the namespace of the classes in the file, if any. |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getNamespace() : string |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->namespace; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Whether the file contains a namespace. |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function hasNamespace() : bool |
|
79
|
|
|
{ |
|
80
|
|
|
return !empty($this->namespace); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* The absolute path to the file. |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getPath() : string |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->file->getPath(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Whether any classes were found in the file. |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function hasClasses() : bool |
|
97
|
|
|
{ |
|
98
|
|
|
return !empty($this->classes); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* The names of the classes that were found in the file (with namespace if any). |
|
103
|
|
|
* @return string[] |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getClassNames() : array |
|
106
|
|
|
{ |
|
107
|
|
|
return array_keys($this->classes); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Retrieves all classes that were detected in the file, |
|
112
|
|
|
* which can be used to retrieve more information about |
|
113
|
|
|
* them. |
|
114
|
|
|
* |
|
115
|
|
|
* @return FileHelper_PHPClassInfo_Class[] |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getClasses() : array |
|
118
|
|
|
{ |
|
119
|
|
|
return array_values($this->classes); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected function parseFile() : void |
|
123
|
|
|
{ |
|
124
|
|
|
$code = php_strip_whitespace($this->getPath()); |
|
125
|
|
|
|
|
126
|
|
|
$this->detectNamespace($code); |
|
127
|
|
|
$result = $this->detectMatches($code); |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
if($result === null) { |
|
|
|
|
|
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$indexes = array_keys($result[0]); |
|
134
|
|
|
|
|
135
|
|
|
foreach($indexes as $idx) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->parseResult( |
|
138
|
|
|
$result[1][$idx], |
|
139
|
|
|
$result[3][$idx], |
|
140
|
|
|
$result[2][$idx], |
|
141
|
|
|
$result[4][$idx], |
|
142
|
|
|
$result[5][$idx] |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
private function detectMatches(string $code) : ?array |
|
148
|
|
|
{ |
|
149
|
|
|
$types = implode('|', self::$types); |
|
150
|
|
|
$result = array(); |
|
151
|
|
|
|
|
152
|
|
|
preg_match_all('/(abstract|final)\s+('.$types.')\s+([\sa-z\d\\\\_,]+){|('.$types.')\s+([\sa-z\d\\\\_,]+){/ix', $code, $result, PREG_PATTERN_ORDER); |
|
153
|
|
|
|
|
154
|
|
|
if(isset($result[0][0])) { |
|
155
|
|
|
return $result; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return null; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
private function detectNamespace(string $code) : void |
|
162
|
|
|
{ |
|
163
|
|
|
$result = array(); |
|
164
|
|
|
preg_match_all('/namespace\s+([^;]+);/ix', $code, $result, PREG_PATTERN_ORDER); |
|
165
|
|
|
|
|
166
|
|
|
if(isset($result[0][0])) { |
|
167
|
|
|
$this->namespace = trim($result[1][0]); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
private function parseResult(string $keyword, string $declaration, string $type, string $simpleType, string $simpleDeclaration) : void |
|
172
|
|
|
{ |
|
173
|
|
|
if(empty($keyword)) { |
|
174
|
|
|
$type = $simpleType; |
|
175
|
|
|
$declaration = $simpleDeclaration; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$class = new FileHelper_PHPClassInfo_Class( |
|
179
|
|
|
$this, |
|
180
|
|
|
$this->stripWhitespace($declaration), |
|
181
|
|
|
trim($keyword), |
|
182
|
|
|
$type |
|
183
|
|
|
); |
|
184
|
|
|
|
|
185
|
|
|
$this->classes[$class->getNameNS()] = $class; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Strips all whitespace from the string, replacing it with |
|
190
|
|
|
* regular spaces (newlines, tabs, etc.). |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $string |
|
193
|
|
|
* @return string |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function stripWhitespace(string $string) : string |
|
196
|
|
|
{ |
|
197
|
|
|
return preg_replace('/\s/', ' ', $string); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.