Passed
Pull Request — master (#28)
by
unknown
49:49 queued 24:56
created

ClassDefinition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 8
dl 0
loc 15
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
2
/**
3
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
4
 */
5
6
namespace PhUml\Code;
7
8
use BadMethodCallException;
9
use PhUml\Code\Methods\Method;
10
use PhUml\Code\Modifiers\CanBeAbstract;
11
use PhUml\Code\Modifiers\Visibility;
12
use PhUml\Code\Parameters\Parameter;
13
use PhUml\Code\Properties\Constant;
14
use PhUml\Code\Properties\HasConstants;
15
use PhUml\Code\Properties\HasProperties;
16
use PhUml\Code\Properties\Property;
17
use PhUml\Code\Properties\WithConstants;
18
use PhUml\Code\Properties\WithProperties;
19
20
/**
21
 * It represents a class definition
22
 */
23
final class ClassDefinition extends Definition implements HasProperties, HasConstants, CanBeAbstract, UseTraits, ImplementsInterfaces
24
{
25
    use WithProperties;
26
    use WithConstants;
27
    use WithTraits;
28
    use WithInterfaces;
29
30
    /**
31
     * @param Method[] $methods
32
     * @param Constant[] $constants
33
     * @param Property[] $properties
34
     * @param Name[] $interfaces
35
     * @param Name[] $traits
36 92
     */
37
    public function __construct(
38
        Name $name,
39
        array $methods = [],
40
        array $constants = [],
41
        private readonly ?Name $parent = null,
42
        array $properties = [],
43
        array $interfaces = [],
44
        array $traits = [],
45
        private readonly bool $isAttribute = false
46 92
    ) {
47 92
        parent::__construct($name, $methods);
48 92
        $this->constants = $constants;
49 92
        $this->properties = $properties;
50
        $this->interfaces = $interfaces;
0 ignored issues
show
Bug introduced by
The property interfaces is declared read-only in PhUml\Code\ClassDefinition.
Loading history...
51
        $this->traits = $traits;
52
    }
53
54
    /**
55
     * This method is used by the `AssociationsBuilder` class to discover associations with other
56
     * classes via the constructor
57
     *
58
     * @return Parameter[]
59 22
     * @see \PhUml\Graphviz\Builders\EdgesBuilder::fromProperties() for more details
60
     */
61 22
    public function constructorParameters(): array
62 22
    {
63
        $constructors = array_filter($this->methods, static fn (Method $method): bool => $method->isConstructor());
64 22
        $constructor = reset($constructors);
65
66
        return $constructor === false ? [] : $constructor->parameters();
67
    }
68
69
    /**
70
     * This method is used to build the `Summary` of a `Codebase`
71
     *
72 7
     * @see Summary::propertiesSummary() for more details
73
     */
74 7
    public function countPropertiesByVisibility(Visibility $visibility): int
75 7
    {
76 7
        return \count(array_filter(
77
            $this->properties,
78
            static fn (Property $property): bool => $property->hasVisibility($visibility)
79
        ));
80
    }
81
82
    /**
83
     * This method is used to build the `Summary` of a `Codebase`
84
     *
85 7
     * @see Summary::propertiesSummary() for more details
86
     */
87 7
    public function countTypedPropertiesByVisibility(Visibility $visibility): int
88 7
    {
89
        return \count(array_filter(
90 6
            $this->properties,
91
            static fn (Property $property): bool =>
92
                $property->hasTypeDeclaration() && $property->hasVisibility($visibility)
93
        ));
94
    }
95
96
    /**
97
     * It is used by the `ClassGraphBuilder` to create the edge to represent inheritance
98
     *
99
     * @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details
100
     */
101 43
    public function parent(): Name
102
    {
103 43
        if ($this->parent === null) {
104
            throw new BadMethodCallException('This class does not have a parent class');
105
        }
106
        return $this->parent;
107
    }
108
109
    /**
110
     * It is used by the `ClassGraphBuilder` to determine if an inheritance association should be
111 19
     * created
112
     *
113 19
     * @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details
114 1
     */
115
    public function hasParent(): bool
116 18
    {
117
        return $this->parent !== null;
118
    }
119
120
    /**
121
     * This method is used when the commands are called with the option `hide-empty-blocks`
122
     *
123
     * It counts both the properties and the constants of a class
124
     *
125 42
     * @see Definition::hasProperties() for more details
126
     */
127 42
    public function hasProperties(): bool
128
    {
129
        return \count($this->constants) + \count($this->properties) > 0;
130
    }
131
132
    /**
133
     * This method is used to determine if the class name should be shown in italics
134
     */
135
    public function isAbstract(): bool
136
    {
137 20
        return array_filter($this->methods(), static fn (Method $method): bool => $method->isAbstract()) !== [];
138
    }
139 20
140
    public function isAttribute(): bool
141
    {
142
        return $this->isAttribute;
143
    }
144
}
145