Passed
Push — master ( d09870...716959 )
by Luis
02:38
created

ClassDefinition::hasParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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