Passed
Push — parser-refactoring ( e758c6...b54cbd )
by Luis
11:11
created

MethodsBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildMethod() 0 7 1
A build() 0 7 2
A resolveVisibility() 0 9 3
A buildParameters() 0 11 2
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
    public function build($definition): array
16
    {
17
        $methods = [];
18
        foreach ($definition->getMethods() as $method) {
19
            $methods[] = $this->buildMethod($method);
20
        }
21
        return $methods;
22
    }
23
24
    public function buildMethod(ClassMethod $method): array
25
    {
26
        return [
27
            $method->name,
28
            $this->resolveVisibility($method),
29
            $this->buildParameters($method->params),
30
            $method->getDocComment(),
31
        ];
32
    }
33
34
    private function resolveVisibility(ClassMethod $statement): string
35
    {
36
        switch (true) {
37
            case $statement->isPublic():
38
                return 'public';
39
            case $statement->isPrivate():
40
                return 'private';
41
            default:
42
                return 'protected';
43
        }
44
    }
45
46
    private function buildParameters(array $parameters): array
47
    {
48
        $params = [];
49
        /** @var \PhpParser\Node\Param $parameter */
50
        foreach ($parameters as $parameter) {
51
            $params[] = [
52
                $parameter->type,
53
                "\${$parameter->name}",
54
            ];
55
        }
56
        return $params;
57
    }
58
}
59