Passed
Push — master ( deecf0...b54cbd )
by Luis
03:32
created

MethodsBuilder::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
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\Builders;
9
10
use PhpParser\Node\Stmt\ClassMethod;
11
12
class MethodsBuilder
13
{
14
    /** @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $definition */
15 54
    public function build($definition): array
16
    {
17 54
        $methods = [];
18 54
        foreach ($definition->getMethods() as $method) {
19 36
            $methods[] = $this->buildMethod($method);
20
        }
21 54
        return $methods;
22
    }
23
24 36
    public function buildMethod(ClassMethod $method): array
25
    {
26
        return [
27 36
            $method->name,
28 36
            $this->resolveVisibility($method),
29 36
            $this->buildParameters($method->params),
30 36
            $method->getDocComment(),
31
        ];
32
    }
33
34 36
    private function resolveVisibility(ClassMethod $statement): string
35
    {
36
        switch (true) {
37 36
            case $statement->isPublic():
38 36
                return 'public';
39 27
            case $statement->isPrivate():
40 27
                return 'private';
41
            default:
42 3
                return 'protected';
43
        }
44
    }
45
46 36
    private function buildParameters(array $parameters): array
47
    {
48 36
        $params = [];
49
        /** @var \PhpParser\Node\Param $parameter */
50 36
        foreach ($parameters as $parameter) {
51 36
            $params[] = [
52 36
                $parameter->type,
53 36
                "\${$parameter->name}",
54
            ];
55
        }
56 36
        return $params;
57
    }
58
}
59