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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\Php85\Rector\Expression\NestedFuncCallsToPipeOperatorRector\Fixture;

final class TwoNestedFunctions
{
public function run(array $input)
{
assert(is_string($input['email']));
}
}

?>
-----
<?php

namespace Rector\Tests\Php85\Rector\Expression\NestedFuncCallsToPipeOperatorRector\Fixture;

final class TwoNestedFunctions
{
public function run(array $input)
{
$input['email']
|> is_string(...)
|> assert(...);
}
}

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

namespace Rector\Tests\Php85\Rector\Expression\NestedFuncCallsToPipeOperatorRector\FixtureMinimumDepth;

final class MinimumDepth
{
public function run(array $input)
{
$normalizedEmail = trim(strtolower(htmlspecialchars($input['email'])));
}
}

?>
-----
<?php

namespace Rector\Tests\Php85\Rector\Expression\NestedFuncCallsToPipeOperatorRector\FixtureMinimumDepth;

final class MinimumDepth
{
public function run(array $input)
{
$normalizedEmail = $input['email']
|> htmlspecialchars(...)
|> strtolower(...)
|> trim(...);
}
}

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

namespace Rector\Tests\Php85\Rector\Expression\NestedFuncCallsToPipeOperatorRector\FixtureMinimumDepth;

final class SkipBelowMinimumDepth
{
public function run(array $input)
{
assert(is_string($input['email']));
$normalizedName = trim(strtolower($input['name']));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Expression\NestedFuncCallsToPipeOperatorRector;

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

final class NestedFuncCallsToPipeOperatorRectorMinimumDepthTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureMinimumDepth');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule_minimum_depth.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php85\Rector\Expression\NestedFuncCallsToPipeOperatorRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(NestedFuncCallsToPipeOperatorRector::class, [
NestedFuncCallsToPipeOperatorRector::MINIMUM_DEPTH => 3,
]);

$rectorConfig->phpVersion(PhpVersion::PHP_85);
};
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ private function shouldUsePropertyTypeForPromotedParam(Property $property, Param
$paramType = TypeCombinator::union($paramType, $this->getType($param->default));
}

if (!$this->typeComparator->isSubtype($paramType, $propertyType)) {
if (! $this->typeComparator->isSubtype($paramType, $propertyType)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,43 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\VariadicPlaceholder;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Php85\Rector\Expression\NestedFuncCallsToPipeOperatorRector\NestedFuncCallsToPipeOperatorRectorTest
*/
final class NestedFuncCallsToPipeOperatorRector extends AbstractRector implements MinPhpVersionInterface
final class NestedFuncCallsToPipeOperatorRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface
{
public const string MINIMUM_DEPTH = 'minimum_depth';

private const int DEFAULT_MINIMUM_DEPTH = 2;

private int $minimumDepth = self::DEFAULT_MINIMUM_DEPTH;

/**
* @param array<string, mixed> $configuration
*/
public function configure(array $configuration): void
{
$minimumDepth = $configuration[self::MINIMUM_DEPTH] ?? self::DEFAULT_MINIMUM_DEPTH;
Assert::integer($minimumDepth);
Assert::greaterThanEq($minimumDepth, 2);

$this->minimumDepth = $minimumDepth;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Convert multiple nested function calls in single line to |> pipe operator',
[
new CodeSample(
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
Expand All @@ -52,6 +72,10 @@ public function run($input)
}
}
CODE_SAMPLE
,
[
self::MINIMUM_DEPTH => 3,
]
)]
);
}
Expand Down Expand Up @@ -129,6 +153,10 @@ private function processNestedCalls(Expr $expr, bool $deep = false): ?Expr
return null;
}

if (! $deep && $this->countNestedFuncCalls($expr) < $this->minimumDepth) {
return null;
}

if (count($expr->args) !== 1) {
return null;
}
Expand Down Expand Up @@ -157,4 +185,21 @@ private function processNestedCalls(Expr $expr, bool $deep = false): ?Expr

return null;
}

private function countNestedFuncCalls(FuncCall $funcCall): int
{
$depth = 1;

while (count($funcCall->args) === 1) {
$arg = $funcCall->args[0];
if (! $arg instanceof Arg || $arg->unpack || ! $arg->value instanceof FuncCall) {
break;
}

++$depth;
$funcCall = $arg->value;
}

return $depth;
}
}
Loading