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.
Passed
Push — master ( 04dd15...1f758d )
by SignpostMarv
06:31
created

DefinitionAssistant::SetterOrGetterClosure()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 18
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 1
nop 3
crap 3
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftObject;
8
9
use Closure;
10
use InvalidArgumentException;
11
use SignpostMarv\DaftMagicPropertyAnalysis\DefinitionAssistant as Base;
12
13
/**
14
* @template-extends Base<DaftObject>
15
*/
16
class DefinitionAssistant extends Base
17
{
18
    const BOOL_EXPECTING_GETTER = false;
19
20
    const BOOL_EXPECTING_SETTER = true;
21
22
    const INT_ARRAY_INDEX_TYPE = 0;
23
24
    const INT_ARRAY_INDEX_GETTER = 1;
25
26
    const INT_ARRAY_INDEX_SETTER = 2;
27
28 46
    public static function IsTypeUnregistered(string $type) : bool
29
    {
30 46
        if ( ! is_a($type, DaftObject::class, true)) {
31 4
            throw new InvalidArgumentException(
32
                'Argument 1 passed to ' .
33
                __METHOD__ .
34
                '() must be an implementation of ' .
35
                DaftObject::class .
36
                ', ' .
37 4
                $type .
38 4
                ' given!'
39
            );
40
        }
41
42 42
        return parent::IsTypeUnregistered($type);
43
    }
44
45
    /**
46
    * @psalm-param class-string<AbstractDaftObject> $maybe
47
    */
48 36
    public static function RegisterAbstractDaftObjectType(string $maybe) : void
49
    {
50
        /**
51
        * @var array<int, string>
52
        */
53 36
        $props = TypeCertainty::EnsureArgumentIsArray($maybe::PROPERTIES);
54
55 36
        static::RegisterDaftObjectTypeFromTypeAndProps($maybe, ...$props);
56 36
    }
57
58 38
    public static function ObtainExpectedProperties($maybe) : array
59
    {
60 38
        $maybe = is_object($maybe) ? get_class($maybe) : $maybe;
61
62 38
        if (static::IsTypeUnregistered($maybe)) {
63 34
            if (TypeParanoia::IsThingStrings($maybe, AbstractDaftObject::class)) {
64 34
                static::RegisterAbstractDaftObjectType($maybe);
65
            }
66
        }
67
68 36
        $maybe = self::MaybeRegisterAdditionalTypes($maybe);
69
70 36
        return parent::ObtainExpectedProperties($maybe);
71
    }
72
73
    /**
74
    * @psalm-param class-string<DaftObject> $type
75
    */
76 4
    public static function AutoRegisterType(string $type, string ...$properties) : void
77
    {
78 4
        static::RegisterDaftObjectTypeFromTypeAndProps($type, ...$properties);
79 4
    }
80
81
    /**
82
    * @psalm-return Closure(string):?string
83
    */
84 38
    public static function SetterOrGetterClosure(
85
        string $type,
86
        bool $SetNotGet,
87
        string ...$props
88
    ) : Closure {
89
        return function (string $property) use ($type, $props, $SetNotGet) : ? string {
90 4
            if (TypeParanoia::MaybeInArray($property, $props)) {
91
                /**
92
                * @var string
93
                */
94 4
                $method = TypeUtilities::MethodNameFromProperty($property, $SetNotGet);
95
96 4
                if (method_exists($type, $method)) {
97 4
                    return $method;
98
                }
99
            }
100
101 2
            return null;
102 38
        };
103
    }
104
105
    /**
106
    * @psalm-param class-string<DaftObject> $type
107
    *
108
    * @psalm-return array{0:class-string<DaftObject>, 1:null|Closure(string):?string, 2:null|Closure(string):?string, 4:string}
109
    */
110 38
    private static function TypeAndGetterAndSetterClosureWithProps(
111
        string $type,
112
        string ...$props
113
    ) : array {
114
        /**
115
        * @psalm-var array{0:class-string<DaftObject>, 1:null|Closure(string):?string, 2:null|Closure(string):?string, 4:string}
116
        */
117 38
        $out = array_merge(
118
            [
119 38
                $type,
120 38
                static::SetterOrGetterClosure($type, self::BOOL_EXPECTING_GETTER, ...$props),
121 38
                static::SetterOrGetterClosure($type, self::BOOL_EXPECTING_SETTER, ...$props),
122
            ],
123 38
            $props
124
        );
125
126 38
        return $out;
127
    }
128
129
    /**
130
    * @psalm-param class-string<DaftObject> $maybe
131
    */
132 38
    private static function RegisterDaftObjectTypeFromTypeAndProps(
133
        string $maybe,
134
        string ...$props
135
    ) : void {
136 38
        $args = static::TypeAndGetterAndSetterClosureWithProps($maybe, ...$props);
137
138
        /**
139
        * @var array<int, string>
140
        */
141 38
        $props = array_slice($args, 3);
142
143 38
        static::RegisterType(
144 38
            $args[self::INT_ARRAY_INDEX_TYPE],
145 38
            $args[self::INT_ARRAY_INDEX_GETTER],
146 38
            $args[self::INT_ARRAY_INDEX_SETTER],
147 19
            ...$props
148
        );
149 38
        self::MaybeRegisterAdditionalTypes($args[0]);
150 38
    }
151
152
    /**
153
    * @psalm-param class-string<DaftObject> $maybe
154
    *
155
    * @psalm-return class-string<DaftObject>
156
    */
157 38
    private static function MaybeRegisterAdditionalTypes(string $maybe) : string
158
    {
159
        foreach (
160
            [
161 38
                DefinesOwnArrayIdInterface::class,
162
                DefinesOwnIntegerIdInterface::class,
163
                DefinesOwnStringIdInterface::class,
164
            ] as $otherType
165
        ) {
166 38
            if ($otherType !== $maybe && self::IsTypeUnregistered($otherType)) {
167 38
                self::RegisterDaftObjectTypeFromTypeAndProps($otherType, 'id');
168
            }
169
        }
170
171 38
        return $maybe;
172
    }
173
}
174