Skip to content

Commit

Permalink
Revert "Overwrite property expression type only if it's subtype of th…
Browse files Browse the repository at this point in the history
…e native type"

This reverts commit eb0e0bc.
  • Loading branch information
ondrejmirtes committed Jan 19, 2025
1 parent 69770e5 commit bf923ad
Show file tree
Hide file tree
Showing 18 changed files with 21 additions and 393 deletions.
15 changes: 6 additions & 9 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2119,12 +2119,10 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
if ($propertyReflection === null) {
return new ErrorType();
}

if (!$propertyReflection->hasNativeType()) {
return new MixedType();
}

$nativeType = $propertyReflection->getNativeType();
if ($nativeType === null) {
return new ErrorType();
}

return $this->getNullsafeShortCircuitingType($node->var, $nativeType);
}
Expand Down Expand Up @@ -2169,11 +2167,10 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
if ($propertyReflection === null) {
return new ErrorType();
}
if (!$propertyReflection->hasNativeType()) {
return new MixedType();
}

$nativeType = $propertyReflection->getNativeType();
if ($nativeType === null) {
return new ErrorType();
}

if ($node->class instanceof Expr) {
return $this->getNullsafeShortCircuitingType($node->class, $nativeType);
Expand Down
26 changes: 2 additions & 24 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5556,18 +5556,7 @@ static function (): void {
$assignedExprType = $scope->getType($assignedExpr);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
if ($propertyReflection->canChangeTypeAfterAssignment()) {
if ($propertyReflection->hasNativeType()) {
$propertyNativeType = $propertyReflection->getNativeType();
if ($propertyNativeType->isSuperTypeOf($assignedExprType)->yes()) {
$assignedExprNativeType = $scope->getNativeType($assignedExpr);
if (!$propertyNativeType->isSuperTypeOf($assignedExprNativeType)->yes()) {
$assignedExprNativeType = $propertyNativeType;
}
$scope = $scope->assignExpression($var, $assignedExprType, $assignedExprNativeType);
}
} else {
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
}
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
}
$declaringClass = $propertyReflection->getDeclaringClass();
if ($declaringClass->hasNativeProperty($propertyName)) {
Expand Down Expand Up @@ -5632,18 +5621,7 @@ static function (): void {
$assignedExprType = $scope->getType($assignedExpr);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
if ($propertyReflection !== null && $propertyReflection->canChangeTypeAfterAssignment()) {
if ($propertyReflection->hasNativeType()) {
$propertyNativeType = $propertyReflection->getNativeType();
if ($propertyNativeType->isSuperTypeOf($assignedExprType)->yes()) {
$assignedExprNativeType = $scope->getNativeType($assignedExpr);
if (!$propertyNativeType->isSuperTypeOf($assignedExprNativeType)->yes()) {
$assignedExprNativeType = $propertyNativeType;
}
$scope = $scope->assignExpression($var, $assignedExprType, $assignedExprNativeType);
}
} else {
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
}
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
}
} else {
// fallback
Expand Down
21 changes: 0 additions & 21 deletions src/Reflection/Annotations/AnnotationPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;

final class AnnotationPropertyReflection implements ExtendedPropertyReflection
Expand Down Expand Up @@ -43,26 +42,6 @@ public function isPublic(): bool
return true;
}

public function hasPhpDocType(): bool
{
return true;
}

public function getPhpDocType(): Type
{
return $this->readableType;
}

public function hasNativeType(): bool
{
return false;
}

public function getNativeType(): Type
{
return new MixedType();
}

public function getReadableType(): Type
{
return $this->readableType;
Expand Down
22 changes: 1 addition & 21 deletions src/Reflection/Dummy/ChangedTypePropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class ChangedTypePropertyReflection implements WrapperPropertyReflection
{

public function __construct(private ClassReflection $declaringClass, private ExtendedPropertyReflection $reflection, private Type $readableType, private Type $writableType, private Type $phpDocType, private Type $nativeType)
public function __construct(private ClassReflection $declaringClass, private ExtendedPropertyReflection $reflection, private Type $readableType, private Type $writableType)
{
}

Expand Down Expand Up @@ -41,26 +41,6 @@ public function getDocComment(): ?string
return $this->reflection->getDocComment();
}

public function hasPhpDocType(): bool
{
return $this->reflection->hasPhpDocType();
}

public function getPhpDocType(): Type
{
return $this->phpDocType;
}

public function hasNativeType(): bool
{
return $this->reflection->hasNativeType();
}

public function getNativeType(): Type
{
return $this->nativeType;
}

public function getReadableType(): Type
{
return $this->readableType;
Expand Down
20 changes: 0 additions & 20 deletions src/Reflection/Dummy/DummyPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,6 @@ public function isPublic(): bool
return true;
}

public function hasPhpDocType(): bool
{
return false;
}

public function getPhpDocType(): Type
{
return new MixedType();
}

public function hasNativeType(): bool
{
return false;
}

public function getNativeType(): Type
{
return new MixedType();
}

public function getReadableType(): Type
{
return new MixedType();
Expand Down
9 changes: 0 additions & 9 deletions src/Reflection/ExtendedPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PHPStan\Reflection;

use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;

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

public const HOOK_SET = 'set';

public function hasPhpDocType(): bool;

public function getPhpDocType(): Type;

public function hasNativeType(): bool;

public function getNativeType(): Type;

public function isAbstract(): TrinaryLogic;

public function isFinal(): TrinaryLogic;
Expand Down
21 changes: 0 additions & 21 deletions src/Reflection/Php/EnumPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;

final class EnumPropertyReflection implements ExtendedPropertyReflection
Expand Down Expand Up @@ -42,26 +41,6 @@ public function getDocComment(): ?string
return null;
}

public function hasPhpDocType(): bool
{
return false;
}

public function getPhpDocType(): Type
{
return new MixedType();
}

public function hasNativeType(): bool
{
return false;
}

public function getNativeType(): Type
{
return new MixedType();
}

public function getReadableType(): Type
{
return $this->type;
Expand Down
21 changes: 0 additions & 21 deletions src/Reflection/Php/SimpleXMLElementProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Type\BooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -45,26 +44,6 @@ public function isPublic(): bool
return true;
}

public function hasPhpDocType(): bool
{
return false;
}

public function getPhpDocType(): Type
{
return new MixedType();
}

public function hasNativeType(): bool
{
return false;
}

public function getNativeType(): Type
{
return new MixedType();
}

public function getReadableType(): Type
{
return $this->type;
Expand Down
21 changes: 0 additions & 21 deletions src/Reflection/Php/UniversalObjectCrateProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;

final class UniversalObjectCrateProperty implements ExtendedPropertyReflection
Expand Down Expand Up @@ -41,26 +40,6 @@ public function isPublic(): bool
return true;
}

public function hasPhpDocType(): bool
{
return false;
}

public function getPhpDocType(): Type
{
return new MixedType();
}

public function hasNativeType(): bool
{
return false;
}

public function getNativeType(): Type
{
return new MixedType();
}

public function getReadableType(): Type
{
return $this->readableType;
Expand Down
20 changes: 0 additions & 20 deletions src/Reflection/ResolvedPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,6 @@ public function isPublic(): bool
return $this->reflection->isPublic();
}

public function hasPhpDocType(): bool
{
return $this->reflection->hasPhpDocType();
}

public function getPhpDocType(): Type
{
return $this->reflection->getPhpDocType();
}

public function hasNativeType(): bool
{
return $this->reflection->hasNativeType();
}

public function getNativeType(): Type
{
return $this->reflection->getNativeType();
}

public function getReadableType(): Type
{
$type = $this->readableType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ private function transformPropertyWithStaticType(ClassReflection $declaringClass
{
$readableType = $this->transformStaticType($property->getReadableType());
$writableType = $this->transformStaticType($property->getWritableType());
$phpDocType = $this->transformStaticType($property->getPhpDocType());
$nativeType = $this->transformStaticType($property->getNativeType());

return new ChangedTypePropertyReflection($declaringClass, $property, $readableType, $writableType, $phpDocType, $nativeType);
return new ChangedTypePropertyReflection($declaringClass, $property, $readableType, $writableType);
}

private function transformStaticType(Type $type): Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ private function transformPropertyWithStaticType(ClassReflection $declaringClass
{
$readableType = $this->transformStaticType($property->getReadableType());
$writableType = $this->transformStaticType($property->getWritableType());
$phpDocType = $this->transformStaticType($property->getPhpDocType());
$nativeType = $this->transformStaticType($property->getNativeType());

return new ChangedTypePropertyReflection($declaringClass, $property, $readableType, $writableType, $phpDocType, $nativeType);
return new ChangedTypePropertyReflection($declaringClass, $property, $readableType, $writableType);
}

private function transformStaticType(Type $type): Type
Expand Down
20 changes: 0 additions & 20 deletions src/Reflection/Type/IntersectionTypePropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,6 @@ public function getDocComment(): ?string
return null;
}

public function hasPhpDocType(): bool
{
return $this->computeResult(static fn (ExtendedPropertyReflection $property) => $property->hasPhpDocType());
}

public function getPhpDocType(): Type
{
return TypeCombinator::intersect(...array_map(static fn (ExtendedPropertyReflection $property): Type => $property->getPhpDocType(), $this->properties));
}

public function hasNativeType(): bool
{
return $this->computeResult(static fn (ExtendedPropertyReflection $property) => $property->hasNativeType());
}

public function getNativeType(): Type
{
return TypeCombinator::intersect(...array_map(static fn (ExtendedPropertyReflection $property): Type => $property->getNativeType(), $this->properties));
}

public function getReadableType(): Type
{
return TypeCombinator::intersect(...array_map(static fn (ExtendedPropertyReflection $property): Type => $property->getReadableType(), $this->properties));
Expand Down
Loading

0 comments on commit bf923ad

Please sign in to comment.