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 ( a22227...baeacc )
by SignpostMarv
04:37
created

PropertyReflectionExtension::PropertyIsPublic()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 4
nop 2
dl 0
loc 6
rs 9.2
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
    private $type;
28
29
    /**
30
    * @var Broker
31
    */
32
    private $broker;
33
34
    /**
35
    * @var bool
36
    */
37
    private $readable = false;
38
39
    /**
40
    * @var bool
41
    */
42
    private $writeable = false;
43
44
    /**
45
    * @var bool
46
    */
47
    private $public;
48
49
    /**
50
    * @var ClassReflection
51
    */
52
    private $readableDeclaringClass;
53
54
    /**
55
    * @var ClassReflection
56
    */
57
    private $writeableDeclaringClass;
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(
63
                $classReflection->getName() .
64
                ' is not an implementation of ' .
65
                DaftObject::class
66
            );
67
        }
68
69
        $this->broker = $broker;
70
71
        $className = $classReflection->getName();
72
73
        $this->public = static::PropertyIsPublic($className, $property);
74
75
        $this->type = new MixedType();
76
77
        $getter = 'Get' . ucfirst($property);
78
        $setter = 'Set' . ucfirst($property);
79
80
        $this->readableDeclaringClass = $classReflection;
81
        $this->writeableDeclaringClass = $classReflection;
82
83
        if ($classReflection->getNativeReflection()->hasMethod($getter)) {
84
            $refMethod = new ReflectionMethod($className, $getter);
85
86
            $this->readableDeclaringClass = $this->SetGetterProps($refMethod);
87
        }
88
89
        if ($classReflection->getNativeReflection()->hasMethod($setter)) {
90
            $refMethod = new ReflectionMethod($className, $setter);
91
92
            $this->writeableDeclaringClass = $this->SetSetterProps($className, $refMethod);
93
        }
94
    }
95
96
    public function getType() : Type
97
    {
98
        return $this->type;
99
    }
100
101
    public function isReadable() : bool
102
    {
103
        return $this->readable;
104
    }
105
106
    public function isWritable() : bool
107
    {
108
        return $this->writeable;
109
    }
110
111
    public function isPublic() : bool
112
    {
113
        return $this->public;
114
    }
115
116
    public function isPrivate() : bool
117
    {
118
        return false === $this->isPublic();
119
    }
120
121
    public function isStatic() : bool
122
    {
123
        return false;
124
    }
125
126
    public function getDeclaringClass() : ClassReflection
127
    {
128
        if ($this->readable) {
129
            return $this->readableDeclaringClass;
130
        }
131
132
        return $this->writeableDeclaringClass;
133
    }
134
135
    private function SetGetterProps(ReflectionMethod $refMethod) : ClassReflection
136
    {
137
        $this->readable = true;
138
139
        if ($refMethod->isStatic()) {
140
            throw new InvalidArgumentException(
141
                'Implementations of ' .
142
                DaftObject::class .
143
                ' must not contain static getters.'
144
            );
145
        }
146
147
        if ($refMethod->hasReturnType()) {
148
            $this->type = TypehintHelper::decideTypeFromReflection($refMethod->getReturnType());
149
        }
150
151
        return static::DetermineDeclaringClass($this->broker, $refMethod);
152
    }
153
154
    private function SetSetterProps(string $class, ReflectionMethod $refMethod) : ClassReflection
155
    {
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
    private 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
    private 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