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,27 @@
<?php

namespace Rector\Tests\Php81\Rector\FuncCall\NullToStrictIntPregSlitFuncCallLimitArgRector\Fixture;

class DifferentNamedArg
{
public function run($output)
{
preg_split('/\s/', $output, NULL, flags: PREG_SPLIT_NO_EMPTY);
}
}

?>
-----
<?php

namespace Rector\Tests\Php81\Rector\FuncCall\NullToStrictIntPregSlitFuncCallLimitArgRector\Fixture;

class DifferentNamedArg
{
public function run($output)
{
preg_split('/\s/', $output, 0, flags: PREG_SPLIT_NO_EMPTY);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\DeadCode\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace Rector\Php81\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionReflection;
Expand Down Expand Up @@ -88,13 +86,7 @@ public function refactor(Node $node): ?Node
}

$args = $node->getArgs();
$position = $this->argsAnalyzer->hasNamedArg($args)
? $this->resolveNamedPosition($args)
: 2;

if ($position === null) {
return null;
}
$position = $this->argsAnalyzer->resolveArgPosition($args, 'limit', 2);

if (! isset($args[$position])) {
return null;
Expand Down Expand Up @@ -131,26 +123,6 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::DEPRECATE_NULL_ARG_IN_STRING_FUNCTION;
}

/**
* @param Arg[] $args
*/
private function resolveNamedPosition(array $args): ?int
{
foreach ($args as $position => $arg) {
if (! $arg->name instanceof Identifier) {
continue;
}

if (! $this->isName($arg->name, 'limit')) {
continue;
}

return $position;
}

return null;
}

private function shouldSkip(FuncCall $funcCall): bool
{
if (! $this->isName($funcCall, 'preg_split')) {
Expand Down
15 changes: 9 additions & 6 deletions rules/Php85/Rector/Class_/SleepToSerializeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Rector\Php85\Rector\Class_;

use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
Expand All @@ -28,8 +30,8 @@
final class SleepToSerializeRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private ReturnAnalyzer $returnAnalyzer
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ReturnAnalyzer $returnAnalyzer
) {
}

Expand Down Expand Up @@ -110,18 +112,19 @@ public function refactor(Node $node): ?Node
return null;
}

if (count($return->expr->items) > 0) {
if ($return->expr->items !== []) {
$newItems = [];
foreach ($return->expr->items as $item) {
if ($item !== null && $item->value instanceof Node\Scalar\String_) {
if ($item !== null && $item->value instanceof String_) {
$propName = $item->value->value;
$newItems[] = new ArrayItem(
new PropertyFetch(new Node\Expr\Variable('this'), $propName),
new PropertyFetch(new Variable('this'), $propName),
$item->value
);
}
}
if (count($newItems) > 0) {

if ($newItems !== []) {
$hasChanged = true;
$return->expr->items = $newItems;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
namespace Rector\Php85\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionReflection;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\Php81\NodeManipulator\NullToStrictStringIntConverter;
Expand All @@ -29,7 +28,8 @@ final class ArrayKeyExistsNullToEmptyStringRector extends AbstractRector impleme
{
public function __construct(
private readonly ReflectionResolver $reflectionResolver,
private readonly NullToStrictStringIntConverter $nullToStrictStringIntConverter
private readonly NullToStrictStringIntConverter $nullToStrictStringIntConverter,
private readonly ArgsAnalyzer $argsAnalyzer
) {
}

Expand Down Expand Up @@ -93,7 +93,7 @@ public function refactor(Node $node): ?Node
$result = $this->nullToStrictStringIntConverter->convertIfNull(
$node,
$args,
$this->resolvePosition($args),
$this->argsAnalyzer->resolveArgPosition($args, 'key', 0),
$isTrait,
$scope,
$parametersAcceptor
Expand All @@ -109,18 +109,4 @@ public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATE_NULL_ARG_IN_ARRAY_KEY_EXISTS_FUNCTION;
}

/**
* @param Arg[] $args
*/
private function resolvePosition(array $args): int
{
foreach ($args as $position => $arg) {
if ($arg->name instanceof Identifier && $arg->name->toString() === 'key') {
return $position;
}
}

return 0;
}
}
28 changes: 27 additions & 1 deletion src/NodeAnalyzer/ArgsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@

use PhpParser\Node\Arg;
use PhpParser\Node\Identifier;
use Rector\NodeNameResolver\NodeNameResolver;

final class ArgsAnalyzer
final readonly class ArgsAnalyzer
{
public function __construct(
private NodeNameResolver $nodeNameResolver
) {
}

/**
* @param Arg[] $args
*/
Expand All @@ -22,4 +28,24 @@ public function hasNamedArg(array $args): bool

return false;
}

/**
* @param Arg[] $args
*/
public function resolveArgPosition(array $args, string $name, int $defaultPosition): int
{
foreach ($args as $position => $arg) {
if (! $arg->name instanceof Identifier) {
continue;
}

if (! $this->nodeNameResolver->isName($arg->name, $name)) {
continue;
}

return $position;
}

return $defaultPosition;
}
}
Loading