Passed
Pull Request — master (#7)
by Luis
20:14 queued 05:16
created

Variable   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 54
ccs 19
cts 19
cp 1
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A arrayTypeName() 0 3 1
A isArray() 0 3 1
A typeName() 0 3 1
A __construct() 0 4 1
A referenceName() 0 7 3
A __toString() 0 6 2
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 7.2
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 BadMethodCallException;
11
use PhUml\Code\Name;
12
13
/**
14
 * It represents a variable declaration
15
 */
16
final class Variable implements HasType
17
{
18
    use WithTypeDeclaration;
19
20
    /** @var string */
21
    protected $name;
22 189
23
    public function __construct(string $name, TypeDeclaration $type)
24 189
    {
25 189
        $this->name = $name;
26 189
        $this->type = $type;
27
    }
28 117
29
    /**
30 117
     * References to arrays need to have the `[]` removed from their names in order to create
31
     * external definitions with a proper name
32
     *
33 57
     * The edges created from these references need to map to the names without the suffix
34
     *
35 57
     * @see \PhUml\Parser\Code\ExternalAssociationsResolver::resolveExternalAttributes()
36 57
     * @see \PhUml\Parser\Code\ExternalAssociationsResolver::resolveExternalConstructorParameters()
37 57
     * @see \PhUml\Graphviz\Builders\EdgesBuilder::addAssociation()
38 57
     */
39
    public function referenceName(): Name
40
    {
41
        $name = $this->isArray() ? $this->arrayTypeName() : $this->typeName();
42
        if ($name === null) {
43
            throw new BadMethodCallException('This attribute is not a reference to a code definition');
44
        }
45
        return $name;
46
    }
47
48
    public function __toString()
49
    {
50
        return sprintf(
51
            '%s%s',
52 57
            $this->name,
53
            $this->type->isPresent() ? ": {$this->type}" : ''
54 57
        );
55
    }
56
57 54
    private function typeName(): ?Name
58
    {
59 54
        return $this->type->name();
60
    }
61
62 57
    private function isArray(): bool
63
    {
64 57
        return $this->type->isArray();
65
    }
66
67 3
    private function arrayTypeName(): Name
68
    {
69 3
        return Name::from($this->type->removeArraySuffix());
70
    }
71
}
72