Passed
Push — cleanup ( 1df3c6...d09870 )
by Luis
12:15
created

MethodsBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 5 1
A resolveVisibility() 0 9 3
A buildMethod() 0 7 1
A buildParameters() 0 8 1
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\Raw\Builders;
9
10
use PhpParser\Node\Param;
11
use PhpParser\Node\Stmt\ClassMethod;
12
13
/**
14
 * It builds an array with the meta-information of a method
15
 *
16
 * The generated array has the following structure
17
 *
18
 * - name
19
 * - visibility
20
 * - parameters
21
 *    - name
22
 *    - type
23
 * - doc block
24
 */
25
class MethodsBuilder
26
{
27
    /** @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $definition */
28
    public function build($definition): array
29
    {
30
        return array_map(function (ClassMethod $method) {
31
            return $this->buildMethod($method);
32
        }, $definition->getMethods());
33
    }
34
35
    public function buildMethod(ClassMethod $method): array
36
    {
37
        return [
38
            $method->name,
39
            $this->resolveVisibility($method),
40
            $this->buildParameters($method->params),
41
            $method->getDocComment(),
42
        ];
43
    }
44
45
    private function resolveVisibility(ClassMethod $statement): string
46
    {
47
        switch (true) {
48
            case $statement->isPublic():
49
                return 'public';
50
            case $statement->isPrivate():
51
                return 'private';
52
            default:
53
                return 'protected';
54
        }
55
    }
56
57
    private function buildParameters(array $parameters): array
58
    {
59
        return array_map(function (Param $parameter) {
60
            return [
61
                "\${$parameter->name}",
62
                $parameter->type,
63
            ];
64
        }, $parameters);
65
    }
66
}
67