Skip to content

Commit

Permalink
[Php80][TypeDeclaration] Fix crash on ClassPropertyAssignToConstructo…
Browse files Browse the repository at this point in the history
…rPromotionRector + ReturnNeverTypeRector (#6694)

* [Php80][TypeDeclaration] Fix crash on ClassPropertyAssignToConstructorPromotionRector + ReturnNeverTypeRector

* fix

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Jan 24, 2025
1 parent fcd141b commit a1170ac
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType;
use PhpParser\NodeVisitor;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -239,7 +241,11 @@ public function refactor(Node $node): ?Node
$this->traverseNodesWithCallable((array) $constructClassMethod->stmts, function (Node $node) use (
$promotionCandidate,
$propertyName
): ?PropertyFetch {
): null|int|PropertyFetch {
if ($node instanceof Class_ || $node instanceof FunctionLike) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

if (! $node instanceof Variable) {
return null;
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Issues/PropertyPromoNeverReturn/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\Tests\Issues\PropertyPromoNeverReturn\Fixture;

class Fixture
{
private $value;
private array $rules;

public function __construct($value)
{
$this->value = $value;

$this->rules = [
'chave' => function ($value) {
return is_string($value) ? true : 'deve ser uma string';
},
];
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\PropertyPromoNeverReturn\Fixture;

class Fixture
{
private array $rules;

public function __construct(private $value)
{
$this->rules = [
'chave' => function ($value) {
return is_string($value) ? true : 'deve ser uma string';
},
];
}
}

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

declare(strict_types=1);

namespace Rector\Tests\Issues\PropertyPromoNeverReturn;

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

final class PropertyPromoNeverReturnTest 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';
}
}
14 changes: 14 additions & 0 deletions tests/Issues/PropertyPromoNeverReturn/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;

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

0 comments on commit a1170ac

Please sign in to comment.