Test Failed
Push — psr4-packages ( 592372 )
by Luis
02:13
created

ClassDefinition::constructorParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 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
use PhUml\Graphviz\HasNodeIdentifier;
11
use PhUml\Graphviz\ObjectHashIdentifier;
12
13
class ClassDefinition implements HasNodeIdentifier
14
{
15
    use ObjectHashIdentifier;
16
17
    /** @var string */
18
    public $name;
19
20
    /** @var Attribute[] */
21
    public $attributes;
22
23
    /** @var Method[] */
24
    public $functions;
25
26
    /** @var InterfaceDefinition[] */
27
    public $implements;
28
29
    /** @var ClassDefinition */
30
    public $extends;
31
32
    public function __construct(
33
        string $name,
34
        array $attributes = [],
35
        array $functions = [],
36
        array $implements = [],
37
        $extends = null
38
    ) {
39
        $this->name = $name;
40
        $this->attributes = $attributes;
41
        $this->functions = $functions;
42
        $this->implements = $implements;
43
        $this->extends = $extends;
44
    }
45
46
    public function hasConstructor(): bool
47
    {
48
        return \count(array_filter($this->functions, function (Method $function) {
49
            return $function->isConstructor();
50
        })) === 1;
51
    }
52
53
    /** @return Variable[] */
54
    public function constructorParameters(): array
55
    {
56
        if (!$this->hasConstructor()) {
57
            return [];
58
        }
59
60
        $constructors = array_filter($this->functions, function (Method $function) {
61
            return $function->isConstructor();
62
        });
63
64
        return reset($constructors)->params;
65
    }
66
67
    public function hasParent(): bool
68
    {
69
        return $this->extends !== null;
70
    }
71
}
72