Skip to content

Commit

Permalink
[CodeQuality] Skip dynamic second arg or has uppercase second arg on …
Browse files Browse the repository at this point in the history
…SimplifyStrposLowerRector (#5855)

* [CodeQuality] Skip dynamic second arg or has uppercase second arg on SimplifyStrposLowerRector

* [ci-review] Rector Rectify

* fix phpstan

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed May 2, 2024
1 parent 6525345 commit d8f262a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector\Fixture;

final class SkipDynamicSecondArg
{
public function run(string $findMe)
{
$string = 'hey';
strpos(strtolower($string), $findMe);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector\Fixture;

final class SkipHasUppercaseSecondArg
{
public function run()
{
$string = 'hey';
strpos(strtolower($string), 'findME1');
}
}
22 changes: 20 additions & 2 deletions rules/CodeQuality/Rector/FuncCall/SimplifyStrposLowerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Rector\CodeQuality\Rector\FuncCall;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -16,6 +18,12 @@
*/
final class SimplifyStrposLowerRector extends AbstractRector
{
/**
* @var string
* @see https://regex101.com/r/Jokjt8/1
*/
private const UPPERCASE_REGEX = '#[A-Z]#';

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -45,11 +53,12 @@ public function refactor(Node $node): ?Node
return null;
}

if (! isset($node->getArgs()[0])) {
$args = $node->getArgs();
if (! isset($args[0], $args[1])) {
return null;
}

$firstArg = $node->getArgs()[0];
$firstArg = $args[0];
if (! $firstArg->value instanceof FuncCall) {
return null;
}
Expand All @@ -60,6 +69,15 @@ public function refactor(Node $node): ?Node
return null;
}

$secondArg = $args[1];
if (! $secondArg->value instanceof String_) {
return null;
}

if (Strings::match($secondArg->value->value, self::UPPERCASE_REGEX) !== null) {
return null;
}

// pop 1 level up
$node->args[0] = $innerFuncCall->getArgs()[0];
$node->name = new Name('stripos');
Expand Down

0 comments on commit d8f262a

Please sign in to comment.