Passed
Pull Request — master (#23)
by
unknown
13:36 queued 10:51
created

ClassDefinition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 8
dl 0
loc 14
ccs 5
cts 5
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;
0 ignored issues
show
Bug introduced by
The type PhUml\Code\Modifiers\Visibility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
24
{
25
    use WithProperties;
26
    use WithConstants;
27
    use WithTraits;
28
29
    /**
30
     * @param Method[] $methods
31
     * @param Constant[] $constants
32
     * @param Property[] $properties
33
     * @param Name[] $interfaces
34
     * @param Name[] $traits
35
     */
36 92
    public function __construct(
37
        Name $name,
38
        array $methods = [],
39
        array $constants = [],
40
        private readonly ?Name $parent = null,
41
        array $properties = [],
42
        private readonly array $interfaces = [],
43
        array $traits = [],
44
        private readonly bool $isAttribute = false
45
    ) {
46 92
        parent::__construct($name, $methods);
47 92
        $this->constants = $constants;
48 92
        $this->properties = $properties;
49 92
        $this->traits = $traits;
50
    }
51
52
    /**
53
     * This method is used by the `AssociationsBuilder` class to discover associations with other
54
     * classes via the constructor
55
     *
56
     * @return Parameter[]
57
     * @see \PhUml\Graphviz\Builders\AssociationsBuilder::fromProperties() for more details
58
     */
59 22
    public function constructorParameters(): array
60
    {
61 22
        $constructors = array_filter($this->methods, static fn (Method $method): bool => $method->isConstructor());
62 22
        $constructor = reset($constructors);
63
64 22
        return $constructor === false ? [] : $constructor->parameters();
65
    }
66
67
    /**
68
     * This method is used to build the `Summary` of a `Codebase`
69
     *
70
     * @see Summary::propertiesSummary() for more details
71
     */
72 7
    public function countPropertiesByVisibility(Visibility $visibility): int
73
    {
74 7
        return \count(array_filter(
75 7
            $this->properties,
76 7
            static fn (Property $property): bool => $property->hasVisibility($visibility)
77
        ));
78
    }
79
80
    /**
81
     * This method is used to build the `Summary` of a `Codebase`
82
     *
83
     * @see Summary::propertiesSummary() for more details
84
     */
85 7
    public function countTypedPropertiesByVisibility(Visibility $visibility): int
86
    {
87 7
        return \count(array_filter(
88 7
            $this->properties,
89
            static fn (Property $property): bool =>
90 6
                $property->hasTypeDeclaration() && $property->hasVisibility($visibility)
91
        ));
92
    }
93
94
    /**
95
     * It is used by the `ClassGraphBuilder` to create the edges to represent implementation
96
     * associations
97
     *
98
     * @return Name[]
99
     * @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details
100
     */
101 43
    public function interfaces(): array
102
    {
103 43
        return $this->interfaces;
104
    }
105
106
    /**
107
     * It is used by the `ClassGraphBuilder` to create the edge to represent inheritance
108
     *
109
     * @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details
110
     */
111 19
    public function parent(): Name
112
    {
113 19
        if ($this->parent === null) {
114 1
            throw new BadMethodCallException('This class does not have a parent class');
115
        }
116 18
        return $this->parent;
117
    }
118
119
    /**
120
     * It is used by the `ClassGraphBuilder` to determine if an inheritance association should be
121
     * created
122
     *
123
     * @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details
124
     */
125 42
    public function hasParent(): bool
126
    {
127 42
        return $this->parent !== null;
128
    }
129
130
    /**
131
     * This method is used when the commands are called with the option `hide-empty-blocks`
132
     *
133
     * It counts both the properties and the constants of a class
134
     *
135
     * @see Definition::hasProperties() for more details
136
     */
137 20
    public function hasProperties(): bool
138
    {
139 20
        return \count($this->constants) + \count($this->properties) > 0;
140
    }
141
142
    /**
143
     * This method is used to determine if the class name should be shown in italics
144
     */
145 20
    public function isAbstract(): bool
146
    {
147 20
        return array_filter($this->methods(), static fn (Method $method): bool => $method->isAbstract()) !== [];
148
    }
149
150 21
    public function isAttribute(): bool
151
    {
152 21
        return $this->isAttribute;
153
    }
154
}
155