BuiltInType::typeCheck()   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 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PhpGenerics\Generator\Type;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Arg;
7
use PhpParser\Node\Expr;
8
use PhpParser\Node\Expr\FuncCall;
9
use PhpParser\Node\Name;
10
use PhpParser\Node\Scalar\String_;
11
use Stratadox\PhpGenerics\Primitives;
12
13
/**
14
 * @method static TypeArgument string()
15
 * @method static TypeArgument bool()
16
 * @method static TypeArgument int()
17
 * @method static TypeArgument float()
18
 * @method static TypeArgument object()
19
 * @method static TypeArgument array()
20
 * @method static TypeArgument resource()
21
 * @method static TypeArgument void()
22
 */
23
final class BuiltInType implements TypeArgument
24
{
25
    /** @var string */
26
    private $type;
27
    /** @var Name */
28
    private $typeCheck;
29
    /** @var Primitives */
30
    private static $primitives;
31
32
    public function __construct(string $type, Name $typeCheck)
33
    {
34
        $this->type = $type;
35
        $this->typeCheck = $typeCheck;
36
    }
37
38
    public static function __callStatic($name, $arguments)
39
    {
40
        return new self($name, new Name(self::check($name)));
41
    }
42
43
    private static function check(string $type): string
44
    {
45
        if (null === self::$primitives) {
46
            self::$primitives = new Primitives();
47
        }
48
        return self::$primitives->checkFor($type);
49
    }
50
51
    public function typeCheck(Expr $expr): Node
52
    {
53
        return new FuncCall($this->typeCheck, [new Arg($expr)]);
54
    }
55
56
    public function typeFetch(): Node
57
    {
58
        return new String_($this->type);
59
    }
60
61
    public function requiresImport(): bool
62
    {
63
        return false;
64
    }
65
66
    public function fullName(): string
67
    {
68
        return $this->type;
69
    }
70
71
    public function __toString(): string
72
    {
73
        return $this->type;
74
    }
75
}
76