Skip to content

Commit

Permalink
Merge pull request #268 from Roave/handle-function-reflection-filenam…
Browse files Browse the repository at this point in the history
…e-false

Ensure locateFunctionByName returns ?string always
  • Loading branch information
Ocramius authored Apr 18, 2017
2 parents ef87705 + dcf2d0a commit a95cb4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/SourceLocator/Type/AutoloadSourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected function createLocatedSource(Identifier $identifier) : ?LocatedSource
*
* @param Identifier $identifier
* @return string|null
* @throws \ReflectionException
*/
private function attemptAutoloadForIdentifier(Identifier $identifier) : ?string
{
Expand Down Expand Up @@ -74,6 +75,7 @@ private function attemptAutoloadForIdentifier(Identifier $identifier) : ?string
*
* @param string $className
* @return string|null
* @throws \ReflectionException
*/
private function locateClassByName(string $className) : ?string
{
Expand Down Expand Up @@ -105,6 +107,7 @@ class_exists($className);
*
* @param string $functionName
* @return string|null
* @throws \ReflectionException
*/
private function locateFunctionByName(string $functionName) : ?string
{
Expand All @@ -113,7 +116,13 @@ private function locateFunctionByName(string $functionName) : ?string
}

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

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

return $reflectionFileName;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/unit/SourceLocator/Type/AutoloadSourceLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,15 @@ public function testShouldNotConsiderEvaledSources() : void
->locateIdentifier($this->getMockReflector(), new Identifier($className, new IdentifierType(IdentifierType::IDENTIFIER_CLASS)))
);
}

public function testReturnsNullWithInternalFunctions() : void
{
self::assertNull(
(new AutoloadSourceLocator())
->locateIdentifier(
$this->getMockReflector(),
new Identifier('strlen', new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION))
)
);
}
}

0 comments on commit a95cb4e

Please sign in to comment.