Skip to content

Commit

Permalink
Added AggregateSourceStubber
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed May 23, 2018
1 parent 1799e2e commit 9149d75
Show file tree
Hide file tree
Showing 2 changed files with 52 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 @@ -11,6 +11,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 @@ -94,6 +96,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()
);
}
}
46 changes: 46 additions & 0 deletions src/SourceLocator/SourceStubber/AggregateSourceStubber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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);
}

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

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

return null;
}

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

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

return null;
}
}

0 comments on commit 9149d75

Please sign in to comment.