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

ParsedMethodsBuilder::buildMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0
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\Members;
9
10
use PhpParser\Node\Stmt\ClassMethod;
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
final class ParsedMethodsBuilder implements MethodsBuilder
19
{
20 45
    public function __construct(
21
        private readonly ParametersBuilder $parametersBuilder,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 21 at column 25
Loading history...
22
        private readonly TypeBuilder $typeBuilder,
23
        private readonly VisibilityBuilder $visibilityBuilder,
24
    ) {
25
    }
26
27
    /**
28
     * @param ClassMethod[] $methods
29
     * @return Method[]
30
     */
31 34
    public function build(array $methods, UseStatements $useStatements): array
32
    {
33 34
        return array_map(
34 34
            fn (ClassMethod $method): Method => $this->buildMethod($method, $useStatements),
35
            $methods
36
        );
37
    }
38
39 22
    private function buildMethod(ClassMethod $method, UseStatements $useStatements): Method
40
    {
41 22
        $name = $method->name->name;
42 22
        $visibility = $this->visibilityBuilder->build($method);
43 22
        $docBlock = $method->getDocComment();
44 22
        $returnType = $this->typeBuilder->fromMethodReturnType($method->returnType, $docBlock, $useStatements);
45 22
        $parameters = $this->parametersBuilder->build($method->params, $docBlock, $useStatements);
46
        return match (true) {
47 22
            $method->isAbstract() => new Method($name, $visibility, $returnType, $parameters, isAbstract: true),
48 22
            $method->isStatic() => new Method($name, $visibility, $returnType, $parameters, isStatic: true),
49 22
            default => new Method($name, $visibility, $returnType, $parameters),
50
        };
51
    }
52
}
53