ClassType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 9
dl 0
loc 44
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A typeCheck() 0 3 1
A fromFullName() 0 3 1
A __toString() 0 3 1
A typeFetch() 0 3 1
A __construct() 0 3 1
A requiresImport() 0 3 1
A nameNode() 0 3 1
A fullName() 0 3 1
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