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.
Test Failed
Push — master ( c776f5...aa6284 )
by SignpostMarv
07:23
created

MethodNameFromProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 5
rs 10
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
use SignpostMarv\DaftObject\TypeUtilities;
22
23
class PropertyReflectionExtension implements PropertyReflection
24
{
25
    /**
26
    * @var Type
27
    */
28
    protected $type;
29
30
    /**
31
    * @var Broker
32
    */
33
    protected $broker;
34
35
    /**
36
    * @var bool
37
    */
38
    protected $readable = false;
39
40
    /**
41
    * @var bool
42
    */
43
    protected $writeable = false;
44
45
    /**
46
    * @var bool
47
    */
48
    protected $public;
49
50
    /**
51
    * @var ClassReflection|null
52
    */
53
    protected $readableReflection;
54
55
    /**
56
    * @var ClassReflection|null
57
    */
58
    protected $writeableReflection;
59
60
    public function __construct(ClassReflection $classReflection, Broker $broker, string $property)
61
    {
62
        if (false === is_a($classReflection->getName(), DaftObject::class, true)) {
63
            throw new InvalidArgumentException(sprintf('%s is not an implementation of %s',
64
                $classReflection->getName(),
65
                DaftObject::class
66
            ));
67
        }
68
69
        $this->broker = $broker;
70
71
        $this->public = static::PropertyIsPublic($classReflection->getName(), $property);
72
73
        $this->type = new MixedType();
74
75
        $this->SetupReflections($classReflection, $property);
76
    }
77
78
    public function getType() : Type
79
    {
80
        return $this->type;
81
    }
82
83
    public function isReadable() : bool
84
    {
85
        return $this->readable;
86
    }
87
88
    public function isWritable() : bool
89
    {
90
        return $this->writeable;
91
    }
92
93
    public function isPublic() : bool
94
    {
95
        return $this->public;
96
    }
97
98
    public function isPrivate() : bool
99
    {
100
        return false === $this->isPublic();
101
    }
102
103
    public function isStatic() : bool
104
    {
105
        return false;
106
    }
107
108
    public function getDeclaringClass() : ClassReflection
109
    {
110
        /**
111
        * @var ClassReflection $reflection
112
        */
113
        $reflection = $this->readable ? $this->readableReflection : $this->writeableReflection;
114
115
        return $reflection;
116
    }
117
118
    protected function SetupReflections(ClassReflection $classReflection, string $property) : void
119
    {
120
        $class = $classReflection->getName();
121
        $getter = static::MethodNameFromProperty($property);
0 ignored issues
show
Unused Code introduced by
The assignment to $getter is dead and can be removed.
Loading history...
122
        $setter = static::MethodNameFromProperty($property, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $setter is dead and can be removed.
Loading history...
123
124
        $this->writeableReflection = $this->readableReflection = $classReflection;
125
126
        if ($classReflection->getNativeReflection()->hasMethod($get)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $get seems to be never defined.
Loading history...
127
            $this->readableReflection = $this->SetGetterProps(new ReflectionMethod($class, $get));
128
        }
129
130
        if ($classReflection->getNativeReflection()->hasMethod($set)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $set seems to be never defined.
Loading history...
131
            $this->writeableReflection = $this->SetSetterProps($class, $set);
132
        }
133
    }
134
135
    protected 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
    protected function SetSetterProps(string $class, string $set) : ClassReflection
155
    {
156
        $refMethod = new ReflectionMethod($class, $set);
157
        $this->writeable = true;
158
159
        $refParam = $refMethod->getParameters()[0];
160
161
        if ($refParam->hasType()) {
162
            $this->type = TypehintHelper::decideTypeFromReflection(
163
                $refParam->getType(),
164
                null,
165
                $class,
166
                false
167
            );
168
        }
169
170
        return static::DetermineDeclaringClass($this->broker, $refMethod);
171
    }
172
173
    protected static function MethodNameFromProperty(
174
        string $prop,
175
        bool $SetNotGet = false
176
    ) : string {
177
        return TypeUtilities::MethodNameFromProperty($prop, $SetNotGet);
178
    }
179
180
    protected static function DetermineDeclaringClass(
181
        Broker $broker,
182
        ReflectionMethod $refMethod
183
    ) : ClassReflection {
184
        $reflectionClass = $refMethod->getDeclaringClass();
185
186
        $filename = null;
187
        if (false !== $reflectionClass->getFileName()) {
188
            $filename = $reflectionClass->getFileName();
189
        }
190
191
        return $broker->getClassFromReflection(
192
            $reflectionClass,
193
            $reflectionClass->getName(),
194
            $reflectionClass->isAnonymous() ? $filename : null
195
        );
196
    }
197
198
    protected static function PropertyIsPublic(string $className, string $property) : bool
199
    {
200
        return
201
            (is_a($className, DefinesOwnUntypedIdInterface::class, true) && 'id' === $property) ||
202
            in_array($property, (array) $className::DaftObjectPublicGetters(), true) ||
203
            in_array($property, (array) $className::DaftObjectPublicSetters(), true);
204
    }
205
}
206