Passed
Push — master ( d46d5f...674e23 )
by Luis
44s queued 12s
created

TagTypeFactory::parameterTypeFrom()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 2
nop 2
dl 0
loc 21
rs 9.9332
c 1
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;
9
10
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\DocBlock\Tags\InvalidTag 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 phpDocumentor\Reflection\DocBlock\Tags\Param;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\DocBlock\Tags\Param 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 phpDocumentor\Reflection\DocBlock\Tags\TagWithType;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\DocBlock\Tags\TagWithType 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 phpDocumentor\Reflection\DocBlockFactory;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\DocBlockFactory 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 phpDocumentor\Reflection\Type;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\Type 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...
15
use phpDocumentor\Reflection\Types\Compound;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\Types\Compound 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...
16
use phpDocumentor\Reflection\Types\Nullable;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\Types\Nullable 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...
17
18
final class TagTypeFactory
19
{
20
    public function __construct(private DocBlockFactory $factory)
21
    {
22
    }
23
24
    public function parameterTypeFrom(string $methodComment, string $parameterName): ?TagType
25
    {
26
        $docBlock = $this->factory->create($methodComment);
27
28
        /** @var TagType[]|InvalidTag[] $parameterTags */
29
        $parameterTags = $docBlock->getTagsByName('param');
30
31
        /** @var Param[] $params */
32
        $params = array_values(array_filter(
33
            $parameterTags,
34
            static fn (TagWithType|InvalidTag $parameter) =>
35
               $parameter instanceof Param && "\${$parameter->getVariableName()}" === $parameterName
36
        ));
37
38
        if (count($params) < 1) {
39
            return null;
40
        }
41
42
        [$param] = $params;
43
44
        return $this->resolveType($param->getType());
45
    }
46
47
    public function returnTypeFrom(string $methodComment): ?TagType
48
    {
49
        $docBlock = $this->factory->create($methodComment);
50
51
        /** @var TagWithType[]|InvalidTag[] $returnTags */
52
        $returnTags = $docBlock->getTagsByName('return');
53
54
        if (count($returnTags) < 1) {
55
            return null;
56
        }
57
58
        [$return] = $returnTags;
59
        if ($return instanceof InvalidTag) {
60
            return null;
61
        }
62
63
        return $this->resolveType($return->getType());
64
    }
65
66
    public function attributeTypeFrom(string $attributeComment): ?TagType
67
    {
68
        $docBlock = $this->factory->create($attributeComment);
69
70
        /** @var TagWithType[]|InvalidTag[] $varTags */
71
        $varTags = $docBlock->getTagsByName('var');
72
73
        if (count($varTags) < 1) {
74
            return null;
75
        }
76
77
        [$var] = $varTags;
78
        if ($var instanceof InvalidTag) {
79
            return null;
80
        }
81
82
        return $this->resolveType($var->getType());
83
    }
84
85
    private function resolveType(?Type $type): ?TagType
86
    {
87
        return match (true) {
88
            $type === null => null,
89
            $type instanceof Nullable => TagType::nullable((string) $type->getActualType()),
0 ignored issues
show
Bug introduced by
The method getActualType() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
            $type instanceof Nullable => TagType::nullable((string) $type->/** @scrutinizer ignore-call */ getActualType()),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
            $type instanceof Compound => TagType::compound(array_map('strval', $type->getIterator()->getArrayCopy())),
91
            default => TagType::named((string) $type)
92
        };
93
    }
94
}
95