Passed
Push — master ( d46d5f...674e23 )
by Luis
44s queued 12s
created

ParsedMethodsBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildMethod() 0 11 1
A build() 0 5 1
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 8.0
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\UseStatements;
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 ParsedMethodsBuilder implements MethodsBuilder
24
{
25
    public function __construct(
26
        private ParametersBuilder $parametersBuilder,
27
        private TypeBuilder $typeBuilder,
28
        private VisibilityBuilder $visibilityBuilder,
29
    ) {
30
    }
31
32
    /**
33
     * @param ClassMethod[] $methods
34
     * @return Method[]
35
     */
36
    public function build(array $methods, UseStatements $useStatements): array
37
    {
38
        return array_map(
39
            fn (ClassMethod $method): Method => $this->buildMethod($method, $useStatements),
40
            $methods
41
        );
42
    }
43
44
    private function buildMethod(ClassMethod $method, UseStatements $useStatements): Method
45
    {
46
        $name = $method->name->name;
47
        $visibility = $this->visibilityBuilder->build($method);
48
        $docBlock = $method->getDocComment();
49
        $returnType = $this->typeBuilder->fromMethodReturnType($method->returnType, $docBlock, $useStatements);
50
        $parameters = $this->parametersBuilder->build($method->params, $docBlock, $useStatements);
51
        return match (true) {
52
            $method->isAbstract() => new Method($name, $visibility, $returnType, $parameters, isAbstract: true),
53
            $method->isStatic() => new Method($name, $visibility, $returnType, $parameters, isStatic: true),
54
            default => new Method($name, $visibility, $returnType, $parameters),
55
        };
56
    }
57
}
58