Passed
Push — master ( d09870...716959 )
by Luis
02:38
created

DefinitionMembersBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildParameters() 0 6 1
A extractTypeFrom() 0 18 4
A methods() 0 6 1
A attributes() 0 6 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;
9
10
use PhUml\Code\Attribute;
11
use PhUml\Code\Method;
12
use PhUml\Code\TypeDeclaration;
13
use PhUml\Code\Variable;
14
use PhUml\Parser\Raw\RawDefinition;
15
16
/**
17
 * It builds the attributes and methods of both classes and interfaces
18
 */
19
class DefinitionMembersBuilder
20
{
21
    /** @return Method[] */
22
    public function methods(RawDefinition $definition): array
23
    {
24 63
        return array_map(function (array $method) {
25 42
            [$name, $modifier, $parameters] = $method;
26 42
            return Method::$modifier($name, $this->buildParameters($parameters));
27 63
        }, $definition->methods());
28
    }
29
30
    /** @return Attribute[] */
31
    public function attributes(RawDefinition $class): array
32
    {
33 54
        return array_map(function (array $attribute) {
34 36
            [$name, $modifier, $comment] = $attribute;
35 36
            return Attribute::$modifier($name, $this->extractTypeFrom($comment));
36 54
        }, $class->attributes());
37
    }
38
39
    /** @return Variable[] */
40
    private function buildParameters(array $parameters): array
41
    {
42 42
        return array_map(function (array $parameter) {
43 42
            [$name, $type] = $parameter;
44 42
            return Variable::declaredWith($name, TypeDeclaration::from($type));
45 42
        }, $parameters);
46
    }
47
48 36
    private function extractTypeFrom(?string $comment): TypeDeclaration
49
    {
50 36
        if ($comment === null) {
51 33
            return TypeDeclaration::absent();
52
        }
53
54 9
        $type = null;  // There might be no type information in the comment
55 9
        $matches = [];
56 9
        $arrayExpression = '/^[\s*]*@var\s+array\(\s*(\w+\s*=>\s*)?(\w+)\s*\).*$/m';
57 9
        if (preg_match($arrayExpression, $comment, $matches)) {
58 9
            $type = $matches[2];
59
        } else {
60 9
            $typeExpression = '/^[\s*]*@var\s+(\S+).*$/m';
61 9
            if (preg_match($typeExpression, $comment, $matches)) {
62 9
                $type = trim($matches[1]);
63
            }
64
        }
65 9
        return TypeDeclaration::from($type);
66
    }
67
}
68