Completed
Push — master ( 389baa...c7c5a4 )
by Luis
14:17 queued 08:06
created

TypeDeclaration::absent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.1
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
/**
11
 * It represents a variable's type declaration
12
 */
13
class TypeDeclaration
14
{
15
    /** @var string[] All valid types for PHP 7.1 */
16
    private static $builtInTypes = [
17
        'int', 'bool', 'string', 'array', 'float', 'callable', 'iterable'
18
    ];
19
20
    /** @var string */
21
    private $name;
22
23 168
    public static function absent(): TypeDeclaration
24
    {
25 168
        return new TypeDeclaration(null);
26
    }
27
28 171
    public static function from(?string $text): TypeDeclaration
29
    {
30 171
        return new TypeDeclaration($text);
31
    }
32
33 132
    public function isPresent(): bool
34
    {
35 132
        return null !== $this->name;
36
    }
37
38
    /**
39
     * It helps building the relationships between classes/interfaces since built-in
40
     * types are not part of a UML class diagram
41
     *
42
     * @see \PhUml\Code\Variables\WithTypeDeclaration::isAReference() for more details
43
     */
44 75
    public function isBuiltIn(): bool
45
    {
46 75
        $type = $this->name;
47 75
        if ($this->isArray()) {
48 24
            $type = $this->removeArraySuffix();
49
        }
50 75
        return $this->isPresent() && \in_array($type, self::$builtInTypes, true);
51
    }
52
53 75
    private function isArray(): bool
54
    {
55 75
        return strpos($this->name, '[]') === \strlen($this->name) - 2;
56
    }
57
58 24
    private function removeArraySuffix(): string
59
    {
60 24
        return substr($this->name, 0, -2);
61
    }
62
63 228
    private function __construct(?string $name)
64
    {
65 228
        $this->name = $name;
66 228
    }
67
68 78
    public function __toString()
69
    {
70 78
        return (string)$this->name;
71
    }
72
}
73