EvaledCodeSourceLocator::createLocatedSource()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 8
cp 0.875
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0175
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BetterReflection\SourceLocator\Type;
6
7
use InvalidArgumentException;
8
use ReflectionClass;
9
use Roave\BetterReflection\Identifier\Identifier;
10
use Roave\BetterReflection\SourceLocator\Ast\Locator;
11
use Roave\BetterReflection\SourceLocator\Exception\InvalidFileLocation;
12
use Roave\BetterReflection\SourceLocator\Located\EvaledLocatedSource;
13
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;
14
use Roave\BetterReflection\SourceLocator\SourceStubber\SourceStubber;
15
use function class_exists;
16
use function file_exists;
17
use function interface_exists;
18
use function trait_exists;
19
20
final class EvaledCodeSourceLocator extends AbstractSourceLocator
21
{
22
    /** @var SourceStubber */
23
    private $stubber;
24
25 7
    public function __construct(Locator $astLocator, SourceStubber $stubber)
26
    {
27 7
        parent::__construct($astLocator);
28
29 7
        $this->stubber = $stubber;
30 7
    }
31
32
    /**
33
     * {@inheritDoc}
34
     *
35
     * @throws InvalidArgumentException
36
     * @throws InvalidFileLocation
37
     */
38 7
    protected function createLocatedSource(Identifier $identifier) : ?LocatedSource
39
    {
40 7
        $classReflection = $this->getInternalReflectionClass($identifier);
41
42 7
        if ($classReflection === null) {
43 3
            return null;
44
        }
45
46 4
        $stubData = $this->stubber->generateClassStub($classReflection->getName());
0 ignored issues
show
Bug introduced by Jaroslav Hanslík
Consider using $classReflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
47
48 4
        if ($stubData === null) {
49
            return null;
50
        }
51
52 4
        return new EvaledLocatedSource($stubData->getStub());
53
    }
54
55 7
    private function getInternalReflectionClass(Identifier $identifier) : ?ReflectionClass
56
    {
57 7
        if (! $identifier->isClass()) {
58 1
            return null;
59
        }
60
61 6
        $name = $identifier->getName();
62
63 6
        if (! (class_exists($name, false) || interface_exists($name, false) || trait_exists($name, false))) {
64 1
            return null; // not an available internal class
65
        }
66
67 5
        $reflection = new ReflectionClass($name);
68 5
        $sourceFile = $reflection->getFileName();
69
70 5
        return $sourceFile && file_exists($sourceFile)
71 5
            ? null : $reflection;
72
    }
73
}
74