Skip to content

Commit

Permalink
[Php81] Skip with next required on NewInInitializerRector
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 17, 2024
1 parent 0efbe13 commit a715a65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture;

final class SkipWithNextRequired
{
private \DateTime $time;

public function __construct(?\DateTime $time, int $required)
{
$this->time = $time ?? new \DateTime();
}
}
11 changes: 10 additions & 1 deletion rules/Php81/Rector/ClassMethod/NewInInitializerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Php81\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
Expand Down Expand Up @@ -203,9 +204,17 @@ private function matchConstructorParams(ClassMethod $classMethod): array
return [];
}

return array_filter(
$params = array_filter(
$classMethod->params,
static fn (Param $param): bool => $param->type instanceof NullableType
);

foreach ($params as $key => $param) {
if (isset($classMethod->params[$key + 1]) && ! $classMethod->params[$key + 1]->default instanceof Expr) {
return [];
}
}

return $params;
}
}

0 comments on commit a715a65

Please sign in to comment.