Passed
Pull Request — master (#8)
by Luis
28:30 queued 13:28
created

TypeDeclaration   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 79
ccs 20
cts 20
cp 1
rs 10
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNullable() 0 3 1
A removeArraySuffix() 0 3 1
A isBuiltInArray() 0 3 1
A isArray() 0 3 1
A from() 0 3 1
A __toString() 0 3 2
A __construct() 0 4 3
A isPresent() 0 3 1
A isNullable() 0 3 1
A isBuiltIn() 0 7 3
A absent() 0 3 1
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 7.4
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 array $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
    private bool $isNullable;
31
32 213
    public static function absent(): TypeDeclaration
33
    {
34
        return new TypeDeclaration(null);
35 249
    }
36
37 249
    public static function from(?string $type): TypeDeclaration
38
    {
39
        return new TypeDeclaration($type);
40 183
    }
41
42 183
    public static function fromNullable(string $type): TypeDeclaration
43
    {
44
        return new TypeDeclaration($type, true);
45
    }
46
47
    public function isPresent(): bool
48
    {
49
        return $this->name !== null;
50
    }
51 117
52
    /**
53 117
     * It helps building the relationships between classes/interfaces since built-in
54 117
     * types are not part of a UML class diagram
55 27
     *
56
     * @see \PhUml\Code\Variables\WithTypeDeclaration::isAReference() for more details
57 117
     */
58
    public function isBuiltIn(): bool
59
    {
60 117
        $type = (string) $this->name;
61
        if ($this->isArray()) {
62 117
            $type = $this->removeArraySuffix();
63
        }
64
        return $this->isPresent() && \in_array($type, self::$builtInTypes, true);
65 27
    }
66
67 27
    public function isBuiltInArray(): bool
68
    {
69
        return (string) $this->name === 'array';
70 318
    }
71
72 318
    public function isArray(): bool
73 318
    {
74
        return strpos((string) $this->name, '[]') === \strlen((string) $this->name) - 2;
75 93
    }
76
77 93
    public function isNullable(): bool
78
    {
79
        return $this->isNullable;
80
    }
81
82
    public function removeArraySuffix(): string
83
    {
84
        return substr((string) $this->name, 0, -2);
85
    }
86
87
    public function __toString()
88
    {
89
        return ($this->isNullable ? '?' : '') . $this->name;
90
    }
91
92
    private function __construct(?string $name, bool $isNullable = false)
93
    {
94
        $this->name = $name !== null ? new Name($name) : null;
95
        $this->isNullable = $name !== null && $isNullable;
96
    }
97
}
98