1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Tokenizer; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerAwareInterface; |
8
|
|
|
use Spiral\Core\Container\InjectableInterface; |
9
|
|
|
use Spiral\Logger\Traits\LoggerTrait; |
10
|
|
|
use Spiral\Tokenizer\Exception\LocatorException; |
11
|
|
|
use Spiral\Tokenizer\Reflection\ReflectionFile; |
12
|
|
|
use Spiral\Tokenizer\Traits\TargetTrait; |
13
|
|
|
use Symfony\Component\Finder\Finder; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Base class for Class and Invocation locators. |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractLocator implements InjectableInterface, LoggerAwareInterface |
19
|
|
|
{ |
20
|
|
|
use LoggerTrait; |
21
|
|
|
use TargetTrait; |
22
|
|
|
|
23
|
|
|
public const INJECTOR = Tokenizer::class; |
24
|
|
|
|
25
|
396 |
|
public function __construct( |
26
|
|
|
protected Finder $finder, |
27
|
|
|
protected readonly bool $debug = false, |
28
|
|
|
) { |
29
|
396 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Available file reflections. Generator. |
33
|
|
|
* |
34
|
|
|
* @throws \Exception |
35
|
|
|
* |
36
|
|
|
* @return \Generator<int, ReflectionFile, mixed, void> |
37
|
|
|
*/ |
38
|
392 |
|
protected function availableReflections(): \Generator |
39
|
|
|
{ |
40
|
392 |
|
foreach ($this->finder->getIterator() as $file) { |
41
|
392 |
|
$reflection = new ReflectionFile((string)$file); |
42
|
|
|
|
43
|
392 |
|
if ($reflection->hasIncludes()) { |
44
|
|
|
// We are not analyzing files which has includes, it's not safe to require such reflections |
45
|
30 |
|
$this->getLogger()->warning( |
46
|
30 |
|
\sprintf('File `%s` has includes and excluded from analysis', (string) $file), |
47
|
30 |
|
['file' => $file] |
48
|
30 |
|
); |
49
|
|
|
|
50
|
30 |
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
392 |
|
yield $reflection; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Safely get class reflection, class loading errors will be blocked and reflection will be |
59
|
|
|
* excluded from analysis. |
60
|
|
|
* |
61
|
|
|
* @template T |
62
|
|
|
* @param class-string<T> $class |
|
|
|
|
63
|
|
|
* @return \ReflectionClass<T> |
64
|
|
|
* |
65
|
|
|
* @throws LocatorException |
66
|
|
|
*/ |
67
|
385 |
|
protected function classReflection(string $class): \ReflectionClass |
68
|
|
|
{ |
69
|
385 |
|
$loader = static function ($class) { |
70
|
303 |
|
if ($class === LocatorException::class) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
303 |
|
throw new LocatorException(\sprintf("Class '%s' can not be loaded", $class)); |
75
|
385 |
|
}; |
76
|
|
|
|
77
|
|
|
//To suspend class dependency exception |
78
|
385 |
|
\spl_autoload_register($loader); |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
//In some cases reflection can thrown an exception if class invalid or can not be loaded, |
82
|
|
|
//we are going to handle such exception and convert it soft exception |
83
|
385 |
|
return new \ReflectionClass($class); |
84
|
303 |
|
} catch (\Throwable $e) { |
85
|
303 |
|
if ($e instanceof LocatorException && $e->getPrevious() != null) { |
86
|
|
|
$e = $e->getPrevious(); |
87
|
|
|
} |
88
|
|
|
|
89
|
303 |
|
$this->getLogger()->error( |
90
|
303 |
|
\sprintf( |
91
|
303 |
|
'%s: %s in %s:%s', |
92
|
303 |
|
$class, |
93
|
303 |
|
$e->getMessage(), |
94
|
303 |
|
$e->getFile(), |
95
|
303 |
|
$e->getLine() |
96
|
303 |
|
), |
97
|
303 |
|
['error' => $e] |
98
|
303 |
|
); |
99
|
|
|
|
100
|
303 |
|
throw new LocatorException($e->getMessage(), (int) $e->getCode(), $e); |
101
|
|
|
} finally { |
102
|
385 |
|
\spl_autoload_unregister($loader); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Safely get enum reflection, class loading errors will be blocked and reflection will be |
108
|
|
|
* excluded from analysis. |
109
|
|
|
* |
110
|
|
|
* @param class-string $enum |
|
|
|
|
111
|
|
|
* |
112
|
|
|
* @return \ReflectionEnum |
113
|
|
|
* |
114
|
|
|
* @throws LocatorException |
115
|
|
|
*/ |
116
|
7 |
|
protected function enumReflection(string $enum): \ReflectionEnum |
|
|
|
|
117
|
|
|
{ |
118
|
7 |
|
$loader = static function (string $enum): void { |
119
|
6 |
|
if ($enum === LocatorException::class) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
6 |
|
throw new LocatorException(\sprintf("Enum '%s' can not be loaded", $enum)); |
124
|
7 |
|
}; |
125
|
|
|
|
126
|
|
|
//To suspend class dependency exception |
127
|
7 |
|
\spl_autoload_register($loader); |
128
|
|
|
|
129
|
|
|
try { |
130
|
|
|
//In some enum reflection can thrown an exception if enum invalid or can not be loaded, |
131
|
|
|
//we are going to handle such exception and convert it soft exception |
132
|
7 |
|
return new \ReflectionEnum($enum); |
133
|
6 |
|
} catch (\Throwable $e) { |
134
|
6 |
|
if ($e instanceof LocatorException && $e->getPrevious() != null) { |
135
|
|
|
$e = $e->getPrevious(); |
136
|
|
|
} |
137
|
|
|
|
138
|
6 |
|
$this->getLogger()->error( |
139
|
6 |
|
\sprintf( |
140
|
6 |
|
'%s: %s in %s:%s', |
141
|
6 |
|
$enum, |
142
|
6 |
|
$e->getMessage(), |
143
|
6 |
|
$e->getFile(), |
144
|
6 |
|
$e->getLine() |
145
|
6 |
|
), |
146
|
6 |
|
['error' => $e] |
147
|
6 |
|
); |
148
|
|
|
|
149
|
6 |
|
throw new LocatorException($e->getMessage(), (int) $e->getCode(), $e); |
150
|
|
|
} finally { |
151
|
7 |
|
\spl_autoload_unregister($loader); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths