Skip to content

Commit 9914301

Browse files
committed
OptimizedSingleFileSourceLocator::locateIdentifiersByType() - support constants
1 parent 3961f7c commit 9914301

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Reflection/BetterReflection/SourceLocator/OptimizedSingleFileSourceLocator.php

+44
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,50 @@ public function locateIdentifiersByType(Reflector $reflector, IdentifierType $id
210210
}
211211
}
212212

213+
if ($identifierType->isConstant()) {
214+
$constantNodes = $fetchedNodesResult->getConstantNodes();
215+
foreach ($constantNodes as $constantNodesArray) {
216+
foreach ($constantNodesArray as $fetchedConstantNode) {
217+
$constantNode = $fetchedConstantNode->getNode();
218+
219+
if ($constantNode instanceof Const_) {
220+
foreach ($constantNode->consts as $constPosition => $const) {
221+
if ($const->namespacedName === null) {
222+
throw new ShouldNotHappenException();
223+
}
224+
225+
$constantReflection = $nodeToReflection->__invoke(
226+
$reflector,
227+
$constantNode,
228+
$fetchedConstantNode->getLocatedSource(),
229+
$fetchedConstantNode->getNamespace(),
230+
$constPosition,
231+
);
232+
if (!$constantReflection instanceof ReflectionConstant) {
233+
throw new ShouldNotHappenException();
234+
}
235+
236+
$reflections[] = $constantReflection;
237+
}
238+
239+
continue;
240+
}
241+
242+
$constantReflection = $nodeToReflection->__invoke(
243+
$reflector,
244+
$constantNode,
245+
$fetchedConstantNode->getLocatedSource(),
246+
$fetchedConstantNode->getNamespace(),
247+
);
248+
if (!$constantReflection instanceof ReflectionConstant) {
249+
throw new ShouldNotHappenException();
250+
}
251+
252+
$reflections[] = $constantReflection;
253+
}
254+
}
255+
}
256+
213257
return $reflections;
214258
}
215259

tests/PHPStan/Reflection/BetterReflection/SourceLocator/OptimizedSingleFileSourceLocatorTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ public function dataForIdenifiersByType(): iterable
101101
],
102102
'constants' => [
103103
new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT),
104-
[],
104+
[
105+
'ANOTHER_NAME',
106+
'ConstFile\ANOTHER_NAME',
107+
'ConstFile\TABLE_NAME',
108+
'OPTIMIZED_SFSL_OBJECT_CONSTANT',
109+
'const_with_dir_const',
110+
],
105111
__DIR__ . '/data/const.php',
106112
],
107113
];

0 commit comments

Comments
 (0)