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

TypeDeclaration   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A removeArraySuffix() 0 3 1
A isArray() 0 3 1
A from() 0 3 1
A __toString() 0 3 1
A __construct() 0 3 1
A isPresent() 0 3 1
A isBuiltIn() 0 7 3
A absent() 0 3 1
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