diff --git a/src/Type/MixedType.php b/src/Type/MixedType.php index 10c2b7cccd..75f8972bc1 100644 --- a/src/Type/MixedType.php +++ b/src/Type/MixedType.php @@ -37,6 +37,7 @@ use PHPStan\Type\Traits\NonGeneralizableTypeTrait; use PHPStan\Type\Traits\NonGenericTypeTrait; use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; +use function get_class; use function sprintf; /** @api */ @@ -310,7 +311,7 @@ public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope) public function equals(Type $type): bool { - if (!$type instanceof self) { + if (get_class($type) !== static::class) { return false; } diff --git a/tests/PHPStan/Generics/TemplateTypeFactoryTest.php b/tests/PHPStan/Generics/TemplateTypeFactoryTest.php index 58af77cd3b..d044b4b469 100644 --- a/tests/PHPStan/Generics/TemplateTypeFactoryTest.php +++ b/tests/PHPStan/Generics/TemplateTypeFactoryTest.php @@ -55,7 +55,12 @@ public function dataCreate(): array null, TemplateTypeVariance::createInvariant(), ), - new MixedType(), + TemplateTypeFactory::create( + TemplateTypeScope::createWithFunction('a'), + 'U', + null, + TemplateTypeVariance::createInvariant(), + ), ], [ new UnionType([ 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, + ], + ]; + } + }