Passed
Pull Request — master (#22)
by Luis
24:40 queued 21:48
created

Summary::typedPropertiesCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 8.1
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 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...
11
12
/**
13
 * It creates a summary of the classes, interfaces, methods, and properties of a codebase
14
 *
15
 * The summary of a `Structure` does not include counts of constants
16
 */
17
final class Summary
18
{
19
    /** @noRector \Rector\Php81\Rector\Property\ReadOnlyPropertyRector  */
20
    private int $interfaceCount;
21
22
    /** @noRector \Rector\Php81\Rector\Property\ReadOnlyPropertyRector  */
23
    private int $classCount;
24
25
    private int $publicFunctionCount;
26
27
    private int $publicPropertyCount;
28
29
    private int $publicTypedProperties;
30
31
    private int $protectedFunctionCount;
32
33
    private int $protectedPropertyCount;
34
35
    private int $protectedTypedProperties;
36
37
    private int $privateFunctionCount;
38
39
    private int $privatePropertyCount;
40
41
    private int $privateTypedProperties;
42
43 9
    public static function from(Codebase $codebase): Summary
44
    {
45 9
        return new Summary($codebase);
46
    }
47
48 9
    private function __construct(Codebase $codebase)
49
    {
50 9
        $this->interfaceCount = 0;
51 9
        $this->classCount = 0;
52 9
        $this->publicFunctionCount = 0;
53 9
        $this->publicPropertyCount = 0;
54 9
        $this->publicTypedProperties = 0;
55 9
        $this->protectedFunctionCount = 0;
56 9
        $this->protectedPropertyCount = 0;
57 9
        $this->protectedTypedProperties = 0;
58 9
        $this->privateFunctionCount = 0;
59 9
        $this->privatePropertyCount = 0;
60 9
        $this->privateTypedProperties = 0;
61 9
        foreach ($codebase->definitions() as $definition) {
62 7
            if ($definition instanceof InterfaceDefinition) {
0 ignored issues
show
Bug introduced by
The type PhUml\Code\InterfaceDefinition 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...
63 2
                $this->interfaceCount++;
64
            }
65 7
            if ($definition instanceof ClassDefinition) {
0 ignored issues
show
Bug introduced by
The type PhUml\Code\ClassDefinition 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...
66 7
                $this->classCount++;
67 7
                $this->propertiesSummary($definition);
68
            }
69 7
            $this->methodsSummary($definition);
70
        }
71
    }
72
73 7
    private function propertiesSummary(ClassDefinition $definition): void
74
    {
75
        // Properties count
76 7
        $this->publicPropertyCount += $definition->countPropertiesByVisibility(Visibility::public());
77 7
        $this->protectedPropertyCount += $definition->countPropertiesByVisibility(Visibility::protected());
78 7
        $this->privatePropertyCount += $definition->countPropertiesByVisibility(Visibility::private());
79
80
        // Typed properties count
81 7
        $this->publicTypedProperties += $definition->countTypedPropertiesByVisibility(Visibility::public());
82 7
        $this->protectedTypedProperties += $definition->countTypedPropertiesByVisibility(Visibility::protected());
83 7
        $this->privateTypedProperties += $definition->countTypedPropertiesByVisibility(Visibility::private());
84
    }
85
86 7
    private function methodsSummary(Definition $definition): void
87
    {
88 7
        $this->publicFunctionCount += $definition->countMethodsByVisibility(Visibility::public());
89 7
        $this->protectedFunctionCount += $definition->countMethodsByVisibility(Visibility::protected());
90 7
        $this->privateFunctionCount += $definition->countMethodsByVisibility(Visibility::private());
91
    }
92
93 6
    public function interfaceCount(): int
94
    {
95 6
        return $this->interfaceCount;
96
    }
97
98 6
    public function classCount(): int
99
    {
100 6
        return $this->classCount;
101
    }
102
103 6
    public function publicFunctionCount(): int
104
    {
105 6
        return $this->publicFunctionCount;
106
    }
107
108 6
    public function publicPropertyCount(): int
109
    {
110 6
        return $this->publicPropertyCount;
111
    }
112
113 2
    public function publicTypedProperties(): int
114
    {
115 2
        return $this->publicTypedProperties;
116
    }
117
118 6
    public function protectedFunctionCount(): int
119
    {
120 6
        return $this->protectedFunctionCount;
121
    }
122
123 6
    public function protectedPropertyCount(): int
124
    {
125 6
        return $this->protectedPropertyCount;
126
    }
127
128 3
    public function protectedTypedProperties(): int
129
    {
130 3
        return $this->protectedTypedProperties;
131
    }
132
133 6
    public function privateFunctionCount(): int
134
    {
135 6
        return $this->privateFunctionCount;
136
    }
137
138 6
    public function privatePropertyCount(): int
139
    {
140 6
        return $this->privatePropertyCount;
141
    }
142
143 2
    public function privateTypedProperties(): int
144
    {
145 2
        return $this->privateTypedProperties;
146
    }
147
148 7
    public function functionCount(): int
149
    {
150 7
        return $this->publicFunctionCount + $this->protectedFunctionCount + $this->privateFunctionCount;
151
    }
152
153 7
    public function propertiesCount(): int
154
    {
155 7
        return $this->publicPropertyCount + $this->protectedPropertyCount + $this->privatePropertyCount;
156
    }
157
158 6
    public function typedPropertiesCount(): int
159
    {
160 6
        return $this->publicTypedProperties + $this->protectedTypedProperties + $this->privateTypedProperties;
161
    }
162
163 7
    public function propertiesPerClass(): float
164
    {
165 7
        if ($this->classCount === 0) {
166 2
            return 0;
167
        }
168
169 5
        return round($this->propertiesCount() / $this->classCount, 2);
170
    }
171
172 7
    public function functionsPerClass(): float
173
    {
174 7
        if ($this->classCount === 0) {
175 2
            return 0;
176
        }
177
178 5
        return round($this->functionCount() / $this->classCount, 2);
179
    }
180
}
181