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 ( d0216f...e7f575 )
by SignpostMarv
02:16
created

PropertyReflectionExtension::isPublic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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