1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Marco Muths |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
15
|
|
|
* copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Squeeze; |
27
|
|
|
|
28
|
|
|
use PhpParser\Node; |
29
|
|
|
use PhpParser\NodeVisitorAbstract; |
30
|
|
|
|
31
|
|
|
class Collector extends NodeVisitorAbstract |
32
|
|
|
{ |
33
|
|
|
/** @var array */ |
34
|
|
|
private $classMap = array(); |
35
|
|
|
|
36
|
|
|
/** @var array */ |
37
|
|
|
private $dependencies = array(); |
38
|
|
|
|
39
|
|
|
/** @var Node\Stmt\ClassLike[] */ |
40
|
|
|
private $foundClasses = array(); |
41
|
|
|
|
42
|
|
|
/** @var bool */ |
43
|
|
|
private $hasFoundInvalidStmt = false; |
44
|
|
|
|
45
|
|
|
private $invalidFunctions = array( |
46
|
|
|
'basename', |
47
|
|
|
'chgrp', |
48
|
|
|
'chmod', |
49
|
|
|
'chown', |
50
|
|
|
'clearstatcache', |
51
|
|
|
'copy', |
52
|
|
|
'delete', |
53
|
|
|
'dirname', |
54
|
|
|
'disk_free_space', |
55
|
|
|
'disk_total_space', |
56
|
|
|
'diskfreespace', |
57
|
|
|
'file_exists', |
58
|
|
|
'file_get_contents', |
59
|
|
|
'file_put_contents', |
60
|
|
|
'file', |
61
|
|
|
'fileatime', |
62
|
|
|
'filectime', |
63
|
|
|
'filegroup', |
64
|
|
|
'fileinode', |
65
|
|
|
'filemtime', |
66
|
|
|
'fileowner', |
67
|
|
|
'fileperms', |
68
|
|
|
'filesize', |
69
|
|
|
'filetype', |
70
|
|
|
'fnmatch', |
71
|
|
|
'fopen', |
72
|
|
|
'glob', |
73
|
|
|
'is_dir', |
74
|
|
|
'is_executable', |
75
|
|
|
'is_file', |
76
|
|
|
'is_link', |
77
|
|
|
'is_readable', |
78
|
|
|
'is_uploaded_file', |
79
|
|
|
'is_writable', |
80
|
|
|
'is_writeable', |
81
|
|
|
'lchgrp', |
82
|
|
|
'lchown', |
83
|
|
|
'link', |
84
|
|
|
'linkinfo', |
85
|
|
|
'lstat', |
86
|
|
|
'mkdir', |
87
|
|
|
'move_uploaded_file', |
88
|
|
|
'parse_ini_file', |
89
|
|
|
'parse_ini_string', |
90
|
|
|
'pathinfo', |
91
|
|
|
'readfile', |
92
|
|
|
'readlink', |
93
|
|
|
'realpath', |
94
|
|
|
'rename', |
95
|
|
|
'rmdir', |
96
|
|
|
'stat', |
97
|
|
|
'symlink', |
98
|
|
|
'tempnam', |
99
|
|
|
'touch', |
100
|
|
|
'unlink', |
101
|
|
|
'stream_resolve_include_path', |
102
|
|
|
'stream_is_local', |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
public function leaveNode(Node $node) |
106
|
|
|
{ |
107
|
|
|
if ($node instanceof Node\Stmt\Class_) { |
108
|
|
|
$this->collect(array($node->extends)); |
109
|
|
|
$this->collect($node->implements); |
110
|
|
|
$this->foundClasses[] = $node; |
111
|
|
|
} elseif ($node instanceof Node\Stmt\Interface_) { |
112
|
|
|
$this->collect($node->extends); |
113
|
|
|
$this->foundClasses[] = $node; |
114
|
|
|
} elseif ($node instanceof Node\Stmt\Trait_) { |
115
|
|
|
$this->foundClasses[] = $node; |
116
|
|
|
} elseif ($node instanceof Node\Stmt\TraitUse) { |
117
|
|
|
$this->collect($node->traits); |
118
|
|
|
} elseif ($node instanceof Node\Stmt\Declare_) { |
119
|
|
|
$this->hasFoundInvalidStmt = true; |
120
|
|
|
} elseif ($node instanceof Node\Expr\Include_) { |
121
|
|
|
$this->hasFoundInvalidStmt = true; |
122
|
|
|
} elseif ($node instanceof Node\Expr\FuncCall) { |
123
|
|
|
if (method_exists($node->name, 'toString')) { |
124
|
|
|
$function = $node->name->toString(); |
125
|
|
|
if (in_array($function, $this->invalidFunctions)) { |
126
|
|
|
$this->hasFoundInvalidStmt = true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param Node\Name[]|null $names |
134
|
|
|
*/ |
135
|
|
|
private function collect($names) |
136
|
|
|
{ |
137
|
|
|
if ($names) { |
138
|
|
|
foreach ($names as $name) { |
139
|
|
|
if ($name) { |
140
|
|
|
$name = $name->toString(); |
141
|
|
|
if (false === strpos($name, '_') |
142
|
|
|
&& count(explode("\\", $name)) == 1 |
143
|
|
|
) { |
144
|
|
|
continue; |
145
|
|
|
} |
146
|
|
|
$this->dependencies[$name] = $name; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function reset() |
153
|
|
|
{ |
154
|
|
|
if (count($this->foundClasses) == 1 |
155
|
|
|
&& !$this->hasFoundInvalidStmt |
156
|
|
|
) { |
157
|
|
|
$node = array_shift($this->foundClasses); |
158
|
|
|
$name = $node->namespacedName; |
159
|
|
|
if ($name instanceof Node\Name) { |
160
|
|
|
$name = $name->toString(); |
161
|
|
|
} |
162
|
|
|
$this->classMap[$name] = $this->dependencies; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->dependencies = array(); |
166
|
|
|
$this->foundClasses = array(); |
167
|
|
|
$this->hasFoundInvalidStmt = false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
public function getClassMap() |
174
|
|
|
{ |
175
|
|
|
return $this->classMap; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|