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
13 changes: 3 additions & 10 deletions docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,8 @@ Changes settype() to (type) where possible
Add preg_quote delimiter when missing

```diff
class SomeClass
{
public function test()
{
- return preg_quote('name');
+ return preg_quote('name', '#');
}
}
-'#' . preg_quote('name') . '#';
+'#' . preg_quote('name', '#') . '#';
```

<br>
Expand Down Expand Up @@ -5923,8 +5917,7 @@ services:
- return SomeStaticClass::go();
+ return $this->someStaticClass->go();
}
-}
+}
}
```

<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -17,28 +19,21 @@
*/
final class AddPregQuoteDelimiterRector extends AbstractRector
{
/**
* @see https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
*/
private const ALL_MODIFIERS = 'imsxeADSUXJu';

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Add preg_quote delimiter when missing', [
new CodeSample(
<<<'PHP'
class SomeClass
{
public function test()
{
return preg_quote('name');
}
}
'#' . preg_quote('name') . '#';
PHP
,
<<<'PHP'
class SomeClass
{
public function test()
{
return preg_quote('name', '#');
}
}
'#' . preg_quote('name', '#') . '#';
PHP

),
Expand Down Expand Up @@ -67,8 +62,54 @@ public function refactor(Node $node): ?Node
return null;
}

$node->args[1] = new Arg(new String_('#'));
$delimiter = $this->determineDelimiter($node);
if ($delimiter === null) {
return null;
}

$node->args[1] = new Arg(new String_($delimiter));

return $node;
}

private function determineDelimiter(FuncCall $funcCall)
{
$parent = $this->getUppermostConcat($funcCall);
if ($parent === null) {
return null;
}
$leftMostConcatNode = $parent->left;
while ($leftMostConcatNode instanceof Concat) {
$leftMostConcatNode = $leftMostConcatNode->left;
}
$rightMostConcatNode = $parent->right;
while ($rightMostConcatNode instanceof Concat) {
$rightMostConcatNode = $rightMostConcatNode->right;
}

if (! $leftMostConcatNode instanceof String_) {
return null;
}
$possibleLeftDelimiter = substr($leftMostConcatNode->value, 0, 1);
if (! $rightMostConcatNode instanceof String_) {
return null;
}
$possibleRightDelimiter = substr(rtrim($rightMostConcatNode->value, self::ALL_MODIFIERS), -1, 1);
if ($possibleLeftDelimiter === $possibleRightDelimiter) {
return $possibleLeftDelimiter;
}

return null;
}

private function getUppermostConcat(FuncCall $funcCall): ?Concat
{
$upperMostConcat = null;
$parent = $funcCall->getAttribute(AttributeKey::PARENT_NODE);
while ($parent instanceof Concat) {
$upperMostConcat = $parent;
$parent = $parent->getAttribute(AttributeKey::PARENT_NODE);
}
return $upperMostConcat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ namespace Rector\CodeQuality\Tests\Rector\FuncCall\AddPregQuoteDelimiterRector\F

class SomeClass
{
public function test()
public function test($input)
{
return preg_quote('name');
$patttern1 = '/' . preg_quote('name'). '/';
$patttern2 = '#' . preg_quote('name'). '#';
$patttern3 = '#\d' . preg_quote('name'). '\d#';
}
}

Expand All @@ -18,9 +20,11 @@ namespace Rector\CodeQuality\Tests\Rector\FuncCall\AddPregQuoteDelimiterRector\F

class SomeClass
{
public function test()
public function test($input)
{
return preg_quote('name', '#');
$patttern1 = '/' . preg_quote('name', '/'). '/';
$patttern2 = '#' . preg_quote('name', '#'). '#';
$patttern3 = '#\d' . preg_quote('name', '#'). '\d#';
}
}

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

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

class SomeClass
{
public function test()
{
$pattern1 = preg_quote('name');
$pattern2 = '/' . preg_quote('name');
$pattern3 = preg_quote('name') . '/';
$pattern4 = '#' . preg_quote('name') . '/';
}
}

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

namespace Rector\CodeQuality\Tests\Rector\FuncCall\AddPregQuoteDelimiterRector\WithModifiers;

class SomeClass
{
public function test($input)
{
$patttern1 = '/' . preg_quote('name'). '/imsxeA';
$patttern2 = '#' . preg_quote('name'). '#DSUXJu';
}
}

?>
-----
<?php

namespace Rector\CodeQuality\Tests\Rector\FuncCall\AddPregQuoteDelimiterRector\WithModifiers;

class SomeClass
{
public function test($input)
{
$patttern1 = '/' . preg_quote('name', '/'). '/imsxeA';
$patttern2 = '#' . preg_quote('name', '#'). '#DSUXJu';
}
}

?>