Skip to content

Commit

Permalink
Merge pull request #373 from kukulich/source-stubber
Browse files Browse the repository at this point in the history
Source stubber based on `jetbrains/phpstorm-stubs`
  • Loading branch information
Ocramius authored Apr 20, 2019
2 parents 1fb8003 + de580a8 commit df78b55
Show file tree
Hide file tree
Showing 110 changed files with 882 additions and 8,439 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"license": "MIT",
"require": {
"php": ">=7.1.0,<7.4.0",
"jetbrains/phpstorm-stubs": "2019.1",
"nikic/php-parser": "^4.0.4",
"phpdocumentor/reflection-docblock": "^4.1.1",
"phpdocumentor/type-resolver": "^0.4.0",
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ parameters:
# Some parent constructors are explicitly to be ignored
- '#does not call parent constructor#'
- '#Access to an undefined property PhpParser\\Node\\Param::\$isOptional#'
# Impossible to define type hint for anonymous class
- '#Call to an undefined method PhpParser\\NodeVisitorAbstract::getNode\(\)#'
23 changes: 20 additions & 3 deletions src/BetterReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use Roave\BetterReflection\Reflector\FunctionReflector;
use Roave\BetterReflection\SourceLocator\Ast\Locator as AstLocator;
use Roave\BetterReflection\SourceLocator\Ast\Parser\MemoizingParser;
use Roave\BetterReflection\SourceLocator\SourceStubber\AggregateSourceStubber;
use Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber;
use Roave\BetterReflection\SourceLocator\SourceStubber\ReflectionSourceStubber;
use Roave\BetterReflection\SourceLocator\SourceStubber\SourceStubber;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\AutoloadSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\EvaledCodeSourceLocator;
Expand Down Expand Up @@ -39,14 +43,18 @@ final class BetterReflection
/** @var FindReflectionOnLine|null */
private $findReflectionOnLine;

/** @var SourceStubber */
private $sourceStubber;

public function sourceLocator() : SourceLocator
{
$astLocator = $this->astLocator();
$astLocator = $this->astLocator();
$sourceStubber = $this->sourceStubber();

return $this->sourceLocator
?? $this->sourceLocator = new MemoizingSourceLocator(new AggregateSourceLocator([
new PhpInternalSourceLocator($astLocator),
new EvaledCodeSourceLocator($astLocator),
new PhpInternalSourceLocator($astLocator, $sourceStubber),
new EvaledCodeSourceLocator($astLocator, $sourceStubber),
new AutoloadSourceLocator($astLocator),
]));
}
Expand Down Expand Up @@ -84,4 +92,13 @@ public function findReflectionsOnLine() : FindReflectionOnLine
return $this->findReflectionOnLine
?? $this->findReflectionOnLine = new FindReflectionOnLine($this->sourceLocator(), $this->astLocator());
}

public function sourceStubber() : SourceStubber
{
return $this->sourceStubber
?? $this->sourceStubber = new AggregateSourceStubber(
new PhpStormStubsSourceStubber($this->phpParser()),
new ReflectionSourceStubber()
);
}
}
10 changes: 0 additions & 10 deletions src/Reflection/ReflectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,6 @@ public function isDisabled() : bool
return false;
}

/**
* Note - we cannot reflect on internal functions (as there is no PHP source
* code we can access. This means, at present, we can only EVER return null
* from this function.
*/
public function getExtensionName() : ?string
{
return null;
}

/**
* @throws NotImplemented
* @throws FunctionDoesNotExist
Expand Down
16 changes: 6 additions & 10 deletions src/Reflection/ReflectionFunctionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,9 @@ public function isDeprecated() : bool
return false;
}

/**
* Is this an internal function?
*
* Note - we cannot reflect on internal functions (as there is no PHP source
* code we can access. This means, at present, we can only EVER return false
* from this function.
*
* @see https://github.com/Roave/BetterReflection/issues/38
*/
public function isInternal() : bool
{
return false;
return $this->locatedSource->isInternal();
}

/**
Expand All @@ -295,6 +286,11 @@ public function isUserDefined() : bool
return ! $this->isInternal();
}

public function getExtensionName() : ?string
{
return $this->locatedSource->getExtensionName();
}

/**
* Check if the function has a variadic parameter.
*/
Expand Down
5 changes: 0 additions & 5 deletions src/Reflection/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,6 @@ public function getImplementingClass() : ReflectionClass
return $this->implementingClass;
}

public function getExtensionName() : ?string
{
return $this->getDeclaringClass()->getExtensionName();
}

public function isInternal() : bool
{
return $this->declaringClass->getLocatedSource()->isInternal();
Expand Down
52 changes: 52 additions & 0 deletions src/SourceLocator/SourceStubber/AggregateSourceStubber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Roave\BetterReflection\SourceLocator\SourceStubber;

use ReflectionClass as CoreReflectionClass;
use ReflectionFunction as CoreReflectionFunction;
use function array_merge;

class AggregateSourceStubber implements SourceStubber
{
/** @var SourceStubber[] */
private $sourceStubbers;

public function __construct(SourceStubber $sourceStubber, SourceStubber ...$otherSourceStubbers)
{
$this->sourceStubbers = array_merge([$sourceStubber], $otherSourceStubbers);
}

/**
* {@inheritDoc}
*/
public function generateClassStub(CoreReflectionClass $classReflection) : ?string
{
foreach ($this->sourceStubbers as $sourceStubber) {
$stub = $sourceStubber->generateClassStub($classReflection);

if ($stub !== null) {
return $stub;
}
}

return null;
}

/**
* {@inheritDoc}
*/
public function generateFunctionStub(CoreReflectionFunction $functionReflection) : ?string
{
foreach ($this->sourceStubbers as $sourceStubber) {
$stub = $sourceStubber->generateFunctionStub($functionReflection);

if ($stub !== null) {
return $stub;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Roave\BetterReflection\SourceLocator\SourceStubber\Exception;

use RuntimeException;

class CouldNotFindPhpStormStubs extends RuntimeException
{
public static function create() : self
{
return new self('Could not find PhpStorm stubs');
}
}
Loading

0 comments on commit df78b55

Please sign in to comment.