Passed
Push — master ( 1ba30a...8ac47c )
by Luis
01:05 queued 12s
created

MembersBuilder::methods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
4
 */
5
6
namespace PhUml\Parser\Code\Builders;
7
8
use PhpParser\Node;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use PhpParser\Node\Stmt\ClassConst;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\ClassConst was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use PhpParser\Node\Stmt\ClassMethod;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\ClassMethod was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use PhpParser\Node\Stmt\EnumCase as ParsedEnumCase;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\EnumCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use PhpParser\Node\Stmt\Property as ParsedProperty;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\Property was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use PhUml\Code\Methods\Method;
14
use PhUml\Code\Properties\Constant;
15
use PhUml\Code\Properties\EnumCase;
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 49
    public function __construct(
33
        private readonly ConstantsBuilder $constantsBuilder,
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 33
    public function constants(array $members): array
45
    {
46
        /** @var ClassConst[] $constants */
47 33
        $constants = array_filter($members, static fn ($property): bool => $property instanceof ClassConst);
48
49
        /** @var ClassConst[] $filteredConstants */
50 33
        $filteredConstants = $this->filters->apply($constants);
51
52 33
        return $this->constantsBuilder->build($filteredConstants);
53
    }
54
55
    /**
56
     * @param Node[] $members
57
     * @return Property[]
58
     */
59 38
    public function properties(array $members, ?ClassMethod $constructor, UseStatements $useStatements): array
60
    {
61 38
        $properties = [];
62 38
        if ($constructor !== null) {
63 25
            $properties = $this->fromPromotedProperties($constructor, $useStatements);
64
        }
65
66
        /** @var ParsedProperty[] $parsedProperties */
67 38
        $parsedProperties = array_filter($members, static fn ($property): bool => $property instanceof ParsedProperty);
68
69
        /** @var ParsedProperty[] $filteredProperties */
70 38
        $filteredProperties = $this->filters->apply($parsedProperties);
71
72 38
        return array_merge($this->propertiesBuilder->build($filteredProperties, $useStatements), $properties);
73
    }
74
75
    /** @return Property[] */
76 25
    private function fromPromotedProperties(ClassMethod $constructor, UseStatements $useStatements): array
77
    {
78 25
        $promotedProperties = array_filter(
79 25
            $constructor->getParams(),
80 25
            static fn (Node\Param $param) => $param->flags !== 0
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Param was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
        );
82
83
        /** @var Node\Param[] $filteredPromotedProperties */
84 25
        $filteredPromotedProperties = $this->filters->apply($promotedProperties);
85
86 25
        return $this->propertiesBuilder->fromPromotedProperties($filteredPromotedProperties, $useStatements);
87
    }
88
89
    /**
90
     * @param ClassMethod[] $methods
91
     * @return Method[]
92
     */
93 37
    public function methods(array $methods, UseStatements $useStatements): array
94
    {
95
        /** @var ClassMethod[] $filteredMethods */
96 37
        $filteredMethods = $this->filters->apply($methods);
97
98 37
        return $this->methodsBuilder->build($filteredMethods, $useStatements);
99
    }
100
101
    /**
102
     * @param Node[] $members
103
     * @return EnumCase[]
104
     */
105 20
    public function enumCases(array $members): array
106
    {
107
        /** @var ParsedEnumCase[] $cases */
108 20
        $cases = array_filter($members, static fn ($member): bool => $member instanceof ParsedEnumCase);
109
110 20
        return array_map(static fn (ParsedEnumCase $case) => new EnumCase((string) $case->name), $cases);
111
    }
112
}
113