Thanks to visit codestin.com
Credit goes to github.com

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: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ parameters:

- '#Register "Rector\\Php80\\Rector\\NotIdentical\\MbStrContainsRector" service to "php80\.php" config set#'

- '#Register "Rector\\Php85\\Rector\\StmtsAwareInterface\\NestedToPipeOperatorRector" service to "php85\.php" config set#'
- '#Register "Rector\\Php85\\Rector\\StmtsAwareInterface\\SequentialAssignmentsToPipeOperatorRector" service to "php85\.php" config set#'

# closure detailed
- '#Method Rector\\Config\\RectorConfig\:\:singleton\(\) has parameter \$concrete with no signature specified for Closure#'
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\NestedToPipeOperatorRector\Fixture;
namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\SequentialAssignmentsToPipeOperatorRector\Fixture;

$value = "hello world";
$result1 = function3($value);
Expand All @@ -11,7 +11,7 @@ $result = function1($result2);
-----
<?php

namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\NestedToPipeOperatorRector\Fixture;
namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\SequentialAssignmentsToPipeOperatorRector\Fixture;

$value = "hello world";
$result = $value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\NestedToPipeOperatorRector\Fixture;
namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\SequentialAssignmentsToPipeOperatorRector\Fixture;

class InsideClassMethod
{
Expand All @@ -17,7 +17,7 @@ class InsideClassMethod
-----
<?php

namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\NestedToPipeOperatorRector\Fixture;
namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\SequentialAssignmentsToPipeOperatorRector\Fixture;

class InsideClassMethod
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\NestedToPipeOperatorRector\Fixture;
namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\SequentialAssignmentsToPipeOperatorRector\Fixture;

$value = "hello world";
$result1 = function3($value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\SequentialAssignmentsToPipeOperatorRector\Fixture;

final class SkipNestedFunctions
{
public function run()
{
$result = trim(strtoupper("Hello World"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\SequentialAssignmentsToPipeOperatorRector\Fixture;

$value = "hello world";
$result1 = function3($value);
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\NestedToPipeOperatorRector;
namespace Rector\Tests\Php85\Rector\StmtsAwareInterface\SequentialAssignmentsToPipeOperatorRector;

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

final class NestedToPipeOperatorRectorTest extends AbstractRectorTestCase
final class SequentialAssignmentsToPipeOperatorRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
Expand Down
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\Php85\Rector\StmtsAwareInterface\SequentialAssignmentsToPipeOperatorRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(SequentialAssignmentsToPipeOperatorRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

/**
* @see https://wiki.php.net/rfc/pipe-operator-v3
* @see \Rector\Tests\Php85\Rector\StmtsAwareInterface\NestedToPipeOperatorRector\NestedToPipeOperatorRectorTest
* @see \Rector\Tests\Php85\Rector\StmtsAwareInterface\SequentialAssignmentsToPipeOperatorRector\SequentialAssignmentsToPipeOperatorRectorTest
*/
final class NestedToPipeOperatorRector extends AbstractRector implements MinPhpVersionInterface
final class SequentialAssignmentsToPipeOperatorRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ExprAnalyzer $exprAnalyzer
Expand All @@ -36,23 +36,24 @@ public function __construct(
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Transform nested function calls and sequential assignments to pipe operator syntax',
'Transform sequential assignments to pipe operator syntax',
[
new CodeSample(
<<<'CODE_SAMPLE'
$value = "hello world";
$result1 = function3($value);
$result1 = function1($value);
$result2 = function2($result1);
$result = function1($result2);
$result = function3($result2);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$value = "hello world";
$result = $value
|> function3(...)
|> function1(...)
|> function2(...)
|> function1(...);
|> function3(...);
CODE_SAMPLE
),
]
Expand Down Expand Up @@ -80,40 +81,25 @@ public function refactor(Node $node): ?Node

$hasChanged = false;

// First, try to transform sequential assignments
$sequentialChanged = $this->transformSequentialAssignments($node);
if ($sequentialChanged) {
$hasChanged = true;
}

// Then, transform nested function calls
$nestedChanged = $this->transformNestedCalls($node);
if ($nestedChanged) {
$hasChanged = true;
}

return $hasChanged ? $node : null;
}

private function transformSequentialAssignments(StmtsAwareInterface $stmtsAware): bool
{
$hasChanged = false;

$statements = $stmtsAware->stmts;
$statements = $node->stmts;
$totalStatements = count($statements) - 1;

for ($i = 0; $i < $totalStatements; ++$i) {
$chain = $this->findAssignmentChain($statements, $i);

if ($chain && count($chain) >= 2) {
$this->processAssignmentChain($stmtsAware, $chain, $i);
$this->processAssignmentChain($node, $chain, $i);
$hasChanged = true;
// Skip processed statements
$i += count($chain) - 1;
}
}

return $hasChanged;
if (! $hasChanged) {
return null;
}

return $node;
}

/**
Expand Down Expand Up @@ -243,63 +229,6 @@ private function processAssignmentChain(StmtsAwareInterface $stmtsAware, array $
$stmtsAware->stmts = $stmts;
}

private function transformNestedCalls(StmtsAwareInterface $stmtsAware): bool
{
$hasChanged = false;

foreach ($stmtsAware->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
}

$expr = $stmt->expr;

if ($expr instanceof Assign) {
$assignedValue = $expr->expr;
$processedValue = $this->processNestedCalls($assignedValue);

if ($processedValue instanceof Expr && $processedValue !== $assignedValue) {
$expr->expr = $processedValue;
$hasChanged = true;
}
} elseif ($expr instanceof FuncCall) {
$processedValue = $this->processNestedCalls($expr);

if ($processedValue instanceof Expr && $processedValue !== $expr) {
$stmt->expr = $processedValue;
$hasChanged = true;
}
}
}

return $hasChanged;
}

private function processNestedCalls(Node $node): ?Expr
{
if (! $node instanceof FuncCall) {
return null;
}

// Check if any argument is a function call
foreach ($node->args as $arg) {
if (! $arg instanceof Arg) {
return null;
}

if ($arg->value instanceof FuncCall) {
return $this->buildPipeExpression($node, $arg->value);
}
}

return null;
}

private function buildPipeExpression(FuncCall $outerCall, FuncCall $innerCall): Pipe
{
return new Pipe($innerCall, $this->createPlaceholderCall($outerCall));
}

private function createPlaceholderCall(FuncCall $funcCall): FuncCall
{
return new FuncCall($funcCall->name, [new VariadicPlaceholder()]);
Expand Down