diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 42034f0ae3..77a265078a 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -4498,7 +4498,7 @@ public function addTypeToExpression(Expr $expr, Type $type): self if ($originalExprType->equals($nativeType)) { $newType = TypeCombinator::intersect($type, $originalExprType); - if ($newType->isConstantScalarValue()->yes() && $newType->equals($originalExprType)) { + if ($newType->isObject()->no() && $newType->equals($originalExprType)) { // don't add the same type over and over again to improve performance return $this; } diff --git a/src/Type/MixedType.php b/src/Type/MixedType.php index 487a474827..175762d000 100644 --- a/src/Type/MixedType.php +++ b/src/Type/MixedType.php @@ -314,6 +314,10 @@ public function equals(Type $type): bool return false; } + if ($type instanceof ErrorType) { + return false; + } + if ($this->subtractedType === null) { if ($type->subtractedType === null) { return true; diff --git a/tests/PHPStan/Type/MixedTypeTest.php b/tests/PHPStan/Type/MixedTypeTest.php index 3ffc8f9db7..85edf6bc4f 100644 --- a/tests/PHPStan/Type/MixedTypeTest.php +++ b/tests/PHPStan/Type/MixedTypeTest.php @@ -1164,4 +1164,50 @@ public function testSubtractedHasOffsetValueType(MixedType $mixedType, Type $typ ); } + /** @dataProvider dataEquals */ + public function testEquals(MixedType $mixedType, Type $typeToCompare, bool $expectedResult): void + { + $this->assertSame( + $expectedResult, + $mixedType->equals($typeToCompare), + sprintf('%s -> equals(%s)', $mixedType->describe(VerbosityLevel::precise()), $typeToCompare->describe(VerbosityLevel::precise())), + ); + } + + public function dataEquals(): array + { + return [ + [ + new MixedType(), + new MixedType(), + true, + ], + [ + new MixedType(true), + new MixedType(), + true, + ], + [ + new MixedType(), + new MixedType(true), + true, + ], + [ + new MixedType(), + new MixedType(true, new IntegerType()), + false, + ], + [ + new MixedType(), + new ErrorType(), + false, + ], + [ + new MixedType(true), + new ErrorType(), + false, + ], + ]; + } + }