From 8f1208d18b3a3d55297336ca52817f92ca18fa86 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Fri, 27 Mar 2020 15:20:20 +0100 Subject: [PATCH] drop preg_match support from StrContains, too vague --- config/set/php/php80.yaml | 3 + .../StrposToStringsContainsRector.php | 1 + .../Rector/NotIdentical/StrContainsRector.php | 106 +----------------- .../Fixture/preg_match.php.inc | 27 ----- .../Fixture/preg_match_string.php.inc | 27 ----- 5 files changed, 6 insertions(+), 158 deletions(-) create mode 100644 config/set/php/php80.yaml delete mode 100644 rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture/preg_match.php.inc delete mode 100644 rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture/preg_match_string.php.inc diff --git a/config/set/php/php80.yaml b/config/set/php/php80.yaml new file mode 100644 index 000000000000..67ae41decb13 --- /dev/null +++ b/config/set/php/php80.yaml @@ -0,0 +1,3 @@ +services: + Rector\Php80\Rector\FunctionLike\UnionTypesRector: null + Rector\Php80\Rector\NotIdentical\StrContainsRector: null diff --git a/rules/nette/src/Rector/NotIdentical/StrposToStringsContainsRector.php b/rules/nette/src/Rector/NotIdentical/StrposToStringsContainsRector.php index 118919af36f2..536f9b23b0cf 100644 --- a/rules/nette/src/Rector/NotIdentical/StrposToStringsContainsRector.php +++ b/rules/nette/src/Rector/NotIdentical/StrposToStringsContainsRector.php @@ -17,6 +17,7 @@ /** * @see https://3v4l.org/CubLi * @see https://github.com/nette/utils/blob/bd961f49b211997202bda1d0fbc410905be370d4/src/Utils/Strings.php#L81 + * * @see \Rector\Nette\Tests\Rector\NotIdentical\StrposToStringsContainsRector\StrposToStringsContainsRectorTest */ final class StrposToStringsContainsRector extends AbstractRector diff --git a/rules/php80/src/Rector/NotIdentical/StrContainsRector.php b/rules/php80/src/Rector/NotIdentical/StrContainsRector.php index ceb51eb8b637..cebb61bf2615 100644 --- a/rules/php80/src/Rector/NotIdentical/StrContainsRector.php +++ b/rules/php80/src/Rector/NotIdentical/StrContainsRector.php @@ -4,15 +4,10 @@ namespace Rector\Php80\Rector\NotIdentical; -use Nette\Utils\Strings; use PhpParser\Node; -use PhpParser\Node\Expr; -use PhpParser\Node\Expr\BinaryOp\Concat; use PhpParser\Node\Expr\BinaryOp\NotIdentical; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\LNumber; -use PhpParser\Node\Scalar\String_; use Rector\Core\Rector\AbstractRector; use Rector\Core\RectorDefinition\CodeSample; use Rector\Core\RectorDefinition\RectorDefinition; @@ -30,14 +25,9 @@ final class StrContainsRector extends AbstractRector */ private const OLD_STR_NAMES = ['strpos', 'strstr']; - /** - * @var string - */ - private const PREG_MATCH_NAME = 'preg_match'; - public function getDefinition(): RectorDefinition { - return new RectorDefinition('Replace strpos() !== false, strstr and preg_match() with str_contains()', [ + return new RectorDefinition('Replace strpos() !== false and strstr() with str_contains()', [ new CodeSample( <<<'PHP' class SomeClass @@ -76,108 +66,16 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $funcCall = $this->matchPossibleFuncCallToFalseOrZero($node); + $funcCall = $this->matchNotIdenticalToFalse($node); if ($funcCall === null) { return null; } - if ($this->isName($funcCall, self::PREG_MATCH_NAME)) { - [$funcCall->args[0], $funcCall->args[1]] = [$funcCall->args[1], $funcCall->args[0]]; - - $clearedDelimiter = $this->clearValueFromDelimiters($funcCall->args[1]->value); - - // unable to handle - if ($clearedDelimiter === null) { - return null; - } - - $funcCall->args[1]->value = $clearedDelimiter; - } - $funcCall->name = new Name('str_contains'); return $funcCall; } - private function matchPossibleFuncCallToFalseOrZero(NotIdentical $notIdentical): ?FuncCall - { - if ($this->isNumber($notIdentical->left, 0)) { - if (! $this->isFuncCallName($notIdentical->right, self::PREG_MATCH_NAME)) { - return null; - } - - return $notIdentical->right; - } - - if ($this->isNumber($notIdentical->right, 0)) { - if (! $this->isFuncCallName($notIdentical->left, self::PREG_MATCH_NAME)) { - return null; - } - - return $notIdentical->left; - } - - return $this->matchNotIdenticalToFalse($notIdentical); - } - - private function isNumber(Expr $expr, int $value): bool - { - if (! $expr instanceof LNumber) { - return false; - } - - return $expr->value === $value; - } - - /** - * Possibly extract to regex package or something - */ - private function clearValueFromDelimiters(Expr $expr): ?Expr - { - // clears '#'. $value . '#' - if ($expr instanceof Concat) { - if (! $expr->right instanceof String_) { - return null; - } - - $rightValue = $this->getValue($expr->right); - if (! $expr->left instanceof Concat) { - return null; - } - - /** @var Concat $leftConcat */ - $leftConcat = $expr->left; - if (! $leftConcat->left instanceof String_) { - return null; - } - - $leftValue = $this->getValue($leftConcat->left); - if ($leftValue === $rightValue) { - return $leftConcat->right; - } - - return null; - } - - // clears '#content#' - if ($expr instanceof String_) { - $stringValue = $expr->value; - - $firstChar = $stringValue[0]; - $lastChar = $stringValue[strlen($stringValue) - 1]; - - if ($firstChar === $lastChar) { - $expr->value = Strings::substring($stringValue, 1, -1); - return $expr; - } - - return null; - } - - // not supported yet - return null; - } - private function matchNotIdenticalToFalse(NotIdentical $notIdentical): ?FuncCall { if ($this->isFalse($notIdentical->left)) { diff --git a/rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture/preg_match.php.inc b/rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture/preg_match.php.inc deleted file mode 100644 index cfdf9e6450fa..000000000000 --- a/rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture/preg_match.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture/preg_match_string.php.inc b/rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture/preg_match_string.php.inc deleted file mode 100644 index b0107ae25002..000000000000 --- a/rules/php80/tests/Rector/NotIdentical/StrContainsRector/Fixture/preg_match_string.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ -