|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL, Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/addendum |
|
7
|
|
|
* @licence AGPL, Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes) |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes) |
|
10
|
|
|
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser) |
|
11
|
|
|
* @link https://maslosoft.com/addendum/ - maslosoft addendum |
|
12
|
|
|
* @link https://code.google.com/p/addendum/ - original addendum project |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Maslosoft\Addendum\Utilities; |
|
16
|
|
|
|
|
17
|
|
|
use function array_key_exists; |
|
18
|
|
|
use function array_unique; |
|
19
|
|
|
use Exception; |
|
20
|
|
|
use function get_class; |
|
21
|
|
|
use function get_parent_class; |
|
22
|
|
|
use function is_object; |
|
23
|
|
|
use ReflectionClass; |
|
24
|
|
|
use function sort; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* ClassChecker |
|
28
|
|
|
* |
|
29
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
30
|
|
|
*/ |
|
31
|
|
|
class ClassChecker |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Array with existent classes |
|
36
|
|
|
* @var string[] |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $_exists = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Partials cache |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $partials = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Check whenever class is anonymous. |
|
48
|
|
|
* @param string|object $class |
|
49
|
|
|
* @return bool True if class is anonymous |
|
50
|
|
|
*/ |
|
51
|
75 |
|
public static function isAnonymous($class) |
|
52
|
|
|
{ |
|
53
|
75 |
|
if(is_object($class)) |
|
54
|
|
|
{ |
|
55
|
|
|
$class = get_class($class); |
|
56
|
|
|
} |
|
57
|
75 |
|
return strpos($class, 'class@anonymous') !== false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Check whenever class or trait or interface exists. |
|
62
|
|
|
* It does autoload if needed. |
|
63
|
|
|
* @param string $class |
|
64
|
|
|
* @return bool True if class or trait or interface exists |
|
65
|
|
|
*/ |
|
66
|
73 |
|
public static function exists($class) |
|
67
|
|
|
{ |
|
68
|
73 |
|
if(is_object($class)) |
|
69
|
|
|
{ |
|
70
|
|
|
$class = get_class($class); |
|
71
|
|
|
} |
|
72
|
73 |
|
if (Blacklister::ignores($class)) |
|
73
|
|
|
{ |
|
74
|
9 |
|
return false; |
|
75
|
|
|
} |
|
76
|
73 |
|
if (self::isConfirmed($class)) |
|
77
|
|
|
{ |
|
78
|
72 |
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
try |
|
81
|
|
|
{ |
|
82
|
43 |
|
if (@class_exists($class)) |
|
83
|
|
|
{ |
|
84
|
43 |
|
return self::confirm($class); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
catch (Exception $ex) |
|
88
|
|
|
{ |
|
89
|
|
|
// Some class loaders throw exception if class not found |
|
90
|
|
|
} |
|
91
|
|
|
try |
|
92
|
|
|
{ |
|
93
|
27 |
|
if (@trait_exists($class)) |
|
94
|
|
|
{ |
|
95
|
27 |
|
return self::confirm($class); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
catch (Exception $ex) |
|
99
|
|
|
{ |
|
100
|
|
|
// Some class loaders throw exception if class not found |
|
101
|
|
|
} |
|
102
|
|
|
try |
|
103
|
|
|
{ |
|
104
|
23 |
|
if (@interface_exists($class)) |
|
105
|
|
|
{ |
|
106
|
23 |
|
return self::confirm($class); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
catch (Exception $ex) |
|
110
|
|
|
{ |
|
111
|
|
|
// Some class loaders throw exception if class not found |
|
112
|
|
|
} |
|
113
|
22 |
|
Blacklister::ignore($class); |
|
114
|
22 |
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get class/interface/trait names from which class is composed. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $className |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
35 |
|
public static function getPartials($className) |
|
124
|
|
|
{ |
|
125
|
35 |
|
if (array_key_exists($className, self::$partials)) |
|
126
|
|
|
{ |
|
127
|
|
|
return self::$partials[$className]; |
|
128
|
|
|
} |
|
129
|
35 |
|
if(!ClassChecker::exists($className)) |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
1 |
|
self::$partials[$className] = []; |
|
132
|
1 |
|
return []; |
|
133
|
|
|
} |
|
134
|
34 |
|
$partials = []; |
|
135
|
|
|
// Iterate over traits |
|
136
|
34 |
|
foreach ((new ReflectionClass($className))->getTraitNames() as $trait) |
|
137
|
|
|
{ |
|
138
|
9 |
|
$partials[] = $trait; |
|
139
|
9 |
|
foreach(self::getPartials($trait) as $traitPart) |
|
140
|
|
|
{ |
|
141
|
9 |
|
$partials[] = $traitPart; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// Iterate over interfaces to get partials |
|
146
|
34 |
|
foreach ((new ReflectionClass($className))->getInterfaceNames() as $interface) |
|
147
|
|
|
{ |
|
148
|
34 |
|
$partials[] = $interface; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// Iterate over parent classes |
|
152
|
|
|
do |
|
153
|
|
|
{ |
|
154
|
34 |
|
$partials[] = $className; |
|
155
|
|
|
|
|
156
|
|
|
// Iterate over traits of parent class |
|
157
|
34 |
|
foreach ((new ReflectionClass($className))->getTraitNames() as $trait) |
|
158
|
|
|
{ |
|
159
|
9 |
|
$partials[] = $trait; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// Iterate over interfaces of parent class |
|
163
|
34 |
|
foreach ((new ReflectionClass($className))->getInterfaceNames() as $interface) |
|
164
|
|
|
{ |
|
165
|
34 |
|
$partials[] = $interface; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
} |
|
169
|
34 |
|
while (($className = get_parent_class($className)) !== false); |
|
170
|
34 |
|
$partials = array_unique($partials); |
|
171
|
34 |
|
sort($partials); |
|
172
|
34 |
|
self::$partials[$className] = $partials; |
|
173
|
34 |
|
return $partials; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
73 |
|
private static function isConfirmed($class) |
|
177
|
|
|
{ |
|
178
|
73 |
|
return isset(self::$_exists[$class]); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
40 |
|
private static function confirm($class) |
|
182
|
|
|
{ |
|
183
|
40 |
|
self::$_exists[$class] = true; |
|
184
|
40 |
|
return true; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.