Passed
Push — variadic-params-visibility-con... ( ca0a66 )
by Luis
13:54
created

FilteredConstantsBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 7.2
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\Code\Builders\Members;
9
10
use PhpParser\Node;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use PhpParser\Node\Const_;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Const_ was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use PhpParser\Node\Expr\ConstFetch;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Expr\ConstFetch was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use PhpParser\Node\Stmt\ClassConst;
0 ignored issues
show
Bug introduced by
The type PhpParser\Node\Stmt\ClassConst was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use PhUml\Code\Attributes\Constant;
15
use PhUml\Code\Variables\TypeDeclaration;
16
17
/**
18
 * It builds an array of `Constants` for either a `ClassDefinition` or an `InterfaceDefinition`
19
 */
20
final class FilteredConstantsBuilder implements ConstantsBuilder
21
{
22
    /** @var string[] */
23
    private static $types = [
24
        'integer' => 'int',
25
        'double' => 'float',
26
        'string' => 'string',
27
    ];
28
29
    /** @var VisibilityBuilder */
30
    private $visibilityBuilder;
31
32
    /** @var VisibilityFilters */
33
    private $visibilityFilters;
34
35
    public function __construct(VisibilityBuilder $visibilityBuilder, VisibilityFilters $filters)
36
    {
37
        $this->visibilityBuilder = $visibilityBuilder;
38
        $this->visibilityFilters = $filters;
39
    }
40
41
    /**
42
     * @param Node[] $classAttributes
43
     * @return Constant[]
44
     */
45
    public function build(array $classAttributes): array
46
    {
47
        $constants = array_filter($classAttributes, static function ($attribute): bool {
48
            return $attribute instanceof ClassConst;
49
        });
50
51
        return array_map(function (ClassConst $constant): Constant {
52
            return new Constant(
53
                (string) $constant->consts[0]->name,
54
                TypeDeclaration::from($this->determineType($constant->consts[0])),
55
                $this->visibilityBuilder->build($constant)
56
            );
57
        }, $this->visibilityFilters->apply($constants));
58
    }
59
60
    private function determineType(Const_ $constant): ?string
61
    {
62
        if (property_exists($constant->value, 'value')) {
63
            return self::$types[\gettype($constant->value->value)];
64
        }
65
        if ($constant->value instanceof ConstFetch
66
            && \in_array($constant->value->name->parts[0], ['true', 'false'], true)) {
67
            return 'bool';
68
        }
69
        return null; // It's an expression
70
    }
71
}
72