GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#117)
by Craig
01:09
created

DocblockFieldValidator::resolveNullable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spatie\DataTransferObject;
6
7
use RecursiveArrayIterator;
8
use RecursiveIteratorIterator;
9
10
class DocblockFieldValidator extends FieldValidator
11
{
12
    public const DOCBLOCK_REGEX = '/@var ((?:(?:[\w?|\\\\<>])+(?:\[])?)+)/';
13
14
    public function __construct(string $definition, bool $hasDefaultValue = false)
15
    {
16
        preg_match(
17
            DocblockFieldValidator::DOCBLOCK_REGEX,
18
            $definition,
19
            $matches
20
        );
21
22
        $definition = trim($matches[1] ?? '');
23
24
        $this->hasTypeDeclaration = $definition !== '';
25
        $this->hasDefaultValue = $hasDefaultValue;
26
        $this->isNullable = $this->resolveNullable($definition);
27
        $this->isMixed = $this->resolveIsMixed($definition);
28
        $this->isMixedArray = $this->resolveIsMixedArray($definition);
29
        $this->allowedTypes = $this->resolveAllowedTypes($definition);
30
        $this->allowedArrayTypes = $this->resolveAllowedArrayTypes($definition);
31
        $this->allowedArrayKeyTypes = $this->resolveAllowedArrayKeyTypes($definition);
32
    }
33
34
    private function resolveNullable(string $definition): bool
35
    {
36
        if (! $definition) {
37
            return true;
38
        }
39
40
        if (Str::contains($definition, ['mixed', 'null', '?'])) {
41
            return true;
42
        }
43
44
        return false;
45
    }
46
47
    private function resolveIsMixed(string $definition): bool
48
    {
49
        return Str::contains($definition, ['mixed']);
50
    }
51
52
    private function resolveIsMixedArray(string $definition): bool
53
    {
54
        $types = $this->normaliseTypes(...explode('|', $definition));
55
56
        foreach ($types as $type) {
57
            if (in_array($type, ['iterable', 'array'])) {
58
                return true;
59
            }
60
        }
61
62
        return false;
63
    }
64
65
    private function resolveAllowedTypes(string $definition): array
66
    {
67
        return $this->normaliseTypes(...explode('|', $definition));
68
    }
69
70
    private function resolveAllowedArrayTypes(string $definition): array
71
    {
72
        return $this->normaliseTypes(...(new RecursiveIteratorIterator(new RecursiveArrayIterator(array_map(
73
            function (string $type) {
74
                if (! $type) {
75
                    return;
76
                }
77
78
                if (strpos($type, '[]') !== false) {
79
                    return str_replace('[]', '', $type);
80
                }
81
82
                if (strpos($type, '>') !== false) {
83
                    preg_match('/([\w\\\\]+)(>)/', $type, $matches);
84
85
                    return $matches[1];
86
                }
87
88
                if (is_subclass_of($type, DataTransferObjectCollection::class)) {
89
                    return $this->resolveAllowedArrayTypesFromCollection($type);
90
                }
91
92
                return null;
93
            },
94
            explode('|', $definition)
95
        )))));
96
    }
97
98
    private function resolveAllowedArrayKeyTypes(string $definition): array
99
    {
100
        return $this->normaliseTypes(...array_map(
101
            function (string $type) {
102
                if (strpos($type, '<') === false) {
103
                    return;
104
                }
105
106
                preg_match('/(<)([\w\\\\]+)(,)/', $type, $matches);
107
108
                return $matches[2] ?? null;
109
            },
110
            explode('|', $definition)
111
        ));
112
    }
113
}
114