Passed
Push — 1.3 ( 2ce63d )
by Luis
11:41
created

ClassDefinition::attributes()   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 represents a class definition
12
 *
13
 * It does not support class constants yet
14
 */
15
class ClassDefinition extends Definition implements CanBeAbstract
16
{
17
    /** @var Attribute[] */
18
    private $attributes;
19
20
    /** @var InterfaceDefinition[] */
21
    private $implements;
22
23
    public function __construct(
24
        string $name,
25
        array $constants = [],
26
        array $attributes = [],
27
        array $methods = [],
28
        array $implements = [],
29
        ClassDefinition $extends = null
30
    ) {
31
        parent::__construct($name, $constants, $methods, $extends);
32
        $this->attributes = $attributes;
33
        $this->implements = $implements;
34
    }
35
36
    public function hasConstructor(): bool
37
    {
38
        return \count(array_filter($this->methods, function (Method $function) {
39
            return $function->isConstructor();
40
        })) === 1;
41
    }
42
43
    /** @return Variable[] */
44
    public function constructorParameters(): array
45
    {
46
        if (!$this->hasConstructor()) {
47
            return [];
48
        }
49
50
        $constructors = array_filter($this->methods, function (Method $method) {
51
            return $method->isConstructor();
52
        });
53
54
        return reset($constructors)->parameters();
55
    }
56
57
    public function hasParent(): bool
58
    {
59
        return $this->extends !== null;
60
    }
61
62
    public function isAbstract(): bool
63
    {
64
        return \count(array_filter($this->methods(), function (Method $method) {
65
            return $method->isAbstract();
66
        })) > 0;
67
    }
68
69
    public function hasAttributes(): bool
70
    {
71
        return \count($this->constants) + \count($this->attributes) > 0;
72
    }
73
74
    public function countAttributesByVisibility(Visibility $modifier): int
75
    {
76
        return \count(array_filter($this->attributes, function (Attribute $attribute) use ($modifier) {
77
            return $attribute->hasVisibility($modifier);
78
        }));
79
    }
80
81
    public function countTypedAttributesByVisibility(Visibility $modifier): int
82
    {
83
        return \count(array_filter($this->attributes, function (Attribute $attribute) use ($modifier) {
84
            return $attribute->hasTypeDeclaration() && $attribute->hasVisibility($modifier);
85
        }));
86
    }
87
88
    /** @return Attribute[] */
89
    public function attributes(): array
90
    {
91
        return $this->attributes;
92
    }
93
94
    /** @return InterfaceDefinition[] */
95
    public function implements(): array
96
    {
97
        return $this->implements;
98
    }
99
}
100