Skip to content

Commit 590e1ba

Browse files
[Downgrade][Php81][Php80] Handle nullable downgrade initialization on use DowngradeNewInInitializerRector+DowngradePropertyPromotionRector (#243)
* [Downgrade][Php81][Php80] Handle nullable downgrade initialization on use DowngradeNewInInitializerRector+DowngradePropertyPromotionRector * [ci-review] Rector Rectify * fix * fix --------- Co-authored-by: GitHub Action <[email protected]>
1 parent edf3b13 commit 590e1ba

File tree

6 files changed

+76
-6
lines changed

6 files changed

+76
-6
lines changed

rules-tests/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector/Fixture/new_not_promoted.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ final class NewNotPromoted
2222
{
2323
public stdClass $obj;
2424

25-
public function __construct(stdClass $obj = null)
25+
public function __construct(?stdClass $obj = null)
2626
{
27-
$obj ??= new stdClass;
27+
$this->obj = $obj ?? new stdClass;
2828
$this->obj = $obj;
2929
}
3030
}

rules/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ private function replaceNewInParams(ClassMethod|Closure|Function_ $functionLike)
162162
/** @var Variable $paramVar */
163163
$paramVar = $param->var;
164164

165-
// check for property promotion
166-
if ($isConstructor && $param->flags > 0) {
165+
if ($isConstructor) {
167166
$propertyFetch = new PropertyFetch(new Variable('this'), $paramVar->name);
168167
$coalesce = new Coalesce($param->var, $default);
169168
$assign = new Assign($propertyFetch, $coalesce);

tests/Issues/DowngradeNewInArrayInitializerPromotion/Fixture/new_in_array.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class NewInArray
3333
/**
3434
* @param array<string, object> $property
3535
*/
36-
public function __construct(array $property = null)
36+
public function __construct(?array $property = null)
3737
{
38-
$property ??= [
38+
$this->property = $property ?? [
3939
'a' => new stdClass()
4040
];
4141
$this->property = $property;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\DowngradeNullableInitialization;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeNullableInitializationTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\DowngradeNullableInitialization\Fixture;
4+
5+
final class AddNullableInitializtion
6+
{
7+
public function __construct(
8+
private array $doctrineTypeToScalarType = [
9+
'tinyint' => new \PHPStan\Type\BooleanType(),
10+
]
11+
) {}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\Issues\DowngradeNullableInitialization\Fixture;
19+
20+
final class AddNullableInitializtion
21+
{
22+
private array $doctrineTypeToScalarType;
23+
public function __construct(?array $doctrineTypeToScalarType = null)
24+
{
25+
$this->doctrineTypeToScalarType = $doctrineTypeToScalarType ?? [
26+
'tinyint' => new \PHPStan\Type\BooleanType(),
27+
];
28+
$this->doctrineTypeToScalarType = $doctrineTypeToScalarType;
29+
}
30+
}
31+
32+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
7+
use Rector\DowngradePhp81\Rector\FunctionLike\DowngradeNewInInitializerRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rules([DowngradeNewInInitializerRector::class, DowngradePropertyPromotionRector::class]);
11+
};

0 commit comments

Comments
 (0)