Skip to content

Commit

Permalink
[Downgrade][Php81][Php80] Handle nullable downgrade initialization on…
Browse files Browse the repository at this point in the history
… 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 <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Nov 22, 2024
1 parent edf3b13 commit 590e1ba
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ final class NewNotPromoted
{
public stdClass $obj;

public function __construct(stdClass $obj = null)
public function __construct(?stdClass $obj = null)
{
$obj ??= new stdClass;
$this->obj = $obj ?? new stdClass;
$this->obj = $obj;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ private function replaceNewInParams(ClassMethod|Closure|Function_ $functionLike)
/** @var Variable $paramVar */
$paramVar = $param->var;

// check for property promotion
if ($isConstructor && $param->flags > 0) {
if ($isConstructor) {
$propertyFetch = new PropertyFetch(new Variable('this'), $paramVar->name);
$coalesce = new Coalesce($param->var, $default);
$assign = new Assign($propertyFetch, $coalesce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class NewInArray
/**
* @param array<string, object> $property
*/
public function __construct(array $property = null)
public function __construct(?array $property = null)
{
$property ??= [
$this->property = $property ?? [
'a' => new stdClass()
];
$this->property = $property;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\DowngradeNullableInitialization;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DowngradeNullableInitializationTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\Issues\DowngradeNullableInitialization\Fixture;

final class AddNullableInitializtion
{
public function __construct(
private array $doctrineTypeToScalarType = [
'tinyint' => new \PHPStan\Type\BooleanType(),
]
) {}
}

?>
-----
<?php

namespace Rector\Tests\Issues\DowngradeNullableInitialization\Fixture;

final class AddNullableInitializtion
{
private array $doctrineTypeToScalarType;
public function __construct(?array $doctrineTypeToScalarType = null)
{
$this->doctrineTypeToScalarType = $doctrineTypeToScalarType ?? [
'tinyint' => new \PHPStan\Type\BooleanType(),
];
$this->doctrineTypeToScalarType = $doctrineTypeToScalarType;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
use Rector\DowngradePhp81\Rector\FunctionLike\DowngradeNewInInitializerRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([DowngradeNewInInitializerRector::class, DowngradePropertyPromotionRector::class]);
};

0 comments on commit 590e1ba

Please sign in to comment.