Passed
Push — master ( 8d387d...2ce63d )
by Luis
03:13
created

ClassDefinition::isAbstract()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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 183
    public function __construct(
24
        string $name,
25
        array $constants = [],
26
        array $attributes = [],
27
        array $methods = [],
28
        array $implements = [],
29
        ClassDefinition $extends = null
30
    ) {
31 183
        parent::__construct($name, $constants, $methods, $extends);
32 183
        $this->attributes = $attributes;
33 183
        $this->implements = $implements;
34 183
    }
35
36
    public function hasConstructor(): bool
37
    {
38 42
        return \count(array_filter($this->methods, function (Method $function) {
39 33
            return $function->isConstructor();
40 42
        })) === 1;
41
    }
42
43
    /** @return Variable[] */
44 27
    public function constructorParameters(): array
45
    {
46 27
        if (!$this->hasConstructor()) {
47 3
            return [];
48
        }
49
50 24
        $constructors = array_filter($this->methods, function (Method $method) {
51 24
            return $method->isConstructor();
52 24
        });
53
54 24
        return reset($constructors)->parameters();
55
    }
56
57 75
    public function hasParent(): bool
58
    {
59 75
        return $this->extends !== null;
60
    }
61
62
    public function isAbstract(): bool
63
    {
64 57
        return \count(array_filter($this->methods(), function (Method $method) {
65 39
            return $method->isAbstract();
66 57
        })) > 0;
67
    }
68
69 57
    public function hasAttributes(): bool
70
    {
71 57
        return \count($this->constants) + \count($this->attributes) > 0;
72
    }
73
74
    public function countAttributesByVisibility(Visibility $modifier): int
75
    {
76 12
        return \count(array_filter($this->attributes, function (Attribute $attribute) use ($modifier) {
77 12
            return $attribute->hasVisibility($modifier);
78 12
        }));
79
    }
80
81
    public function countTypedAttributesByVisibility(Visibility $modifier): int
82
    {
83 12
        return \count(array_filter($this->attributes, function (Attribute $attribute) use ($modifier) {
84 12
            return $attribute->hasTypeDeclaration() && $attribute->hasVisibility($modifier);
85 12
        }));
86
    }
87
88
    /** @return Attribute[] */
89 63
    public function attributes(): array
90
    {
91 63
        return $this->attributes;
92
    }
93
94
    /** @return InterfaceDefinition[] */
95 78
    public function implements(): array
96
    {
97 78
        return $this->implements;
98
    }
99
}
100