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 ( 3b53b2...79440d )
by SignpostMarv
02:59
created

PropertyReflectionExtension::SetupReflections()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 14
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
use SignpostMarv\DaftObject\DefinesOwnUntypedIdInterface;
21
22
class PropertyReflectionExtension implements PropertyReflection
23
{
24
    /**
25
    * @var Type
26
    */
27
    protected $type;
28
29
    /**
30
    * @var Broker
31
    */
32
    protected $broker;
33
34
    /**
35
    * @var bool
36
    */
37
    protected $readable = false;
38
39
    /**
40
    * @var bool
41
    */
42
    protected $writeable = false;
43
44
    /**
45
    * @var bool
46
    */
47
    protected $public;
48
49
    /**
50
    * @var ClassReflection|null
51
    */
52
    protected $readableReflection;
53
54
    /**
55
    * @var ClassReflection|null
56
    */
57
    protected $writeableReflection;
58
59
    public function __construct(ClassReflection $classReflection, Broker $broker, string $property)
60
    {
61
        if (false === is_a($classReflection->getName(), DaftObject::class, true)) {
62
            throw new InvalidArgumentException(sprintf('%s is not an implementation of %s',
63
                $classReflection->getName(),
64
                DaftObject::class
65
            ));
66
        }
67
68
        $this->broker = $broker;
69
70
        $this->public = static::PropertyIsPublic($classReflection->getName(), $property);
71
72
        $this->type = new MixedType();
73
74
        $this->SetupReflections($classReflection, $property);
75
    }
76
77
    public function getType() : Type
78
    {
79
        return $this->type;
80
    }
81
82
    public function isReadable() : bool
83
    {
84
        return $this->readable;
85
    }
86
87
    public function isWritable() : bool
88
    {
89
        return $this->writeable;
90
    }
91
92
    public function isPublic() : bool
93
    {
94
        return $this->public;
95
    }
96
97
    public function isPrivate() : bool
98
    {
99
        return false === $this->isPublic();
100
    }
101
102
    public function isStatic() : bool
103
    {
104
        return false;
105
    }
106
107
    public function getDeclaringClass() : ClassReflection
108
    {
109
        /**
110
        * @var ClassReflection $reflection
111
        */
112
        $reflection = $this->readable ? $this->readableReflection : $this->writeableReflection;
113
114
        return $reflection;
115
    }
116
117
    protected function SetupReflections(ClassReflection $classReflection, string $property) : void
118
    {
119
        $class = $classReflection->getName();
120
        $get = 'Get' . ucfirst($property);
121
        $set = 'Set' . ucfirst($property);
122
123
        $this->writeableReflection = $this->readableReflection = $classReflection;
124
125
        if ($classReflection->getNativeReflection()->hasMethod($get)) {
126
            $this->readableReflection = $this->SetGetterProps(new ReflectionMethod($class, $get));
127
        }
128
129
        if ($classReflection->getNativeReflection()->hasMethod($set)) {
130
            $this->writeableReflection = $this->SetSetterProps($class, $set);
131
        }
132
    }
133
134
    protected function SetGetterProps(ReflectionMethod $refMethod) : ClassReflection
135
    {
136
        $this->readable = true;
137
138
        if ($refMethod->isStatic()) {
139
            throw new InvalidArgumentException(
140
                'Implementations of ' .
141
                DaftObject::class .
142
                ' must not contain static getters.'
143
            );
144
        }
145
146
        if ($refMethod->hasReturnType()) {
147
            $this->type = TypehintHelper::decideTypeFromReflection($refMethod->getReturnType());
148
        }
149
150
        return static::DetermineDeclaringClass($this->broker, $refMethod);
151
    }
152
153
    protected function SetSetterProps(string $class, string $set) : ClassReflection
154
    {
155
        $refMethod = new ReflectionMethod($class, $set);
156
        $this->writeable = true;
157
158
        $refParam = $refMethod->getParameters()[0];
159
160
        if ($refParam->hasType()) {
161
            $this->type = TypehintHelper::decideTypeFromReflection(
162
                $refParam->getType(),
163
                null,
164
                $class,
165
                false
166
            );
167
        }
168
169
        return static::DetermineDeclaringClass($this->broker, $refMethod);
170
    }
171
172
    protected static function DetermineDeclaringClass(
173
        Broker $broker,
174
        ReflectionMethod $refMethod
175
    ) : ClassReflection {
176
        return $broker->getClassFromReflection(
177
            $refMethod->getDeclaringClass(),
178
            $refMethod->getDeclaringClass()->getName(),
179
            $refMethod->getDeclaringClass()->isAnonymous()
180
        );
181
    }
182
183
    protected static function PropertyIsPublic(string $className, string $property) : bool
184
    {
185
        return
186
            (is_a($className, DefinesOwnUntypedIdInterface::class, true) && 'id' === $property) ||
187
            in_array($property, $className::DaftObjectPublicGetters(), true) ||
188
            in_array($property, $className::DaftObjectPublicSetters(), true);
189
    }
190
}
191