Skip to content

Commit

Permalink
Added AggregateSourceStubber
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Sep 22, 2017
1 parent e6f1224 commit 76dcab5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/BetterReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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;
Expand Down Expand Up @@ -107,6 +109,9 @@ public function findReflectionsOnLine() : FindReflectionOnLine
public function sourceStubber() : SourceStubber
{
return $this->sourceStubber
?? $this->sourceStubber = new ReflectionSourceStubber();
?? $this->sourceStubber = new AggregateSourceStubber([
new PhpStormStubsSourceStubber($this->phpParser()),
new ReflectionSourceStubber(),
]);
}
}
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;

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

/**
* @param SourceStubber[] $sourceStubbers
*/
public function __construct(array $sourceStubbers = [])
{
// This slightly confusing code simply type-checks the $sourceStubbers
// array by unpacking them and splatting them in the closure.
$validator = function (SourceStubber ...$sourceLocator) : array {
return $sourceLocator;
};
$this->sourceStubbers = $validator(...$sourceStubbers);
}

public function getClassStub(CoreReflectionClass $classReflection) : ?string
{
foreach ($this->sourceStubbers as $sourceStubber) {
$stub = $sourceStubber->getClassStub($classReflection);
if (null !== $stub) {
return $stub;
}
}

return null;
}

public function getFunctionStub(CoreReflectionFunction $functionReflection) : ?string
{
foreach ($this->sourceStubbers as $sourceStubber) {
$stub = $sourceStubber->getFunctionStub($functionReflection);
if (null !== $stub) {
return $stub;
}
}

return null;
}
}

0 comments on commit 76dcab5

Please sign in to comment.