From dcc3d6af26081f8f6947bd8015c525be74488123 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 29 Sep 2025 18:58:40 +0200 Subject: [PATCH] generalize string type --- .../Fixture/keep_string_type_simple.php.inc | 54 +++++++++++++++++++ .../TypeManipulator/TypeNormalizer.php | 6 +++ 2 files changed, 60 insertions(+) create mode 100644 rules-tests/TypeDeclarationDocblocks/Rector/Class_/AddReturnDocblockDataProviderRector/Fixture/keep_string_type_simple.php.inc diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/Class_/AddReturnDocblockDataProviderRector/Fixture/keep_string_type_simple.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/Class_/AddReturnDocblockDataProviderRector/Fixture/keep_string_type_simple.php.inc new file mode 100644 index 00000000000..934ba63e8ec --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/Class_/AddReturnDocblockDataProviderRector/Fixture/keep_string_type_simple.php.inc @@ -0,0 +1,54 @@ + +----- +> + */ + public static function provideData(): \Iterator + { + $anotherTypeObjectType = new ObjectType(AnotherType::class); + yield [__DIR__ . '/Fixture/new_class.php.inc', 1, $anotherTypeObjectType]; + yield [__DIR__ . '/Fixture/argument_typehint.php.inc', 1, $anotherTypeObjectType]; + } +} + +?> diff --git a/rules/Privatization/TypeManipulator/TypeNormalizer.php b/rules/Privatization/TypeManipulator/TypeNormalizer.php index af3bbe8e7e6..e29c0c16df5 100644 --- a/rules/Privatization/TypeManipulator/TypeNormalizer.php +++ b/rules/Privatization/TypeManipulator/TypeNormalizer.php @@ -4,6 +4,8 @@ namespace Rector\Privatization\TypeManipulator; +use PHPStan\Type\Accessory\AccessoryLiteralStringType; +use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; use PHPStan\Type\ArrayType; use PHPStan\Type\BooleanType; use PHPStan\Type\Constant\ConstantArrayType; @@ -55,6 +57,10 @@ public function generalizeConstantBoolTypes(Type $type): Type public function generalizeConstantTypes(Type $type): Type { return TypeTraverser::map($type, function (Type $type, callable $traverseCallback): Type { + if ($type instanceof AccessoryNonFalsyStringType || $type instanceof AccessoryLiteralStringType) { + return new StringType(); + } + if ($type instanceof ConstantBooleanType) { return new BooleanType(); }