Passed
Push — master ( 8d387d...2ce63d )
by Luis
03:13
created

ConstantsBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
ccs 7
cts 7
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\Raw\Builders;
9
10
use PhpParser\Node\Const_;
11
use PhpParser\Node\Expr\ConstFetch;
12
use PhpParser\Node\Scalar;
13
use PhpParser\Node\Stmt\ClassConst;
14
15
class ConstantsBuilder
16
{
17
    private static $types = [
18
        'integer' => 'int',
19
        'double' => 'float',
20
        'string' => 'string',
21
    ];
22
23
    /** @param \PhpParser\Node[] $classAttributes */
24
    public function build(array $classAttributes): array
25
    {
26 93
        $constants = array_filter($classAttributes, function ($attribute) {
27 81
            return $attribute instanceof ClassConst;
28 93
        });
29 93
        return array_map(function (ClassConst $constant) {
30
            return [
31 39
                "{$constant->consts[0]->name}",
32 39
                $this->determineType($constant->consts[0]),
33
            ];
34 93
        }, $constants);
35
    }
36
37 39
    private function determineType(Const_ $constant): ?string
38
    {
39 39
        if ($constant->value instanceof Scalar) {
40 36
            return self::$types[\gettype($constant->value->value)];
41
        }
42 6
        if ($constant->value instanceof ConstFetch) {
43 3
            if (\in_array($constant->value->name->parts[0], ['true', 'false'], true)) {
44 3
                return 'bool';
45
            }
46
        }
47 3
        return null; // It's an expression
48
    }
49
}
50