Passed
Push — 1.5 ( c7c5a4...fe8795 )
by Luis
06:06
created

MethodsBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.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\Param;
11
use PhpParser\Node\Stmt\ClassMethod;
12
use PhUml\Code\Methods\AbstractMethod;
13
use PhUml\Code\Methods\Method;
14
use PhUml\Code\Methods\MethodDocBlock;
15
use PhUml\Code\Methods\StaticMethod;
16
use PhUml\Code\Variables\TypeDeclaration;
17
use PhUml\Code\Variables\Variable;
18
19
/**
20
 * It builds an array with `Method`s for either a `ClassDefinition` or an `InterfaceDefinition`
21
 *
22
 * It can run one or more `VisibilityFilter`s
23
 *
24
 * @see PrivateVisibilityFilter
25
 * @see ProtectedVisibilityFilter
26
 */
27
class MethodsBuilder extends MembersBuilder
28
{
29
    /**
30
     * @param ClassMethod[] $classMethods
31
     * @return Method[]
32
     */
33
    public function build(array $classMethods): array
34
    {
35 99
        return array_map(function (ClassMethod $method) {
36 81
            return $this->buildMethod($method);
37 99
        }, $this->runFilters($classMethods));
38
    }
39
40 81
    private function buildMethod(ClassMethod $method): Method
41
    {
42 81
        $name = $method->name;
43 81
        $modifier = $this->resolveVisibility($method);
44 81
        $comment = $method->getDocComment();
45 81
        $returnType = MethodDocBlock::from($comment)->returnType();
46 81
        $parameters = $method->params;
47 81
        if ($method->isAbstract()) {
48 27
            return AbstractMethod::$modifier($name, $this->buildParameters($parameters, $comment), $returnType);
49
        }
50 81
        if ($method->isStatic()) {
51 60
            return StaticMethod::$modifier($name, $this->buildParameters($parameters, $comment), $returnType);
52
        }
53 81
        return Method::$modifier($name, $this->buildParameters($parameters, $comment), $returnType);
54
    }
55
56
    private function buildParameters(array $parameters, ?string $docBlock): array
57
    {
58 81
        return array_map(function (Param $parameter) use ($docBlock) {
59 72
            $name = "\${$parameter->name}";
60 72
            $type = $parameter->type;
61 72
            if ($type !== null) {
62 36
                $typeDeclaration = TypeDeclaration::from($type);
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type PhpParser\Node\NullableType; however, parameter $text of PhUml\Code\Variables\TypeDeclaration::from() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
                $typeDeclaration = TypeDeclaration::from(/** @scrutinizer ignore-type */ $type);
Loading history...
63
            } else {
64 72
                $typeDeclaration = MethodDocBlock::from($docBlock)->typeOfParameter($name);
65
            }
66 72
            return Variable::declaredWith($name, $typeDeclaration);
67 81
        }, $parameters);
68
    }
69
}
70