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 ( e7f575...95bd65 )
by SignpostMarv
01:46
created

PropertyReflectionExtension::__construct()   D

Complexity

Conditions 9
Paths 18

Size

Total Lines 97
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 58
nc 18
nop 3
dl 0
loc 97
rs 4.9219
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 bool
30
    */
31
    private $readable = false;
32
33
    /**
34
    * @var bool
35
    */
36
    private $writeable = false;
37
38
    /**
39
    * @var bool
40
    */
41
    private $public;
42
43
    /**
44
    * @var ClassReflection
45
    */
46
    private $readableDeclaringClass;
47
48
    /**
49
    * @var ClassReflection
50
    */
51
    private $writeableDeclaringClass;
52
53
    public function __construct(
54
        ClassReflection $classReflection,
55
        Broker $broker,
56
        string $propertyName
57
    ) {
58
        if (
59
            false === is_a(
60
                $classReflection->getName(),
61
                DaftObject::class,
62
                true
63
            )
64
        ) {
65
            throw new InvalidArgumentException(
66
                $classReflection->getName() .
67
                ' is not an implementation of ' .
68
                DaftObject::class
69
            );
70
        }
71
72
        $className = $classReflection->getName();
73
74
        $this->public =
75
            in_array(
76
                $propertyName,
77
                $className::DaftObjectPublicGetters(),
78
                true
79
            ) ||
80
            in_array(
81
                $propertyName,
82
                $className::DaftObjectPublicSetters(),
83
                true
84
            );
85
86
        $type = new MixedType();
87
88
        $getter = 'Get' . ucfirst($propertyName);
89
        $setter = 'Set' . ucfirst($propertyName);
90
91
        $this->readableDeclaringClass = $classReflection;
92
        $this->writeableDeclaringClass = $classReflection;
93
94
        if ($classReflection->getNativeReflection()->hasMethod($getter)) {
95
            $this->readable = true;
96
97
            $refMethod = new ReflectionMethod($className, $getter);
98
99
            if ($refMethod->isStatic()) {
100
                throw new InvalidArgumentException(
101
                    'Implementations of ' .
102
                    DaftObject::class .
103
                    ' must not contain static getters.'
104
                );
105
            }
106
107
            if ($refMethod->hasReturnType()) {
108
                $type = TypehintHelper::decideTypeFromReflection(
109
                    $refMethod->getReturnType()
110
                );
111
            }
112
113
            $this->readableDeclaringClass = $broker->getClassFromReflection(
114
                $refMethod->getDeclaringClass(),
115
                $refMethod->getDeclaringClass()->getName()
116
            );
117
        }
118
119
        if ($classReflection->getNativeReflection()->hasMethod($setter)) {
120
            $this->writeable = true;
121
122
            $refMethod = new ReflectionMethod($className, $setter);
123
124
            if ($refMethod->getNumberOfRequiredParameters() < 1) {
125
                throw new InvalidArgumentException(
126
                    'Implementations of ' .
127
                    DaftObject::class .
128
                    ' must require at least one parameter on all setters!'
129
                );
130
            }
131
132
            $refParam = $refMethod->getParameters()[0];
133
134
            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

134
            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...
135
                $type = TypehintHelper::decideTypeFromReflection(
136
                    $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

136
                    $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...
137
                    null,
138
                    $className,
139
                    false
140
                );
141
            }
142
143
            $this->writeableDeclaringClass = $broker->getClassFromReflection(
144
                $refMethod->getDeclaringClass(),
145
                $refMethod->getDeclaringClass()->getName()
146
            );
147
        }
148
149
        $this->type = $type;
150
    }
151
152
    public function getType() : Type
153
    {
154
        return $this->type;
155
    }
156
157
    public function isReadable() : bool
158
    {
159
        return $this->readable;
160
    }
161
162
    public function isWritable() : bool
163
    {
164
        return $this->writeable;
165
    }
166
167
    public function isPublic() : bool
168
    {
169
        return $this->public;
170
    }
171
172
    public function isPrivate() : bool
173
    {
174
        return false === $this->isPublic();
175
    }
176
177
    public function isStatic() : bool
178
    {
179
        return false;
180
    }
181
182
    public function getDeclaringClass() : ClassReflection
183
    {
184
        if ($this->readable) {
185
            return $this->readableDeclaringClass;
186
        }
187
188
        return $this->writeableDeclaringClass;
189
    }
190
}
191