Passed
Pull Request — 5.0 (#11)
by Luis
11:53 queued 09:06
created

ParsedConstantsBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 8.0
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\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...
11
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...
12
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...
13
use PhUml\Code\Attributes\Constant;
14
use PhUml\Code\Variables\TypeDeclaration;
15
16
/**
17
 * It builds an array of `Constants` for either a `ClassDefinition` or an `InterfaceDefinition`
18
 */
19
final class ParsedConstantsBuilder implements ConstantsBuilder
20
{
21
    /** @var string[] */
22
    private const TYPES = [
23
        'integer' => 'int',
24
        'double' => 'float',
25
        'string' => 'string',
26
    ];
27
28 43
    public function __construct(private VisibilityBuilder $visibilityBuilder)
29
    {
30 43
    }
31
32
    /**
33
     * @param ClassConst[] $classConstants
34
     * @return Constant[]
35
     */
36 28
    public function build(array $classConstants): array
37
    {
38 28
        return array_map(fn (ClassConst $constant): Constant => new Constant(
39 16
            (string) $constant->consts[0]->name,
40 16
            TypeDeclaration::from($this->determineType($constant->consts[0])),
41 16
            $this->visibilityBuilder->build($constant)
42 28
        ), $classConstants);
43
    }
44
45 16
    private function determineType(Const_ $constant): ?string
46
    {
47 16
        if (property_exists($constant->value, 'value')) {
48 15
            return self::TYPES[\gettype($constant->value->value)];
49
        }
50 12
        if (! $constant->value instanceof ConstFetch) {
51 1
            return null;
52
        }
53 11
        return 'bool';
54
    }
55
}
56