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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

55 changes: 8 additions & 47 deletions rules/CodeQuality/Rector/FuncCall/SimplifyStrposLowerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@

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\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector\SimplifyStrposLowerRectorTest
* @deprecated This rule is deprecated, as it handles a niche strpos(strtolower()) case that is not part of the PHP upgrade path.
*/
final class SimplifyStrposLowerRector extends AbstractRector
final class SimplifyStrposLowerRector extends AbstractRector implements DeprecatedInterface
{
/**
* @see https://regex101.com/r/Jokjt8/1
*/
private const string UPPERCASE_REGEX = '#[A-Z]#';

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand All @@ -44,42 +38,9 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'strpos')) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

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

$firstArg = $args[0];
if (! $firstArg->value instanceof FuncCall) {
return null;
}

$innerFuncCall = $firstArg->value;
if (! $this->isName($innerFuncCall, 'strtolower')) {
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');

return $node;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as it handles a niche strpos(strtolower()) case that is not part of the PHP upgrade path',
self::class
));
}
}
2 changes: 0 additions & 2 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
use Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector;
use Rector\CodeQuality\Rector\FuncCall\SimplifyFuncGetArgsCountRector;
use Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector;
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector;
use Rector\CodeQuality\Rector\FuncCall\SortCallLikeNamedArgsRector;
use Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector;
Expand Down Expand Up @@ -119,7 +118,6 @@ final class CodeQualityLevel
SimplifyForeachToCoalescingRector::class,
SimplifyFuncGetArgsCountRector::class,
SimplifyInArrayValuesRector::class,
SimplifyStrposLowerRector::class,
SimplifyArraySearchRector::class,
SimplifyConditionsRector::class,
SimplifyIfNotNullReturnRector::class,
Expand Down
Loading