Skip to content

Commit c5d17f8

Browse files
committed
PhpPropertyReflection - accept Type object as native type instead of AST node
1 parent 8bc2b84 commit c5d17f8

File tree

3 files changed

+10
-24
lines changed

3 files changed

+10
-24
lines changed

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private function createProperty(
222222

223223
$phpDocType = TypeCombinator::union(...$types);
224224

225-
return new PhpPropertyReflection($declaringClassReflection, null, null, $phpDocType, $phpDocType, $classReflection->getNativeReflection()->getProperty($propertyName), null, null, null, false, false, false, false, [], false, true, false, false, true);
225+
return new PhpPropertyReflection($declaringClassReflection, null, new MixedType(), $phpDocType, $phpDocType, $classReflection->getNativeReflection()->getProperty($propertyName), null, null, null, false, false, false, false, [], false, true, false, false, true);
226226
}
227227
}
228228

@@ -331,10 +331,7 @@ private function createProperty(
331331
);
332332
}
333333

334-
$nativeType = null;
335-
if ($propertyReflection->getType() !== null) {
336-
$nativeType = $propertyReflection->getType();
337-
}
334+
$nativeType = TypehintHelper::decideTypeFromReflection($propertyReflection->getType(), selfClass: $declaringClassReflection);
338335

339336
$declaringTrait = null;
340337
$reflectionProvider = $this->reflectionProviderProvider->getReflectionProvider();

src/Reflection/Php/PhpPropertyReflection.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace PHPStan\Reflection\Php;
44

5-
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionIntersectionType;
6-
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionNamedType;
75
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty;
8-
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionUnionType;
96
use PHPStan\Reflection\AttributeReflection;
107
use PHPStan\Reflection\ClassReflection;
118
use PHPStan\Reflection\ExtendedMethodReflection;
@@ -24,8 +21,6 @@
2421
final class PhpPropertyReflection implements ExtendedPropertyReflection
2522
{
2623

27-
private ?Type $finalNativeType = null;
28-
2924
private ?Type $readableType = null;
3025

3126
private ?Type $writableType = null;
@@ -36,7 +31,7 @@ final class PhpPropertyReflection implements ExtendedPropertyReflection
3631
public function __construct(
3732
private ClassReflection $declaringClass,
3833
private ?ClassReflection $declaringTrait,
39-
private ReflectionUnionType|ReflectionNamedType|ReflectionIntersectionType|null $nativeType,
34+
private Type $nativeType,
4035
private ?Type $readablePhpDocType,
4136
private ?Type $writablePhpDocType,
4237
private ReflectionProperty $reflection,
@@ -109,10 +104,9 @@ public function isReadOnlyByPhpDoc(): bool
109104

110105
public function getReadableType(): Type
111106
{
112-
return $this->readableType ??= TypehintHelper::decideTypeFromReflection(
107+
return $this->readableType ??= TypehintHelper::decideType(
113108
$this->nativeType,
114109
$this->readablePhpDocType,
115-
$this->declaringClass,
116110
);
117111
}
118112

@@ -131,10 +125,9 @@ public function getWritableType(): Type
131125
}
132126

133127
if ($this->writablePhpDocType === null || $this->writablePhpDocType instanceof NeverType) {
134-
return $this->writableType = TypehintHelper::decideTypeFromReflection(
128+
return $this->writableType = TypehintHelper::decideType(
135129
$this->nativeType,
136130
$this->readablePhpDocType,
137-
$this->declaringClass,
138131
);
139132
}
140133

@@ -145,10 +138,9 @@ public function getWritableType(): Type
145138
return $this->writableType = $this->writablePhpDocType;
146139
}
147140

148-
return $this->writableType = TypehintHelper::decideTypeFromReflection(
141+
return $this->writableType = TypehintHelper::decideType(
149142
$this->nativeType,
150143
$this->writablePhpDocType,
151-
$this->declaringClass,
152144
);
153145
}
154146

@@ -194,15 +186,12 @@ public function getPhpDocType(): Type
194186

195187
public function hasNativeType(): bool
196188
{
197-
return $this->nativeType !== null;
189+
return !$this->nativeType instanceof MixedType || $this->nativeType->isExplicitMixed();
198190
}
199191

200192
public function getNativeType(): Type
201193
{
202-
return $this->finalNativeType ??= TypehintHelper::decideTypeFromReflection(
203-
$this->nativeType,
204-
selfClass: $this->declaringClass,
205-
);
194+
return $this->nativeType;
206195
}
207196

208197
public function isReadable(): bool

tests/PHPStan/Analyser/nsrt/integer-range-types.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ public function supportsPhpdocIntegerRange() {
190190
assertType('int<min, 100>', $this->min);
191191
assertType('int<0, max>', $this->max);
192192

193-
assertType('*ERROR*', $this->error1);
194-
assertType('*ERROR*', $this->error2);
193+
assertType('mixed', $this->error1);
194+
assertType('mixed', $this->error2);
195195
assertType('int', $this->int);
196196
}
197197

0 commit comments

Comments
 (0)