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

FilteredMethodsBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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\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 PhUml\Code\Methods\Method;
12
use PhUml\Code\Methods\MethodDocBlock;
13
14
/**
15
 * It builds an array with `Method`s for a `ClassDefinition`, an `InterfaceDefinition` or a
16
 * `TraitDefinition`
17
 *
18
 * It can run one or more `VisibilityFilter`s
19
 *
20
 * @see PrivateVisibilityFilter
21
 * @see ProtectedVisibilityFilter
22
 */
23
final class FilteredMethodsBuilder implements MethodsBuilder
24
{
25
    /** @var ParametersBuilder */
26
    private $parametersBuilder;
27
28
    /** @var VisibilityBuilder */
29
    private $visibilityBuilder;
30
31
    /** @var TypeBuilder */
32
    private $typeBuilder;
33
34
    /** @var VisibilityFilters */
35
    private $visibilityFilters;
36
37
    public function __construct(
38
        ParametersBuilder $parametersBuilder,
39
        TypeBuilder $typeBuilder,
40
        VisibilityBuilder $visibilityBuilder,
41
        VisibilityFilters $filters
42
    ) {
43
        $this->visibilityFilters = $filters;
44
        $this->typeBuilder = $typeBuilder;
45
        $this->parametersBuilder = $parametersBuilder;
46
        $this->visibilityBuilder = $visibilityBuilder;
47
    }
48
49
    /**
50
     * @param ClassMethod[] $methods
51
     * @return Method[]
52
     */
53
    public function build(array $methods): array
54
    {
55
        return array_map(function (ClassMethod $method): Method {
56
            return $this->buildMethod($method);
57
        }, $this->visibilityFilters->apply($methods));
58
    }
59
60
    private function buildMethod(ClassMethod $method): Method
61
    {
62
        $name = $method->name->name;
63
        $visibility = $this->visibilityBuilder->build($method);
64
        $docBlock = $method->getDocComment() === null ? null : $method->getDocComment()->getText();
65
        $methodDocBlock = MethodDocBlock::from($docBlock);
66
        $returnType = $this->typeBuilder->fromMethodReturnType($method->returnType, $methodDocBlock);
67
        $parameters = $this->parametersBuilder->build($method->params, $methodDocBlock);
68
        switch (true) {
69
            case $method->isAbstract():
70
                return new Method($name, $visibility, $returnType, $parameters, true);
71
            case $method->isStatic():
72
                return new Method($name, $visibility, $returnType, $parameters, false, true);
73
            default:
74
                return new Method($name, $visibility, $returnType, $parameters);
75
        }
76
    }
77
}
78