Passed
Push — variadic-params-visibility-con... ( ca0a66 )
by Luis
13:54
created

FilteredAttributesBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 15 2
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 7.2
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\Members;
9
10
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...
11
use PhpParser\Node\Stmt\Property;
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...
12
use PhUml\Code\Attributes\Attribute;
13
use PhUml\Code\Attributes\AttributeDocBlock;
14
use PhUml\Code\Variables\Variable;
15
use PhUml\Parser\Code\Builders\Filters\PrivateVisibilityFilter;
16
use PhUml\Parser\Code\Builders\Filters\ProtectedVisibilityFilter;
17
18
/**
19
 * It builds an array of `Attributes` for a `ClassDefinition` or a `TraitDefinition`
20
 *
21
 * It applies one or more `VisibilityFilter`s
22
 *
23
 * @see PrivateVisibilityFilter
24
 * @see ProtectedVisibilityFilter
25
 */
26
final class FilteredAttributesBuilder implements AttributesBuilder
27
{
28
    /** @var VisibilityBuilder */
29
    private $visibilityBuilder;
30
31
    /** @var VisibilityFilters */
32
    private $visibilityFilters;
33
34
    public function __construct(VisibilityBuilder $visibilityBuilder, VisibilityFilters $filters)
35
    {
36
        $this->visibilityBuilder = $visibilityBuilder;
37
        $this->visibilityFilters = $filters;
38
    }
39
40
    /**
41
     * @param Node[] $parsedAttributes
42
     * @return Attribute[]
43
     */
44
    public function build(array $parsedAttributes): array
45
    {
46
        $attributes = array_filter($parsedAttributes, static function ($attribute): bool {
47
            return $attribute instanceof Property;
48
        });
49
50
        return array_map(function (Property $attribute): Attribute {
51
            $name = "\${$attribute->props[0]->name}";
52
            $visibility = $this->visibilityBuilder->build($attribute);
53
            $comment = $attribute->getDocComment() === null ? null : $attribute->getDocComment()->getText();
54
            $docBlock = AttributeDocBlock::from($comment);
55
            $variable = new Variable($name, $docBlock->extractType());
56
57
            return new Attribute($variable, $visibility, $attribute->isStatic());
58
        }, $this->visibilityFilters->apply($attributes));
59
    }
60
}
61