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
2 changes: 2 additions & 0 deletions config/set/downgrade-php84.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector;
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayFindKeyRector;
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector;
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeExitNamedArgumentRector;
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
use Rector\ValueObject\PhpVersion;
Expand All @@ -15,6 +16,7 @@
$rectorConfig->phpVersion(PhpVersion::PHP_83);
$rectorConfig->rules([
DowngradeNewMethodCallWithoutParenthesesRector::class,
DowngradeExitNamedArgumentRector::class,
DowngradeRoundingModeEnumRector::class,
DowngradeArrayAllRector::class,
DowngradeArrayAnyRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\FuncCall\DowngradeExitNamedArgumentRector;

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

final class DowngradeExitNamedArgumentRectorTest 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,33 @@
<?php

namespace Rector\Tests\DowngradePhp84\Rector\FuncCall\DowngradeExitNamedArgumentRector\Fixture;

class Fixture
{
public function run()
{
exit(status: 1);
die(status: 'failure');
\exit(status: getStatus());
\die(status: getStatus());
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp84\Rector\FuncCall\DowngradeExitNamedArgumentRector\Fixture;

class Fixture
{
public function run()
{
exit(1);
die('failure');
\exit(getStatus());
\die(getStatus());
}
}

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

namespace Rector\Tests\DowngradePhp84\Rector\FuncCall\DowngradeExitNamedArgumentRector\Fixture;

final class SkipWithoutNamedArgument
{
public function run()
{
exit(1);
die('failure');
custom(status: 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeExitNamedArgumentRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeExitNamedArgumentRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp84\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Identifier;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/exit-as-function
*
* @see \Rector\Tests\DowngradePhp84\Rector\FuncCall\DowngradeExitNamedArgumentRector\DowngradeExitNamedArgumentRectorTest
*/
final class DowngradeExitNamedArgumentRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove named argument from exit() and die()',
[
new CodeSample(
<<<'CODE_SAMPLE'
exit(status: 1);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
exit(1);
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isNames($node, ['exit', 'die'])) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$args = $node->getArgs();
if (count($args) !== 1) {
return null;
}

$statusArg = $args[0];
if (! $statusArg instanceof Arg) {
return null;
}

if (! $statusArg->name instanceof Identifier) {
return null;
}

$statusArg->name = null;
return $node;
}
}
Loading