EvaledCodeSourceLocator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 54
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createLocatedSource() 0 16 3
B getInternalReflectionClass() 0 18 7
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