Passed
Pull Request — master (#22)
by Luis
24:40 queued 21:48
created

MembersBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 69
ccs 23
cts 23
cp 1
rs 10
c 1
b 0
f 0
wmc 6
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 8.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;
11
use PhpParser\Node\Stmt\ClassConst;
12
use PhpParser\Node\Stmt\ClassMethod;
13
use PhpParser\Node\Stmt\Property as ParsedProperty;
14
use PhUml\Code\Methods\Method;
15
use PhUml\Code\Properties\Constant;
16
use PhUml\Code\Properties\Property;
17
use PhUml\Code\UseStatements;
18
use PhUml\Parser\Code\Builders\Members\ConstantsBuilder;
19
use PhUml\Parser\Code\Builders\Members\MethodsBuilder;
20
use PhUml\Parser\Code\Builders\Members\PropertiesBuilder;
21
use PhUml\Parser\Code\Builders\Members\VisibilityFilters;
22
23
/**
24
 * It builds the constants, properties and methods of a definition
25
 *
26
 * @see ConstantsBuilder for more details about the constants creation
27
 * @see PropertiesBuilder for more details about the properties creation
28
 * @see MethodsBuilder for more details about the methods creation
29
 */
30
final class MembersBuilder
31
{
32 48
    public function __construct(
33
        private readonly ConstantsBuilder $constantsBuilder,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 33 at column 25
Loading history...
34
        private readonly PropertiesBuilder $propertiesBuilder,
35
        private readonly MethodsBuilder $methodsBuilder,
36
        private readonly VisibilityFilters $filters,
37
    ) {
38
    }
39
40
    /**
41
     * @param Node[] $members
42
     * @return Constant[]
43
     */
44 32
    public function constants(array $members): array
45
    {
46
        /** @var ClassConst[] $constants */
47 32
        $constants = array_filter($members, static fn ($property): bool => $property instanceof ClassConst);
48
49
        /** @var ClassConst[] $filteredConstants */
50 32
        $filteredConstants = $this->filters->apply($constants);
51
52 32
        return $this->constantsBuilder->build($filteredConstants);
53
    }
54
55
    /**
56
     * @param Node[] $members
57
     * @return Property[]
58
     */
59 37
    public function properties(array $members, ?ClassMethod $constructor, UseStatements $useStatements): array
60
    {
61 37
        $properties = [];
62 37
        if ($constructor !== null) {
63 24
            $properties = $this->fromPromotedProperties($constructor, $useStatements);
64
        }
65
66
        /** @var ParsedProperty[] $parsedProperties */
67 37
        $parsedProperties = array_filter($members, static fn ($property): bool => $property instanceof ParsedProperty);
68
69
        /** @var ParsedProperty[] $filteredProperties */
70 37
        $filteredProperties = $this->filters->apply($parsedProperties);
71
72 37
        return array_merge($this->propertiesBuilder->build($filteredProperties, $useStatements), $properties);
73
    }
74
75
    /**
76
     * @param ClassMethod[] $methods
77
     * @return Method[]
78
     */
79 36
    public function methods(array $methods, UseStatements $useStatements): array
80
    {
81
        /** @var ClassMethod[] $filteredMethods */
82 36
        $filteredMethods = $this->filters->apply($methods);
83
84 36
        return $this->methodsBuilder->build($filteredMethods, $useStatements);
85
    }
86
87
    /** @return Property[] */
88 24
    private function fromPromotedProperties(ClassMethod $constructor, UseStatements $useStatements): array
89
    {
90 24
        $promotedProperties = array_filter(
91 24
            $constructor->getParams(),
92 24
            static fn (Node\Param $param) => $param->flags !== 0
93
        );
94
95
        /** @var Node\Param[] $filteredPromotedProperties */
96 24
        $filteredPromotedProperties = $this->filters->apply($promotedProperties);
97
98 24
        return $this->propertiesBuilder->fromPromotedProperties($filteredPromotedProperties, $useStatements);
99
    }
100
}
101