Passed
Pull Request — 5.0 (#11)
by Luis
11:53 queued 09:06
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 14
cts 14
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
 * 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