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   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 1
dl 0
loc 85
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A resolveNullable() 0 12 3
A resolveIsMixed() 0 4 1
A resolveIsMixedArray() 0 12 3
A resolveAllowedTypes() 0 4 1
A resolveAllowedArrayTypes() 0 25 5
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 = $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
    }
32
33
    private function resolveNullable(string $definition): bool
34
    {
35
        if (! $definition) {
36
            return true;
37
        }
38
39
        if (Str::contains($definition, ['mixed', 'null', '?'])) {
40
            return true;
41
        }
42
43
        return false;
44
    }
45
46
    private function resolveIsMixed(string $definition): bool
47
    {
48
        return Str::contains($definition, ['mixed']);
49
    }
50
51
    private function resolveIsMixedArray(string $definition): bool
52
    {
53
        $types = $this->normaliseTypes(...explode('|', $definition));
54
55
        foreach ($types as $type) {
56
            if (in_array($type, ['iterable', 'array'])) {
57
                return true;
58
            }
59
        }
60
61
        return false;
62
    }
63
64
    private function resolveAllowedTypes(string $definition): array
65
    {
66
        return $this->normaliseTypes(...explode('|', $definition));
67
    }
68
69
    private function resolveAllowedArrayTypes(string $definition): array
70
    {
71
        return $this->normaliseTypes(...(new RecursiveIteratorIterator(new RecursiveArrayIterator(array_map(
72
            function (string $type) {
73
                if (! $type) {
74
                    return;
75
                }
76
77
                if (strpos($type, '[]') !== false) {
78
                    return str_replace('[]', '', $type);
79
                }
80
81
                if (strpos($type, 'iterable<') !== false) {
82
                    return str_replace(['iterable<', '>'], ['', ''], $type);
83
                }
84
85
                if (is_subclass_of($type, DataTransferObjectCollection::class)) {
86
                    return $this->resolveAllowedArrayTypesFromCollection($type);
87
                }
88
89
                return null;
90
            },
91
            explode('|', $definition)
92
        )))));
93
    }
94
}
95