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.
Completed
Push — master ( 95bd65...07bf17 )
by SignpostMarv
01:59
created

PropertyReflectionExtension::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 35
nc 8
nop 3
dl 0
loc 59
rs 8.7117
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $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
        $this->type = $type;
117
    }
118
119
    public function getType() : Type
120
    {
121
        return $this->type;
122
    }
123
124
    public function isReadable() : bool
125
    {
126
        return $this->readable;
127
    }
128
129
    public function isWritable() : bool
130
    {
131
        return $this->writeable;
132
    }
133
134
    public function isPublic() : bool
135
    {
136
        return $this->public;
137
    }
138
139
    public function isPrivate() : bool
140
    {
141
        return false === $this->isPublic();
142
    }
143
144
    public function isStatic() : bool
145
    {
146
        return false;
147
    }
148
149
    public function getDeclaringClass() : ClassReflection
150
    {
151
        if ($this->readable) {
152
            return $this->readableDeclaringClass;
153
        }
154
155
        return $this->writeableDeclaringClass;
156
    }
157
158
    private function SetGetterProps(
159
        ReflectionMethod $refMethod
160
    ) : ClassReflection {
161
        $this->readable = true;
162
163
        if ($refMethod->isStatic()) {
164
            throw new InvalidArgumentException(
165
                'Implementations of ' .
166
                DaftObject::class .
167
                ' must not contain static getters.'
168
            );
169
        }
170
171
        if ($refMethod->hasReturnType()) {
172
            $type = TypehintHelper::decideTypeFromReflection(
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
173
                $refMethod->getReturnType()
174
            );
175
        }
176
177
        return static::DetermineDeclaringClass(
178
            $this->broker,
179
            $refMethod
180
        );
181
    }
182
183
    private function SetSetterProps(
184
        string $className,
185
        ReflectionMethod $refMethod
186
    ) : ClassReflection {
187
        $this->writeable = true;
188
189
        if ($refMethod->getNumberOfRequiredParameters() < 1) {
190
            throw new InvalidArgumentException(
191
                'Implementations of ' .
192
                DaftObject::class .
193
                ' must require at least one parameter on all setters!'
194
            );
195
        }
196
197
        $refParam = $refMethod->getParameters()[0];
198
199
        if ($refParam->hasType()) {
0 ignored issues
show
Bug introduced by
The method hasType() does not exist on ReflectionParameter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
        if ($refParam->/** @scrutinizer ignore-call */ hasType()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
200
            $type = TypehintHelper::decideTypeFromReflection(
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
201
                $refParam->getType(),
0 ignored issues
show
Bug introduced by
The method getType() does not exist on ReflectionParameter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
                $refParam->/** @scrutinizer ignore-call */ getType(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
202
                null,
203
                $className,
204
                false
205
            );
206
        }
207
208
        return static::DetermineDeclaringClass(
209
            $this->broker,
210
            $refMethod
211
        );
212
    }
213
214
    private static function DetermineDeclaringClass(
215
        Broker $broker,
216
        ReflectionMethod $refMethod
217
    ) : ClassReflection {
218
        return $broker->getClassFromReflection(
219
            $refMethod->getDeclaringClass(),
220
            $refMethod->getDeclaringClass()->getName()
221
        );
222
    }
223
}
224