Completed
Push — master ( 3f1b14...d7a449 )
by Luis
14:49 queued 04:53
created

ClassDefinition   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasParent() 0 3 1
A constructorParameters() 0 11 2
A hasConstructor() 0 5 1
A __construct() 0 12 1
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