Skip to content

Commit

Permalink
Added AggregateSourceStubber
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Apr 19, 2019
1 parent a60156b commit d392fb7
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 @@ -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()
);
}
}
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;
}
}

0 comments on commit d392fb7

Please sign in to comment.