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/php81.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector;
use Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector;
use Rector\Php81\Rector\FuncCall\NullToStrictIntPregSlitFuncCallLimitArgRector;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
use Rector\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector;
use Rector\Php81\Rector\MethodCall\RemoveReflectionSetAccessibleCallsRector;
Expand All @@ -24,6 +25,7 @@
SpatieEnumClassToEnumRector::class,
SpatieEnumMethodCallToEnumConstRector::class,
NullToStrictStringFuncCallArgRector::class,
NullToStrictIntPregSlitFuncCallLimitArgRector::class,
FirstClassCallableRector::class,
RemoveReflectionSetAccessibleCallsRector::class,
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

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

?>
-----
<?php

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

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

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

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

class SkipNoLimit
{
public function run($output)
{
$keywords = preg_split("/[\s,]+/", $output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

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

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

final class NullToStrictIntPregSlitFuncCallLimitArgRectorTest 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,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php81\Rector\FuncCall\NullToStrictIntPregSlitFuncCallLimitArgRector;

return RectorConfig::configure()
->withRules([NullToStrictIntPregSlitFuncCallLimitArgRector::class]);
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Cast\Int_ as CastInt_;
use PhpParser\Node\Expr\Cast\String_ as CastString_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\InterpolatedString;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
Expand All @@ -24,7 +26,7 @@
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\Value\ValueResolver;

final readonly class NullToStrictStringConverter
final readonly class NullToStrictStringIntConverter
{
public function __construct(
private ValueResolver $valueResolver,
Expand All @@ -42,20 +44,21 @@ public function convertIfNull(
int $position,
bool $isTrait,
Scope $scope,
ParametersAcceptor $parametersAcceptor
ParametersAcceptor $parametersAcceptor,
string $targetType = 'string'
): ?FuncCall {
if (! isset($args[$position])) {
return null;
}

$argValue = $args[$position]->value;
if ($this->valueResolver->isNull($argValue)) {
$args[$position]->value = new String_('');
$args[$position]->value = $targetType === 'string' ? new String_('') : new Int_(0);
$funcCall->args = $args;
return $funcCall;
}

if ($this->shouldSkipValue($argValue, $scope, $isTrait)) {
if ($this->shouldSkipValue($argValue, $scope, $isTrait, $targetType)) {
return null;
}

Expand All @@ -67,32 +70,42 @@ public function convertIfNull(
}
}

if ($argValue instanceof Ternary && ! $this->shouldSkipValue($argValue->else, $scope, $isTrait)) {
if ($argValue instanceof Ternary && ! $this->shouldSkipValue($argValue->else, $scope, $isTrait, $targetType)) {
if ($this->valueResolver->isNull($argValue->else)) {
$argValue->else = new String_('');
$argValue->else = $targetType === 'string' ? new String_('') : new Int_(0);
} else {
$argValue->else = new CastString_($argValue->else);
$argValue->else = $targetType === 'string' ? new CastString_($argValue->else) : new CastInt_(
$argValue->else
);
}

$args[$position]->value = $argValue;
$funcCall->args = $args;
return $funcCall;
}

$args[$position]->value = new CastString_($argValue);
$args[$position]->value = $targetType === 'string' ? new CastString_($argValue) : new CastInt_($argValue);
$funcCall->args = $args;
return $funcCall;
}

private function shouldSkipValue(Expr $expr, Scope $scope, bool $isTrait): bool
private function shouldSkipValue(Expr $expr, Scope $scope, bool $isTrait, string $targetType): bool
{
$type = $this->nodeTypeResolver->getType($expr);
if ($type->isString()->yes()) {
if ($type->isString()->yes() && $targetType === 'string') {
return true;
}

if ($type->isInteger()->yes() && $targetType === 'int') {
return true;
}

$nativeType = $this->nodeTypeResolver->getNativeType($expr);
if ($nativeType->isString()->yes()) {
if ($nativeType->isString()->yes() && $targetType === 'string') {
return true;
}

if ($nativeType->isInteger()->yes() && $targetType === 'int') {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

declare(strict_types=1);

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;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\Php81\NodeManipulator\NullToStrictStringIntConverter;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://3v4l.org/cVPim
* @see \Rector\Tests\Php81\Rector\FuncCall\NullToStrictIntPregSlitFuncCallLimitArgRector\NullToStrictIntPregSlitFuncCallLimitArgRectorTest
*/
final class NullToStrictIntPregSlitFuncCallLimitArgRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ReflectionResolver $reflectionResolver,
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly NullToStrictStringIntConverter $nullToStrictStringIntConverter
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change null to strict int defined preg_split limit arg function call argument',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
preg_split('/\s/', $output, NULL, PREG_SPLIT_NO_EMPTY)
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
preg_split('/\s/', $output, 0, PREG_SPLIT_NO_EMPTY)
}
}
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->shouldSkip($node)) {
return null;
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}

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

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

if (! isset($args[$position])) {
return null;
}

$classReflection = $scope->getClassReflection();
$isTrait = $classReflection instanceof ClassReflection && $classReflection->isTrait();

$functionReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
if (! $functionReflection instanceof FunctionReflection) {
return null;
}

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($functionReflection, $node, $scope);
$result = $this->nullToStrictStringIntConverter->convertIfNull(
$node,
$args,
$position,
$isTrait,
$scope,
$parametersAcceptor,
'int'
);

if ($result instanceof Node) {
return $result;
}

return null;
}

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')) {
return true;
}

return $funcCall->isFirstClassCallable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\Php81\Enum\NameNullToStrictNullFunctionMap;
use Rector\Php81\NodeManipulator\NullToStrictStringConverter;
use Rector\Php81\NodeManipulator\NullToStrictStringIntConverter;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\ValueObject\PhpVersionFeature;
Expand All @@ -32,7 +32,7 @@ final class NullToStrictStringFuncCallArgRector extends AbstractRector implement
public function __construct(
private readonly ReflectionResolver $reflectionResolver,
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly NullToStrictStringConverter $nullToStrictStringConverter
private readonly NullToStrictStringIntConverter $nullToStrictStringIntConverter
) {
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public function refactor(Node $node): ?Node
$isChanged = false;

foreach ($positions as $position) {
$result = $this->nullToStrictStringConverter->convertIfNull(
$result = $this->nullToStrictStringIntConverter->convertIfNull(
$node,
$args,
(int) $position,
Expand Down
Loading
Loading