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

ClassDefinition::hasConstructor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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;
9
10
use BadMethodCallException;
11
use PhUml\Code\Attributes\Attribute;
12
use PhUml\Code\Attributes\HasAttributes;
13
use PhUml\Code\Attributes\HasConstants;
14
use PhUml\Code\Attributes\WithAttributes;
15
use PhUml\Code\Attributes\WithConstants;
16
use PhUml\Code\Methods\Method;
17
use PhUml\Code\Modifiers\CanBeAbstract;
18
use PhUml\Code\Modifiers\Visibility;
19
use PhUml\Code\Parameters\Parameter;
20
21
/**
22
 * It represents a class definition
23
 */
24
class ClassDefinition extends Definition implements HasAttributes, HasConstants, CanBeAbstract, UseTraits
25
{
26
    use WithAttributes;
27
    use WithConstants;
28
    use WithTraits;
29
30
    /** @var Name|null */
31
    protected $parent;
32
33
    /** @var Name[] */
34
    private $interfaces;
35
36
    /**
37
     * @param Method[] $methods
38
     * @param \PhUml\Code\Attributes\Constant[] $constants
39 276
     * @param Attribute[] $attributes
40
     * @param Name[] $interfaces
41
     * @param Name[] $traits
42
     */
43
    public function __construct(
44
        Name $name,
45
        array $methods = [],
46
        array $constants = [],
47
        Name $parent = null,
48 276
        array $attributes = [],
49 276
        array $interfaces = [],
50 276
        array $traits = []
51 276
    ) {
52 276
        parent::__construct($name, $methods);
53 276
        $this->constants = $constants;
54 276
        $this->parent = $parent;
55
        $this->attributes = $attributes;
56
        $this->interfaces = $interfaces;
57
        $this->traits = $traits;
58
    }
59
60
    /**
61
     * This method is used by the `AssociationsBuilder` class to discover associations with other
62
     * classes via the constructor
63 78
     *
64
     * @return Parameter[]
65 78
     * @see \PhUml\Graphviz\Builders\AssociationsBuilder::fromAttributes() for more details
66 57
     */
67
    public function constructorParameters(): array
68
    {
69 54
        $constructors = array_filter($this->methods, static function (Method $method): bool {
70 54
            return $method->isConstructor();
71 54
        });
72
        $constructor = reset($constructors);
73 54
74
        return $constructor === false ? [] : $constructor->parameters();
75
    }
76
77
    /**
78
     * This method is used to build the `Summary` of a `Codebase`
79
     *
80
     * @see Summary::attributesSummary() for more details
81
     */
82
    public function countAttributesByVisibility(Visibility $modifier): int
83 12
    {
84 12
        return \count(array_filter($this->attributes, static function (Attribute $attribute) use ($modifier): bool {
85 12
            return $attribute->hasVisibility($modifier);
86
        }));
87
    }
88
89
    /**
90
     * This method is used to build the `Summary` of a `Codebase`
91
     *
92
     * @see Summary::attributesSummary() for more details
93
     */
94
    public function countTypedAttributesByVisibility(Visibility $modifier): int
95 12
    {
96 12
        return \count(array_filter($this->attributes, static function (Attribute $attribute) use ($modifier): bool {
97 12
            return $attribute->hasTypeDeclaration() && $attribute->hasVisibility($modifier);
98
        }));
99
    }
100
101
    /**
102
     * It is used by the `ClassGraphBuilder` to create the edges to represent implementation
103
     * associations
104
     *
105
     * @return Name[]
106
     * @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details
107 159
     */
108
    public function interfaces(): array
109 159
    {
110
        return $this->interfaces;
111
    }
112
113
    /**
114
     * It is used by the `ClassGraphBuilder` to create the edge to represent inheritance
115
     *
116
     * @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details
117 66
     */
118
    public function parent(): Name
119 66
    {
120
        if ($this->parent === null) {
121
            throw new BadMethodCallException('This class does not have a parent class');
122
        }
123
        return $this->parent;
124
    }
125
126
    /**
127
     * It is used by the `ClassGraphBuilder` to determine if an inheritance association should be
128 156
     * created
129
     *
130 156
     * @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details
131
     */
132
    public function hasParent(): bool
133
    {
134
        return $this->parent !== null;
135
    }
136
137
    /**
138
     * This method is used when the commands are called with the option `hide-empty-blocks`
139
     *
140 81
     * It counts both the attributes and the constants of a class
141
     *
142 81
     * @see Definition::hasAttributes() for more details
143
     */
144
    public function hasAttributes(): bool
145
    {
146
        return \count($this->constants) + \count($this->attributes) > 0;
147
    }
148
149
    /**
150 81
     * This method is used to determine if the class name should be shown in italics
151 48
     */
152 81
    public function isAbstract(): bool
153
    {
154
        return \count(array_filter($this->methods(), static function (Method $method): bool {
155
            return $method->isAbstract();
156
        })) > 0;
157 78
    }
158
}
159