Completed
Push — code_refactoring ( 3ee845 )
by Luis
14:42
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
16
{
17
    /** @var Attribute[] */
18
    private $attributes;
19
20
    /** @var InterfaceDefinition[] */
21
    private $implements;
22
23
    public function __construct(
24
        string $name,
25
        array $attributes = [],
26
        array $methods = [],
27
        array $implements = [],
28
        ClassDefinition $extends = null
29
    ) {
30
        parent::__construct($name, $methods, $extends);
31
        $this->attributes = $attributes;
32
        $this->implements = $implements;
33
    }
34
35
    public function hasConstructor(): bool
36
    {
37
        return \count(array_filter($this->methods, 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->methods, function (Method $method) {
50
            return $method->isConstructor();
51
        });
52
53
        return reset($constructors)->parameters();
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->hasVisibility($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->hasVisibility($modifier);
72
        }));
73
    }
74
75
    /** @return Attribute[] */
76
    public function attributes(): array
77
    {
78
        return $this->attributes;
79
    }
80
81
    /** @return InterfaceDefinition[] */
82
    public function implements(): array
83
    {
84
        return $this->implements;
85
    }
86
}
87