Passed
Push — master ( 1ba30a...8ac47c )
by Luis
01:05 queued 12s
created

ParsedConstantsBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 35
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 7 1
A __construct() 0 2 1
A determineType() 0 9 3
1
<?php declare(strict_types=1);
2
/**
3
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
4
 */
5
6
namespace PhUml\Parser\Code\Builders\Members;
7
8
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...
9
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...
10
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...
11
use PhUml\Code\Properties\Constant;
12
use PhUml\Code\Variables\TypeDeclaration;
13
14
/**
15
 * It builds an array of `Constants` for either a `ClassDefinition` or an `InterfaceDefinition`
16
 */
17
final class ParsedConstantsBuilder implements ConstantsBuilder
18
{
19
    /** @var string[] */
20
    private const TYPES = [
21
        'integer' => 'int',
22
        'double' => 'float',
23
        'string' => 'string',
24
    ];
25
26 45
    public function __construct(private readonly VisibilityBuilder $visibilityBuilder)
27
    {
28
    }
29
30
    /**
31
     * @param ClassConst[] $classConstants
32
     * @return Constant[]
33
     */
34 30
    public function build(array $classConstants): array
35
    {
36 30
        return array_map(fn (ClassConst $constant): Constant => new Constant(
37 17
            (string) $constant->consts[0]->name,
38 17
            TypeDeclaration::from($this->determineType($constant->consts[0])),
39 17
            $this->visibilityBuilder->build($constant)
40
        ), $classConstants);
41
    }
42
43 17
    private function determineType(Const_ $constant): ?string
44
    {
45 17
        if (property_exists($constant->value, 'value')) {
46 16
            return self::TYPES[\gettype($constant->value->value)];
47
        }
48 13
        if (! $constant->value instanceof ConstFetch) {
49 1
            return null;
50
        }
51 12
        return 'bool';
52
    }
53
}
54