Completed
Push — symfony-console-application ( 3187e2...c3ee2a )
by Luis
10:39
created

Summary::attributesSummary()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 8
nop 1
dl 0
loc 22
rs 6.6037
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
class Summary
11
{
12
    private $interfaceCount;
13
    private $classCount;
14
    private $publicFunctionCount;
15
    private $publicAttributeCount;
16
    private $publicTypedAttributes;
17
    private $protectedFunctionCount;
18
    private $protectedAttributeCount;
19
    private $protectedTypedAttributes;
20
    private $privateFunctionCount;
21
    private $privateAttributeCount;
22
    private $privateTypedAttributes;
23
24
    public function __construct()
25
    {
26
        $this->interfaceCount = 0;
27
        $this->classCount = 0;
28
        $this->publicFunctionCount = 0;
29
        $this->publicAttributeCount = 0;
30
        $this->publicTypedAttributes = 0;
31
        $this->protectedFunctionCount = 0;
32
        $this->protectedAttributeCount = 0;
33
        $this->protectedTypedAttributes = 0;
34
        $this->privateFunctionCount = 0;
35
        $this->privateAttributeCount = 0;
36
        $this->privateTypedAttributes = 0;
37
    }
38
39
    public function from(Structure $structure): void
40
    {
41
        foreach ($structure->definitions() as $definition) {
42
            if ($definition instanceof InterfaceDefinition) {
43
                $this->interfaceCount++;
44
            }
45
            if ($definition instanceof ClassDefinition) {
46
                $this->classCount++;
47
                $this->attributesSummary($definition);
48
            }
49
            $this->methodsSummary($definition);
50
        }
51
    }
52
53
    private function attributesSummary(ClassDefinition $definition): void
54
    {
55
        foreach ($definition->attributes as $attribute) {
56
            switch ($attribute->modifier) {
57
                case 'public':
58
                    $this->publicAttributeCount++;
59
                    if ($attribute->type->isPresent()) {
60
                        $this->publicTypedAttributes++;
61
                    }
62
                    break;
63
                case 'protected':
64
                    $this->protectedAttributeCount++;
65
                    if ($attribute->type->isPresent()) {
66
                        $this->protectedTypedAttributes++;
67
                    }
68
                    break;
69
                case 'private':
70
                    $this->privateAttributeCount++;
71
                    if ($attribute->type->isPresent()) {
72
                        $this->privateTypedAttributes++;
73
                    }
74
                    break;
75
            }
76
        }
77
    }
78
79
    private function methodsSummary(Definition $definition): void
80
    {
81
        foreach ($definition->functions as $function) {
82
            switch ($function->modifier) {
83
                case 'public':
84
                    $this->publicFunctionCount++;
85
                    break;
86
                case 'protected':
87
                    $this->protectedFunctionCount++;
88
                    break;
89
                case 'private':
90
                    $this->privateFunctionCount++;
91
                    break;
92
            }
93
        }
94
    }
95
96
    public function interfaceCount(): int
97
    {
98
        return $this->interfaceCount;
99
    }
100
101
    public function classCount(): int
102
    {
103
        return $this->classCount;
104
    }
105
106
    public function publicFunctionCount(): int
107
    {
108
        return $this->publicFunctionCount;
109
    }
110
111
    public function publicAttributeCount(): int
112
    {
113
        return $this->publicAttributeCount;
114
    }
115
116
    public function publicTypedAttributes(): int
117
    {
118
        return $this->publicTypedAttributes;
119
    }
120
121
    public function protectedFunctionCount(): int
122
    {
123
        return $this->protectedFunctionCount;
124
    }
125
126
    public function protectedAttributeCount(): int
127
    {
128
        return $this->protectedAttributeCount;
129
    }
130
131
    public function protectedTypedAttributes(): int
132
    {
133
        return $this->protectedTypedAttributes;
134
    }
135
136
    public function privateFunctionCount(): int
137
    {
138
        return $this->privateFunctionCount;
139
    }
140
141
    public function privateAttributeCount(): int
142
    {
143
        return $this->privateAttributeCount;
144
    }
145
146
    public function privateTypedAttributes(): int
147
    {
148
        return $this->privateTypedAttributes;
149
    }
150
151
    public function functionCount(): int
152
    {
153
        return $this->publicFunctionCount + $this->protectedFunctionCount + $this->privateFunctionCount;
154
    }
155
156
    public function attributeCount(): int
157
    {
158
        return $this->publicAttributeCount + $this->protectedAttributeCount + $this->privateAttributeCount;
159
    }
160
161
    public function typedAttributeCount(): int
162
    {
163
        return $this->publicTypedAttributes + $this->protectedTypedAttributes + $this->privateTypedAttributes;
164
    }
165
166
    public function attributesPerClass(): float
167
    {
168
        return round($this->attributeCount() / $this->classCount, 2);
169
    }
170
171
    public function functionsPerClass(): float
172
    {
173
        return round($this->functionCount() / $this->classCount, 2);
174
    }
175
}
176