Skip to content

Commit bf923ad

Browse files
committed
Revert "Overwrite property expression type only if it's subtype of the native type"
This reverts commit eb0e0bc.
1 parent 69770e5 commit bf923ad

18 files changed

+21
-393
lines changed

src/Analyser/MutatingScope.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,12 +2119,10 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
21192119
if ($propertyReflection === null) {
21202120
return new ErrorType();
21212121
}
2122-
2123-
if (!$propertyReflection->hasNativeType()) {
2124-
return new MixedType();
2125-
}
2126-
21272122
$nativeType = $propertyReflection->getNativeType();
2123+
if ($nativeType === null) {
2124+
return new ErrorType();
2125+
}
21282126

21292127
return $this->getNullsafeShortCircuitingType($node->var, $nativeType);
21302128
}
@@ -2169,11 +2167,10 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
21692167
if ($propertyReflection === null) {
21702168
return new ErrorType();
21712169
}
2172-
if (!$propertyReflection->hasNativeType()) {
2173-
return new MixedType();
2174-
}
2175-
21762170
$nativeType = $propertyReflection->getNativeType();
2171+
if ($nativeType === null) {
2172+
return new ErrorType();
2173+
}
21772174

21782175
if ($node->class instanceof Expr) {
21792176
return $this->getNullsafeShortCircuitingType($node->class, $nativeType);

src/Analyser/NodeScopeResolver.php

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5556,18 +5556,7 @@ static function (): void {
55565556
$assignedExprType = $scope->getType($assignedExpr);
55575557
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
55585558
if ($propertyReflection->canChangeTypeAfterAssignment()) {
5559-
if ($propertyReflection->hasNativeType()) {
5560-
$propertyNativeType = $propertyReflection->getNativeType();
5561-
if ($propertyNativeType->isSuperTypeOf($assignedExprType)->yes()) {
5562-
$assignedExprNativeType = $scope->getNativeType($assignedExpr);
5563-
if (!$propertyNativeType->isSuperTypeOf($assignedExprNativeType)->yes()) {
5564-
$assignedExprNativeType = $propertyNativeType;
5565-
}
5566-
$scope = $scope->assignExpression($var, $assignedExprType, $assignedExprNativeType);
5567-
}
5568-
} else {
5569-
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
5570-
}
5559+
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
55715560
}
55725561
$declaringClass = $propertyReflection->getDeclaringClass();
55735562
if ($declaringClass->hasNativeProperty($propertyName)) {
@@ -5632,18 +5621,7 @@ static function (): void {
56325621
$assignedExprType = $scope->getType($assignedExpr);
56335622
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
56345623
if ($propertyReflection !== null && $propertyReflection->canChangeTypeAfterAssignment()) {
5635-
if ($propertyReflection->hasNativeType()) {
5636-
$propertyNativeType = $propertyReflection->getNativeType();
5637-
if ($propertyNativeType->isSuperTypeOf($assignedExprType)->yes()) {
5638-
$assignedExprNativeType = $scope->getNativeType($assignedExpr);
5639-
if (!$propertyNativeType->isSuperTypeOf($assignedExprNativeType)->yes()) {
5640-
$assignedExprNativeType = $propertyNativeType;
5641-
}
5642-
$scope = $scope->assignExpression($var, $assignedExprType, $assignedExprNativeType);
5643-
}
5644-
} else {
5645-
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
5646-
}
5624+
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
56475625
}
56485626
} else {
56495627
// fallback

src/Reflection/Annotations/AnnotationPropertyReflection.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Reflection\ExtendedPropertyReflection;
88
use PHPStan\ShouldNotHappenException;
99
use PHPStan\TrinaryLogic;
10-
use PHPStan\Type\MixedType;
1110
use PHPStan\Type\Type;
1211

1312
final class AnnotationPropertyReflection implements ExtendedPropertyReflection
@@ -43,26 +42,6 @@ public function isPublic(): bool
4342
return true;
4443
}
4544

46-
public function hasPhpDocType(): bool
47-
{
48-
return true;
49-
}
50-
51-
public function getPhpDocType(): Type
52-
{
53-
return $this->readableType;
54-
}
55-
56-
public function hasNativeType(): bool
57-
{
58-
return false;
59-
}
60-
61-
public function getNativeType(): Type
62-
{
63-
return new MixedType();
64-
}
65-
6645
public function getReadableType(): Type
6746
{
6847
return $this->readableType;

src/Reflection/Dummy/ChangedTypePropertyReflection.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
final class ChangedTypePropertyReflection implements WrapperPropertyReflection
1313
{
1414

15-
public function __construct(private ClassReflection $declaringClass, private ExtendedPropertyReflection $reflection, private Type $readableType, private Type $writableType, private Type $phpDocType, private Type $nativeType)
15+
public function __construct(private ClassReflection $declaringClass, private ExtendedPropertyReflection $reflection, private Type $readableType, private Type $writableType)
1616
{
1717
}
1818

@@ -41,26 +41,6 @@ public function getDocComment(): ?string
4141
return $this->reflection->getDocComment();
4242
}
4343

44-
public function hasPhpDocType(): bool
45-
{
46-
return $this->reflection->hasPhpDocType();
47-
}
48-
49-
public function getPhpDocType(): Type
50-
{
51-
return $this->phpDocType;
52-
}
53-
54-
public function hasNativeType(): bool
55-
{
56-
return $this->reflection->hasNativeType();
57-
}
58-
59-
public function getNativeType(): Type
60-
{
61-
return $this->nativeType;
62-
}
63-
6444
public function getReadableType(): Type
6545
{
6646
return $this->readableType;

src/Reflection/Dummy/DummyPropertyReflection.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,6 @@ public function isPublic(): bool
3737
return true;
3838
}
3939

40-
public function hasPhpDocType(): bool
41-
{
42-
return false;
43-
}
44-
45-
public function getPhpDocType(): Type
46-
{
47-
return new MixedType();
48-
}
49-
50-
public function hasNativeType(): bool
51-
{
52-
return false;
53-
}
54-
55-
public function getNativeType(): Type
56-
{
57-
return new MixedType();
58-
}
59-
6040
public function getReadableType(): Type
6141
{
6242
return new MixedType();

src/Reflection/ExtendedPropertyReflection.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Reflection;
44

55
use PHPStan\TrinaryLogic;
6-
use PHPStan\Type\Type;
76

87
/**
98
* The purpose of this interface is to be able to
@@ -26,14 +25,6 @@ interface ExtendedPropertyReflection extends PropertyReflection
2625

2726
public const HOOK_SET = 'set';
2827

29-
public function hasPhpDocType(): bool;
30-
31-
public function getPhpDocType(): Type;
32-
33-
public function hasNativeType(): bool;
34-
35-
public function getNativeType(): Type;
36-
3728
public function isAbstract(): TrinaryLogic;
3829

3930
public function isFinal(): TrinaryLogic;

src/Reflection/Php/EnumPropertyReflection.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Reflection\ExtendedPropertyReflection;
88
use PHPStan\ShouldNotHappenException;
99
use PHPStan\TrinaryLogic;
10-
use PHPStan\Type\MixedType;
1110
use PHPStan\Type\Type;
1211

1312
final class EnumPropertyReflection implements ExtendedPropertyReflection
@@ -42,26 +41,6 @@ public function getDocComment(): ?string
4241
return null;
4342
}
4443

45-
public function hasPhpDocType(): bool
46-
{
47-
return false;
48-
}
49-
50-
public function getPhpDocType(): Type
51-
{
52-
return new MixedType();
53-
}
54-
55-
public function hasNativeType(): bool
56-
{
57-
return false;
58-
}
59-
60-
public function getNativeType(): Type
61-
{
62-
return new MixedType();
63-
}
64-
6544
public function getReadableType(): Type
6645
{
6746
return $this->type;

src/Reflection/Php/SimpleXMLElementProperty.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Type\BooleanType;
1111
use PHPStan\Type\FloatType;
1212
use PHPStan\Type\IntegerType;
13-
use PHPStan\Type\MixedType;
1413
use PHPStan\Type\StringType;
1514
use PHPStan\Type\Type;
1615
use PHPStan\Type\TypeCombinator;
@@ -45,26 +44,6 @@ public function isPublic(): bool
4544
return true;
4645
}
4746

48-
public function hasPhpDocType(): bool
49-
{
50-
return false;
51-
}
52-
53-
public function getPhpDocType(): Type
54-
{
55-
return new MixedType();
56-
}
57-
58-
public function hasNativeType(): bool
59-
{
60-
return false;
61-
}
62-
63-
public function getNativeType(): Type
64-
{
65-
return new MixedType();
66-
}
67-
6847
public function getReadableType(): Type
6948
{
7049
return $this->type;

src/Reflection/Php/UniversalObjectCrateProperty.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Reflection\ExtendedPropertyReflection;
88
use PHPStan\ShouldNotHappenException;
99
use PHPStan\TrinaryLogic;
10-
use PHPStan\Type\MixedType;
1110
use PHPStan\Type\Type;
1211

1312
final class UniversalObjectCrateProperty implements ExtendedPropertyReflection
@@ -41,26 +40,6 @@ public function isPublic(): bool
4140
return true;
4241
}
4342

44-
public function hasPhpDocType(): bool
45-
{
46-
return false;
47-
}
48-
49-
public function getPhpDocType(): Type
50-
{
51-
return new MixedType();
52-
}
53-
54-
public function hasNativeType(): bool
55-
{
56-
return false;
57-
}
58-
59-
public function getNativeType(): Type
60-
{
61-
return new MixedType();
62-
}
63-
6443
public function getReadableType(): Type
6544
{
6645
return $this->readableType;

src/Reflection/ResolvedPropertyReflection.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,6 @@ public function isPublic(): bool
5959
return $this->reflection->isPublic();
6060
}
6161

62-
public function hasPhpDocType(): bool
63-
{
64-
return $this->reflection->hasPhpDocType();
65-
}
66-
67-
public function getPhpDocType(): Type
68-
{
69-
return $this->reflection->getPhpDocType();
70-
}
71-
72-
public function hasNativeType(): bool
73-
{
74-
return $this->reflection->hasNativeType();
75-
}
76-
77-
public function getNativeType(): Type
78-
{
79-
return $this->reflection->getNativeType();
80-
}
81-
8262
public function getReadableType(): Type
8363
{
8464
$type = $this->readableType;

src/Reflection/Type/CallbackUnresolvedPropertyPrototypeReflection.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ private function transformPropertyWithStaticType(ClassReflection $declaringClass
7979
{
8080
$readableType = $this->transformStaticType($property->getReadableType());
8181
$writableType = $this->transformStaticType($property->getWritableType());
82-
$phpDocType = $this->transformStaticType($property->getPhpDocType());
83-
$nativeType = $this->transformStaticType($property->getNativeType());
8482

85-
return new ChangedTypePropertyReflection($declaringClass, $property, $readableType, $writableType, $phpDocType, $nativeType);
83+
return new ChangedTypePropertyReflection($declaringClass, $property, $readableType, $writableType);
8684
}
8785

8886
private function transformStaticType(Type $type): Type

src/Reflection/Type/CalledOnTypeUnresolvedPropertyPrototypeReflection.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,8 @@ private function transformPropertyWithStaticType(ClassReflection $declaringClass
7474
{
7575
$readableType = $this->transformStaticType($property->getReadableType());
7676
$writableType = $this->transformStaticType($property->getWritableType());
77-
$phpDocType = $this->transformStaticType($property->getPhpDocType());
78-
$nativeType = $this->transformStaticType($property->getNativeType());
7977

80-
return new ChangedTypePropertyReflection($declaringClass, $property, $readableType, $writableType, $phpDocType, $nativeType);
78+
return new ChangedTypePropertyReflection($declaringClass, $property, $readableType, $writableType);
8179
}
8280

8381
private function transformStaticType(Type $type): Type

src/Reflection/Type/IntersectionTypePropertyReflection.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,6 @@ public function getDocComment(): ?string
8080
return null;
8181
}
8282

83-
public function hasPhpDocType(): bool
84-
{
85-
return $this->computeResult(static fn (ExtendedPropertyReflection $property) => $property->hasPhpDocType());
86-
}
87-
88-
public function getPhpDocType(): Type
89-
{
90-
return TypeCombinator::intersect(...array_map(static fn (ExtendedPropertyReflection $property): Type => $property->getPhpDocType(), $this->properties));
91-
}
92-
93-
public function hasNativeType(): bool
94-
{
95-
return $this->computeResult(static fn (ExtendedPropertyReflection $property) => $property->hasNativeType());
96-
}
97-
98-
public function getNativeType(): Type
99-
{
100-
return TypeCombinator::intersect(...array_map(static fn (ExtendedPropertyReflection $property): Type => $property->getNativeType(), $this->properties));
101-
}
102-
10383
public function getReadableType(): Type
10484
{
10585
return TypeCombinator::intersect(...array_map(static fn (ExtendedPropertyReflection $property): Type => $property->getReadableType(), $this->properties));

0 commit comments

Comments
 (0)