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

ClassDefinition::countAttributesByVisibility()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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 represents a class definition
12
 *
13
 * It does not support class constants yet
14
 */
15
class ClassDefinition extends Definition
16
{
17
    /** @var Attribute[] */
18
    public $attributes;
19
20
    /** @var InterfaceDefinition[] */
21
    public $implements;
22
23
    public function __construct(
24
        string $name,
25
        array $attributes = [],
26
        array $functions = [],
27
        array $implements = [],
28
        ClassDefinition $extends = null
29
    ) {
30
        parent::__construct($name, $functions, $extends);
31
        $this->attributes = $attributes;
32
        $this->implements = $implements;
33
    }
34
35
    public function hasConstructor(): bool
36
    {
37
        return \count(array_filter($this->functions, function (Method $function) {
38
            return $function->isConstructor();
39
        })) === 1;
40
    }
41
42
    /** @return Variable[] */
43
    public function constructorParameters(): array
44
    {
45
        if (!$this->hasConstructor()) {
46
            return [];
47
        }
48
49
        $constructors = array_filter($this->functions, function (Method $method) {
50
            return $method->isConstructor();
51
        });
52
53
        return reset($constructors)->params;
54
    }
55
56
    public function hasParent(): bool
57
    {
58
        return $this->extends !== null;
59
    }
60
61
    public function countAttributesByVisibility(Visibility $modifier): int
62
    {
63
        return \count(array_filter($this->attributes, function (Attribute $attribute) use ($modifier) {
64
            return $attribute->modifier->equals($modifier);
65
        }));
66
    }
67
68
    public function countTypedAttributesByVisibility(Visibility $modifier): int
69
    {
70
        return \count(array_filter($this->attributes, function (Attribute $attribute) use ($modifier) {
71
            return $attribute->isTyped() && $attribute->modifier->equals($modifier);
72
        }));
73
    }
74
}
75