Passed
Push — 1.5 ( c7c5a4...fe8795 )
by Luis
06:06
created

ClassDefinitionBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildInterfaces() 0 5 1
A __construct() 0 8 1
A build() 0 9 2
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\Parser\Code\Builders;
9
10
use PhpParser\Node\Name;
11
use PhpParser\Node\Stmt\Class_;
12
use PhUml\Code\ClassDefinition;
13
use PhUml\Code\Name as ClassDefinitionName;
14
use PhUml\Parser\Code\Builders\Members\AttributesBuilder;
15
use PhUml\Parser\Code\Builders\Members\ConstantsBuilder;
16
use PhUml\Parser\Code\Builders\Members\MethodsBuilder;
17
18
/**
19
 * It builds a `ClassDefinition`
20
 *
21
 * @see ConstantsBuilder for more details about the constants creation
22
 * @see AttributesBuilder for more details about the attributes creation
23
 * @see MethodsBuilder for more details about the methods creation
24
 */
25
class ClassDefinitionBuilder
26
{
27
    /** @var AttributesBuilder */
28
    protected $attributesBuilder;
29
30
    /** @var MethodsBuilder */
31
    protected $methodsBuilder;
32
33
    /** @var ConstantsBuilder */
34
    protected $constantsBuilder;
35
36 129
    public function __construct(
37
        ConstantsBuilder $constantsBuilder = null,
38
        AttributesBuilder $attributesBuilder = null,
39
        MethodsBuilder $methodsBuilder = null
40
    ) {
41 129
        $this->constantsBuilder = $constantsBuilder ?? new ConstantsBuilder();
42 129
        $this->attributesBuilder = $attributesBuilder ?? new AttributesBuilder([]);
43 129
        $this->methodsBuilder = $methodsBuilder ?? new MethodsBuilder([]);
44 129
    }
45
46 96
    public function build(Class_ $class): ClassDefinition
47
    {
48 96
        return new ClassDefinition(
49 96
            ClassDefinitionName::from($class->name),
0 ignored issues
show
Bug introduced by
It seems like $class->name can also be of type null; however, parameter $text of PhUml\Code\Name::from() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            ClassDefinitionName::from(/** @scrutinizer ignore-type */ $class->name),
Loading history...
50 96
            $this->constantsBuilder->build($class->stmts),
51 96
            $this->methodsBuilder->build($class->getMethods()),
52 96
            !empty($class->extends) ? ClassDefinitionName::from(end($class->extends->parts)) : null,
53 96
            $this->attributesBuilder->build($class->stmts),
54 96
            $this->buildInterfaces($class->implements)
55
        );
56
    }
57
58
    /** @return ClassDefinitionName[] */
59
    protected function buildInterfaces(array $implements): array
60
    {
61 102
        return array_map(function (Name $name) {
62 42
            return ClassDefinitionName::from($name->getLast());
63 102
        }, $implements);
64
    }
65
}
66