Issues (222)

src/Code/ClassDefinition.php (2 issues)

Labels
Severity
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
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, 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
     */
37 93
    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
    ) {
47 93
        parent::__construct($name, $methods);
48 93
        $this->constants = $constants;
49 93
        $this->properties = $properties;
50 93
        $this->interfaces = $interfaces;
0 ignored issues
show
The property interfaces is declared read-only in PhUml\Code\ClassDefinition.
Loading history...
51 93
        $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
     * @see \PhUml\Graphviz\Builders\EdgesBuilder::fromProperties() for more details
60
     */
61 25
    public function constructorParameters(): array
62
    {
63 25
        $constructors = array_filter($this->methods, static fn (Method $method): bool => $method->isConstructor());
64 25
        $constructor = reset($constructors);
65
66 25
        return $constructor === false ? [] : $constructor->parameters();
67
    }
68
69
    /**
70
     * This method is used to build the `Summary` of a `Codebase`
71
     *
72
     * @see Summary::propertiesSummary() for more details
73
     */
74 7
    public function countPropertiesByVisibility(Visibility $visibility): int
75
    {
76 7
        return \count(array_filter(
77 7
            $this->properties,
78 7
            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
     * @see Summary::propertiesSummary() for more details
86
     */
87 7
    public function countTypedPropertiesByVisibility(Visibility $visibility): int
88
    {
89 7
        return \count(array_filter(
90 7
            $this->properties,
91
            static fn (Property $property): bool =>
92 6
                $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 19
    public function parent(): Name
102
    {
103 19
        if ($this->parent === null) {
104 1
            throw new BadMethodCallException('This class does not have a parent class');
105
        }
106 18
        return $this->parent;
107
    }
108
109
    /**
110
     * It is used by the `ClassGraphBuilder` to determine if an inheritance association should be
111
     * created
112
     *
113
     * @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details
114
     */
115 42
    public function hasParent(): bool
116
    {
117 42
        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
     * @see Definition::hasProperties() for more details
126
     */
127 20
    public function hasProperties(): bool
128
    {
129 20
        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 20
    public function isAbstract(): bool
136
    {
137 20
        return array_filter($this->methods(), static fn (Method $method): bool => $method->isAbstract()) !== [];
138
    }
139
140 21
    public function isAttribute(): bool
141
    {
142 21
        return $this->isAttribute;
143
    }
144
}
145