Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\AddReturnDocblockDataProviderRector\Fixture;

use PHPStan\Type\ObjectType;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\VariableTypeResolver\Source\AnotherType;

final class KeepStringTypeSimple extends TestCase
{
#[DataProvider('provideData')]
public function testSomething()
{
}

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];
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\AddReturnDocblockDataProviderRector\Fixture;

use PHPStan\Type\ObjectType;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\VariableTypeResolver\Source\AnotherType;

final class KeepStringTypeSimple extends TestCase
{
#[DataProvider('provideData')]
public function testSomething()
{
}

/**
* @return \Iterator<array<int, (int | \PHPStan\Type\ObjectType | string)>>
*/
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];
}
}

?>
6 changes: 6 additions & 0 deletions rules/Privatization/TypeManipulator/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Loading