ClassType::typeFetch()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PhpGenerics\Generator\Type;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Expr;
7
use PhpParser\Node\Expr\ClassConstFetch;
8
use PhpParser\Node\Expr\Instanceof_;
9
use PhpParser\Node\Identifier;
10
use PhpParser\Node\Name;
11
use ReflectionClass;
12
use ReflectionException;
13
use function class_exists;
14
15
final class ClassType implements TypeArgument
16
{
17
    /** @var ReflectionClass */
18
    private $reflection;
19
20
    public function __construct(ReflectionClass $reflection)
21
    {
22
        $this->reflection = $reflection;
23
    }
24
25
    /** @throws ReflectionException */
26
    public static function fromFullName(string $fqcn): self
27
    {
28
        return new self(new ReflectionClass($fqcn));
29
    }
30
31
    public function typeCheck(Expr $expr): Node
32
    {
33
        return new Instanceof_($expr, $this->nameNode());
34
    }
35
36
    public function typeFetch(): Node
37
    {
38
        return new ClassConstFetch($this->nameNode(), new Identifier('class'));
39
    }
40
41
    public function requiresImport(): bool
42
    {
43
        return true;
44
    }
45
46
    public function fullName(): string
47
    {
48
        return $this->reflection->getName();
49
    }
50
51
    public function __toString(): string
52
    {
53
        return $this->reflection->getShortName();
54
    }
55
56
    private function nameNode(): Name
57
    {
58
        return new Name((string) $this);
59
    }
60
}
61