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

TypeDeclaration::isArray()   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;
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