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 ( 173404...51e5d1 )
by SignpostMarv
02:28
created

PropertyReflectionExtension::SetSetterProps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 2
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject\PHPStan;
10
11
use InvalidArgumentException;
12
use PHPStan\Broker\Broker;
13
use PHPStan\Reflection\ClassReflection;
14
use PHPStan\Reflection\PropertyReflection;
15
use PHPStan\Type\MixedType;
16
use PHPStan\Type\Type;
17
use PHPStan\Type\TypehintHelper;
18
use ReflectionMethod;
19
use SignpostMarv\DaftObject\DaftObject;
20
21
class PropertyReflectionExtension implements PropertyReflection
22
{
23
    /**
24
    * @var Type
25
    */
26
    private $type;
27
28
    /**
29
    * @var Broker
30
    */
31
    private $broker;
32
33
    /**
34
    * @var bool
35
    */
36
    private $readable = false;
37
38
    /**
39
    * @var bool
40
    */
41
    private $writeable = false;
42
43
    /**
44
    * @var bool
45
    */
46
    private $public;
47
48
    /**
49
    * @var ClassReflection
50
    */
51
    private $readableDeclaringClass;
52
53
    /**
54
    * @var ClassReflection
55
    */
56
    private $writeableDeclaringClass;
57
58
    public function __construct(
59
        ClassReflection $classReflection,
60
        Broker $broker,
61
        string $propertyName
62
    ) {
63
        if (
64
            false === is_a(
65
                $classReflection->getName(),
66
                DaftObject::class,
67
                true
68
            )
69
        ) {
70
            throw new InvalidArgumentException(
71
                $classReflection->getName() .
72
                ' is not an implementation of ' .
73
                DaftObject::class
74
            );
75
        }
76
77
        $this->broker = $broker;
78
79
        $className = $classReflection->getName();
80
81
        $this->public =
82
            in_array(
83
                $propertyName,
84
                $className::DaftObjectPublicGetters(),
85
                true
86
            ) ||
87
            in_array(
88
                $propertyName,
89
                $className::DaftObjectPublicSetters(),
90
                true
91
            );
92
93
        $this->type = new MixedType();
94
95
        $getter = 'Get' . ucfirst($propertyName);
96
        $setter = 'Set' . ucfirst($propertyName);
97
98
        $this->readableDeclaringClass = $classReflection;
99
        $this->writeableDeclaringClass = $classReflection;
100
101
        if ($classReflection->getNativeReflection()->hasMethod($getter)) {
102
            $refMethod = new ReflectionMethod($className, $getter);
103
104
            $this->readableDeclaringClass = $this->SetGetterProps($refMethod);
105
        }
106
107
        if ($classReflection->getNativeReflection()->hasMethod($setter)) {
108
            $refMethod = new ReflectionMethod($className, $setter);
109
110
            $this->writeableDeclaringClass = $this->SetSetterProps(
111
                $className,
112
                $refMethod
113
            );
114
        }
115
    }
116
117
    public function getType() : Type
118
    {
119
        return $this->type;
120
    }
121
122
    public function isReadable() : bool
123
    {
124
        return $this->readable;
125
    }
126
127
    public function isWritable() : bool
128
    {
129
        return $this->writeable;
130
    }
131
132
    public function isPublic() : bool
133
    {
134
        return $this->public;
135
    }
136
137
    public function isPrivate() : bool
138
    {
139
        return false === $this->isPublic();
140
    }
141
142
    public function isStatic() : bool
143
    {
144
        return false;
145
    }
146
147
    public function getDeclaringClass() : ClassReflection
148
    {
149
        if ($this->readable) {
150
            return $this->readableDeclaringClass;
151
        }
152
153
        return $this->writeableDeclaringClass;
154
    }
155
156
    private function SetGetterProps(
157
        ReflectionMethod $refMethod
158
    ) : ClassReflection {
159
        $this->readable = true;
160
161
        if ($refMethod->isStatic()) {
162
            throw new InvalidArgumentException(
163
                'Implementations of ' .
164
                DaftObject::class .
165
                ' must not contain static getters.'
166
            );
167
        }
168
169
        if ($refMethod->hasReturnType()) {
170
            $this->type = TypehintHelper::decideTypeFromReflection(
171
                $refMethod->getReturnType()
172
            );
173
        }
174
175
        return static::DetermineDeclaringClass(
176
            $this->broker,
177
            $refMethod
178
        );
179
    }
180
181
    private function SetSetterProps(
182
        string $className,
183
        ReflectionMethod $refMethod
184
    ) : ClassReflection {
185
        $this->writeable = true;
186
187
        $refParam = $refMethod->getParameters()[0];
188
189
        if ($refParam->hasType()) {
190
            $this->type = TypehintHelper::decideTypeFromReflection(
191
                $refParam->getType(),
192
                null,
193
                $className,
194
                false
195
            );
196
        }
197
198
        return static::DetermineDeclaringClass(
199
            $this->broker,
200
            $refMethod
201
        );
202
    }
203
204
    private static function DetermineDeclaringClass(
205
        Broker $broker,
206
        ReflectionMethod $refMethod
207
    ) : ClassReflection {
208
        return $broker->getClassFromReflection(
209
            $refMethod->getDeclaringClass(),
210
            $refMethod->getDeclaringClass()->getName(),
211
            $refMethod->getDeclaringClass()->isAnonymous()
212
        );
213
    }
214
}
215