Passed
Pull Request — master (#7)
by Luis
20:14 queued 05:16
created

TypeDeclaration::fromNullable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 7.2
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Code\Variables;
9
10
use PhUml\Code\Name;
11
use PhUml\Code\Named;
12
use PhUml\Code\WithName;
13
14
/**
15
 * It represents a variable's type declaration
16
 */
17
final class TypeDeclaration implements Named
18
{
19
    use WithName;
20
21
    /** @var string[] All valid types for PHP 7.1, pseudo-types, and aliases */
22
    private static $builtInTypes = [
23
        'int', 'bool', 'string', 'array', 'float', 'callable', 'iterable',
24
        // pseudo-types
25
        'mixed', 'number', 'object', 'resource', 'self',
26
        // aliases
27
        'boolean', 'integer', 'double',
28
    ];
29
30 213
    /** @var bool */
31
    private $isNullable;
32 213
33
    public static function absent(): TypeDeclaration
34
    {
35 249
        return new TypeDeclaration(null);
36
    }
37 249
38
    public static function from(?string $type): TypeDeclaration
39
    {
40 183
        return new TypeDeclaration($type);
41
    }
42 183
43
    public static function fromNullable(string $type): TypeDeclaration
44
    {
45
        return new TypeDeclaration($type, true);
46
    }
47
48
    public function isPresent(): bool
49
    {
50
        return $this->name !== null;
51 117
    }
52
53 117
    /**
54 117
     * It helps building the relationships between classes/interfaces since built-in
55 27
     * types are not part of a UML class diagram
56
     *
57 117
     * @see \PhUml\Code\Variables\WithTypeDeclaration::isAReference() for more details
58
     */
59
    public function isBuiltIn(): bool
60 117
    {
61
        $type = (string) $this->name;
62 117
        if ($this->isArray()) {
63
            $type = $this->removeArraySuffix();
64
        }
65 27
        return $this->isPresent() && \in_array($type, self::$builtInTypes, true);
66
    }
67 27
68
    public function isArray(): bool
69
    {
70 318
        return strpos((string) $this->name, '[]') === \strlen((string) $this->name) - 2;
71
    }
72 318
73 318
    public function isNullable(): bool
74
    {
75 93
        return $this->isNullable;
76
    }
77 93
78
    public function removeArraySuffix(): string
79
    {
80
        return substr((string) $this->name, 0, -2);
81
    }
82
83
    public function __toString()
84
    {
85
        return ($this->isNullable ? '?' : '') . $this->name;
86
    }
87
88
    private function __construct(?string $name, bool $isNullable = false)
89
    {
90
        $this->name = $name !== null ? Name::from($name) : null;
91
        $this->isNullable = $name !== null && $isNullable;
92
    }
93
}
94