Completed
Push — statistics_refactoring ( b2d9c2 )
by Luis
13:39
created

Summary::classCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.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
/**
11
 * It creates a summary of the classes, interfaces, methods, and attributes of a codebase
12
 */
13
class Summary
14
{
15
    private $interfaceCount;
16
    private $classCount;
17
    private $publicFunctionCount;
18
    private $publicAttributeCount;
19
    private $publicTypedAttributes;
20
    private $protectedFunctionCount;
21
    private $protectedAttributeCount;
22
    private $protectedTypedAttributes;
23
    private $privateFunctionCount;
24
    private $privateAttributeCount;
25
    private $privateTypedAttributes;
26
27
    public function __construct()
28
    {
29
        $this->interfaceCount = 0;
30
        $this->classCount = 0;
31
        $this->publicFunctionCount = 0;
32
        $this->publicAttributeCount = 0;
33
        $this->publicTypedAttributes = 0;
34
        $this->protectedFunctionCount = 0;
35
        $this->protectedAttributeCount = 0;
36
        $this->protectedTypedAttributes = 0;
37
        $this->privateFunctionCount = 0;
38
        $this->privateAttributeCount = 0;
39
        $this->privateTypedAttributes = 0;
40
    }
41
42
    public function from(Structure $structure): void
43
    {
44
        foreach ($structure->definitions() as $definition) {
45
            if ($definition instanceof InterfaceDefinition) {
46
                $this->interfaceCount++;
47
            }
48
            if ($definition instanceof ClassDefinition) {
49
                $this->classCount++;
50
                $this->attributesSummary($definition);
51
            }
52
            $this->methodsSummary($definition);
53
        }
54
    }
55
56
    private function attributesSummary(ClassDefinition $definition): void
57
    {
58
        // Attributes count
59
        $this->publicAttributeCount += $definition->countAttributesByVisibility(Visibility::public());
60
        $this->protectedAttributeCount += $definition->countAttributesByVisibility(Visibility::protected());
61
        $this->privateAttributeCount += $definition->countAttributesByVisibility(Visibility::private());
62
63
        // Typed attributes count
64
        $this->publicTypedAttributes += $definition->countTypedAttributesByVisibility(Visibility::public());
65
        $this->protectedTypedAttributes += $definition->countTypedAttributesByVisibility(Visibility::protected());
66
        $this->privateTypedAttributes += $definition->countTypedAttributesByVisibility(Visibility::private());
67
    }
68
69
    private function methodsSummary(Definition $definition): void
70
    {
71
        $this->publicFunctionCount += $definition->countMethodsByVisibility(Visibility::public());
72
        $this->protectedFunctionCount += $definition->countMethodsByVisibility(Visibility::protected());
73
        $this->privateFunctionCount += $definition->countMethodsByVisibility(Visibility::private());
74
    }
75
76
    public function interfaceCount(): int
77
    {
78
        return $this->interfaceCount;
79
    }
80
81
    public function classCount(): int
82
    {
83
        return $this->classCount;
84
    }
85
86
    public function publicFunctionCount(): int
87
    {
88
        return $this->publicFunctionCount;
89
    }
90
91
    public function publicAttributeCount(): int
92
    {
93
        return $this->publicAttributeCount;
94
    }
95
96
    public function publicTypedAttributes(): int
97
    {
98
        return $this->publicTypedAttributes;
99
    }
100
101
    public function protectedFunctionCount(): int
102
    {
103
        return $this->protectedFunctionCount;
104
    }
105
106
    public function protectedAttributeCount(): int
107
    {
108
        return $this->protectedAttributeCount;
109
    }
110
111
    public function protectedTypedAttributes(): int
112
    {
113
        return $this->protectedTypedAttributes;
114
    }
115
116
    public function privateFunctionCount(): int
117
    {
118
        return $this->privateFunctionCount;
119
    }
120
121
    public function privateAttributeCount(): int
122
    {
123
        return $this->privateAttributeCount;
124
    }
125
126
    public function privateTypedAttributes(): int
127
    {
128
        return $this->privateTypedAttributes;
129
    }
130
131
    public function functionCount(): int
132
    {
133
        return $this->publicFunctionCount + $this->protectedFunctionCount + $this->privateFunctionCount;
134
    }
135
136
    public function attributeCount(): int
137
    {
138
        return $this->publicAttributeCount + $this->protectedAttributeCount + $this->privateAttributeCount;
139
    }
140
141
    public function typedAttributeCount(): int
142
    {
143
        return $this->publicTypedAttributes + $this->protectedTypedAttributes + $this->privateTypedAttributes;
144
    }
145
146
    public function attributesPerClass(): float
147
    {
148
        return round($this->attributeCount() / $this->classCount, 2);
149
    }
150
151
    public function functionsPerClass(): float
152
    {
153
        return round($this->functionCount() / $this->classCount, 2);
154
    }
155
}
156