TypeString   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 31
c 3
b 0
f 0
dl 0
loc 72
rs 10
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 24 8
A getUnionType() 0 17 2
A getFqnType() 0 6 4
A intersectionTypeToString() 0 9 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use ReflectionIntersectionType;
0 ignored issues
show
Bug introduced by
The type ReflectionIntersectionType 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...
8
use ReflectionNamedType;
9
use ReflectionType;
10
use ReflectionUnionType;
11
12
use function array_map;
13
use function assert;
14
use function class_exists;
15
use function implode;
16
use function sprintf;
17
18
final class TypeString
19
{
20
    /** @var string */
21
    private $nullableStr;
22
23
    public function __construct(string $nullableStr)
24
    {
25
        $this->nullableStr = $nullableStr;
26
    }
27
28
    public function __invoke(?ReflectionType $type): string
29
    {
30
        if (! $type) {
31
            return '';
32
        }
33
34
        // PHP 8.0+
35
        if (class_exists('ReflectionUnionType') && $type instanceof ReflectionUnionType) {
36
            return $this->getUnionType($type);
37
        }
38
39
        if ($type instanceof ReflectionNamedType) {
40
            $typeStr = self::getFqnType($type);
41
            // Check for Nullable in single types
42
            if ($typeStr !== 'mixed' && $type->allowsNull() && $type->getName() !== 'null') {
43
                $typeStr = $this->nullableStr . $typeStr;
44
            }
45
46
            return $typeStr;
47
        }
48
49
        assert($type instanceof ReflectionIntersectionType);
50
51
        return $this->intersectionTypeToString($type);
52
    }
53
54
    private function intersectionTypeToString(ReflectionIntersectionType $intersectionType): string
55
    {
56
        $types = $intersectionType->getTypes();
57
        /** @var array<ReflectionNamedType> $types */
58
        $typeStrings = array_map(static function (ReflectionNamedType $type): string {
59
            return '\\' . $type->getName();
60
        }, $types);
61
62
        return implode(' & ', $typeStrings);
63
    }
64
65
    public function getUnionType(ReflectionUnionType $type): string
66
    {
67
        $types = array_map(static function ($t) {
68
            if ($t instanceof ReflectionIntersectionType) {
69
                $types = $t->getTypes();
70
                /** @var array<ReflectionNamedType>  $types */
71
                $intersectionTypes = array_map(static function (ReflectionNamedType $t): string {
72
                    return self::getFqnType($t);
73
                }, $types);
74
75
                return sprintf('(%s)', implode('&', $intersectionTypes));
76
            }
77
78
            return self::getFqnType($t);
79
        }, $type->getTypes());
80
81
        return implode('|', $types);
82
    }
83
84
    private static function getFqnType(ReflectionNamedType $namedType): string
85
    {
86
        $type = $namedType->getName();
87
        $isBuiltin = $namedType->isBuiltin() || $type === 'static' || $type === 'self';
88
89
        return $isBuiltin ? $type : '\\' . $type;
90
    }
91
}
92