Completed
Push — master ( 2ce63d...389baa )
by Luis
11:05 queued 03:24
created

TypeDeclaration   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A from() 0 3 1
A absent() 0 3 1
A isPresent() 0 3 1
A isBuiltIn() 0 7 3
A __toString() 0 3 1
A isArray() 0 3 1
A removeArraySuffix() 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;
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 219
    public function __construct(?string $name)
24
    {
25 219
        $this->name = $name;
26 219
    }
27
28 165
    public static function absent(): TypeDeclaration
29
    {
30 165
        return new TypeDeclaration(null);
31
    }
32
33 159
    public static function from(?string $text): TypeDeclaration
34
    {
35 159
        return new TypeDeclaration($text);
36
    }
37
38 120
    public function isPresent(): bool
39
    {
40 120
        return null !== $this->name;
41
    }
42
43
    /**
44
     * This will help when building the relationships between classes/interfaces since built-in
45
     * types are not part of a UML class diagram
46
     */
47 48
    public function isBuiltIn(): bool
48
    {
49 48
        $type = $this->name;
50 48
        if ($this->isArray()) {
51 6
            $type = $this->removeArraySuffix();
52
        }
53 48
        return $this->isPresent() && \in_array($type, self::$builtInTypes, true);
54
    }
55
56 48
    private function isArray(): bool
57
    {
58 48
        return strpos($this->name, '[]') === \strlen($this->name) - 2;
59
    }
60
61 6
    private function removeArraySuffix(): string
62
    {
63 6
        return substr($this->name, 0, -2);
64
    }
65
66 66
    public function __toString()
67
    {
68 66
        return (string)$this->name;
69
    }
70
}
71