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
3 changes: 2 additions & 1 deletion .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ jobs:
-
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
# use php 8.5 to allow test (void) cast syntax
php-version: 8.5
coverage: none

- uses: "ramsey/composer-install@v2"
Expand Down
2 changes: 2 additions & 0 deletions config/set/downgrade-php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use Rector\DowngradePhp85\Rector\Class_\DowngradeFinalPropertyPromotionRector;
use Rector\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector;
use Rector\DowngradePhp85\Rector\FuncCall\DowngradeArrayFirstLastRector;
use Rector\DowngradePhp85\Rector\StmtsAwareInterface\DowngradePipeOperatorRector;
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector;
Expand All @@ -18,6 +19,7 @@
DowngradeArrayFirstLastRector::class,
DowngradeFinalPropertyPromotionRector::class,
DowngradePipeOperatorRector::class,
DowngradeVoidCastRector::class,
]);

// https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_driver_specific_pdo_constants_and_methods
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector;

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

final class DowngradeVoidCastRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
#[RequiresPhp('8.5')]
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,41 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector\Fixture;

final class Fixture
{
#[\NoDiscard]
function foo(): string {
return 'bar';
}

public function run()
{
(void) $this->foo();
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector\Fixture;

final class Fixture
{
#[\NoDiscard]
function foo(): string {
return 'bar';
}

public function run()
{
$_void = $this->foo();
}
}

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

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector\Fixture;

final class SkipDifferentCast
{
public function run()
{
(int) call();
}
}

?>
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\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeVoidCastRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ private function cleanTrailingComma(Closure|ClassMethod|Function_ $node, array $
{
$lastPosition = array_key_last($array);

if ($lastPosition === null) {
return null;
}

$last = $array[$lastPosition];
if (! $this->followedByCommaAnalyzer->isFollowed($this->file, $last)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function getOldComments(ClassMethod $constructorClassMethod): array
{
$oldComments = [];
foreach ($constructorClassMethod->params as $param) {
$oldComments[$this->getName($param->var)] = $param->getAttribute(AttributeKey::COMMENTS);
$oldComments[(string) $this->getName($param->var)] = $param->getAttribute(AttributeKey::COMMENTS);
}

return $oldComments;
Expand Down
84 changes: 84 additions & 0 deletions rules/DowngradePhp85/Rector/Expression/DowngradeVoidCastRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp85\Rector\Expression;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Cast\Void_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use Rector\Naming\Naming\VariableNaming;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://wiki.php.net/rfc/marking_return_value_as_important
* @see \Rector\Tests\DowngradePhp85\Rector\Expression\DowngradeVoidCastRector\DowngradeVoidCastRectorTest
*/
final class DowngradeVoidCastRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming
) {
}

public function getNodeTypes(): array
{
return [Expression::class];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace void casts with proper handling of return values',
[
new CodeSample(
<<<'CODE_SAMPLE'
#[\NoDiscard]
function getPhpVersion(): string
{
return 'PHP 8.5';
}

(void) getPhpVersion();
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
#[\NoDiscard]
function getPhpVersion(): string
{
return 'PHP 8.5';
}

$_void = getPhpVersion();
CODE_SAMPLE
),
]
);
}

/**
* @param Expression $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof Void_) {
return null;
}

$scope = ScopeFetcher::fetch($node);
$variable = new Variable($this->variableNaming->createCountedValueName('_void', $scope));

// the assign is needed to avoid warning
// see https://3v4l.org/ie68D#v8.5.3 vs https://3v4l.org/nLc5J#v8.5.3
$node->expr = new Assign(
$variable,
$node->expr->expr
);
return $node;
}
}
Loading