Skip to content

Commit

Permalink
AutoloadSourceLocator for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 17, 2020
1 parent f709189 commit bd57a87
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPStan\File\FileReader;
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use Roave\BetterReflection\Identifier\Identifier;
use Roave\BetterReflection\Identifier\IdentifierType;
use Roave\BetterReflection\Reflection\Reflection;
Expand Down Expand Up @@ -54,6 +55,22 @@ public function __construct(?AstLocator $astLocator = null)
*/
public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
if ($identifier->isFunction()) {
$functionName = $identifier->getName();
if (!function_exists($functionName)) {
return null;
}

$reflection = new ReflectionFunction($functionName);
$reflectionFileName = $reflection->getFileName();

if (!is_string($reflectionFileName)) {
return null;
}

return $this->findReflection($reflector, $reflectionFileName, $identifier);
}

if (!$identifier->isClass()) {
return null;
}
Expand All @@ -64,19 +81,24 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
}
[$potentiallyLocatedFile, $className] = $locateResult;

return $this->findReflection($reflector, $potentiallyLocatedFile, new Identifier($className, $identifier->getType()));
}

private function findReflection(Reflector $reflector, string $file, Identifier $identifier): ?Reflection
{
try {
$fileContents = FileReader::read($potentiallyLocatedFile);
$fileContents = FileReader::read($file);
} catch (\PHPStan\File\CouldNotReadFileException $e) {
return null;
}

$locatedSource = new LocatedSource(
$fileContents,
$potentiallyLocatedFile
$file
);

try {
return $this->astLocator->findReflection($reflector, $locatedSource, new Identifier($className, $identifier->getType()));
return $this->astLocator->findReflection($reflector, $locatedSource, $identifier);
} catch (IdentifierNotFound $exception) {
return null;
}
Expand Down

0 comments on commit bd57a87

Please sign in to comment.