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
1 change: 1 addition & 0 deletions config/set/code-quality/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ services:
Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector: ~
Rector\CodeQuality\Rector\If_\ShortenElseIfRector: ~
Rector\SOLID\Rector\ClassMethod\UseInterfaceOverImplementationInConstructorRector: ~
Rector\CodeQuality\Rector\FuncCall\AddPregQuoteDelimiterRector: ~
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ private function processGroupNamesBySuffix(
}

// is suffix in the same category, e.g. "Exception/SomeException.php"
$expectedLocationFilePattern = sprintf('#\/%s\/.+%s#', preg_quote($groupName), preg_quote($suffixPattern));
$expectedLocationFilePattern = sprintf(
'#\/%s\/.+%s#',
preg_quote($groupName, '#'),
preg_quote($suffixPattern, '#')
);
if (Strings::match($smartFileInfo->getRealPath(), $expectedLocationFilePattern)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function isTagMatchToNodeAndClass(string $tag, Node $node, string $matchi
private function isUseMatchingName(string $tag, UseUse $useUse): bool
{
$shortName = $useUse->alias ? $useUse->alias->name : $useUse->name->getLast();
$shortNamePattern = preg_quote($shortName);
$shortNamePattern = preg_quote($shortName, '#');

return (bool) Strings::match($tag, '#' . $shortNamePattern . '(\\\\[\w]+)?#i');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\AddPregQuoteDelimiterRector\AddPregQuoteDelimiterRectorTest
*/
final class AddPregQuoteDelimiterRector extends AbstractRector
{
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');
}
}
PHP
,
<<<'PHP'
class SomeClass
{
public function test()
{
return preg_quote('name', '#');
}
}
PHP

),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'preg_quote')) {
return null;
}

// already completed
if (isset($node->args[1])) {
return null;
}

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

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

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

use Iterator;
use Rector\CodeQuality\Rector\FuncCall\AddPregQuoteDelimiterRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddPregQuoteDelimiterRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

public function provideDataForTest(): Iterator
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
yield [__DIR__ . '/Fixture/skip.php.inc'];
}

protected function getRectorClass(): string
{
return AddPregQuoteDelimiterRector::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

class SomeClass
{
public function test()
{
return preg_quote('name');
}
}

?>
-----
<?php

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

class SomeClass
{
public function test()
{
return preg_quote('name', '#');
}
}

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

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

class Skip
{
public function test()
{
return preg_quote('name', '_');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ private function isCurrentNamespaceSameShortClassAlreadyUsed(
// short with space " Type"| fqn
$shortNameOrFullyQualifiedNamePattern = sprintf(
'#(\s%s\b|\b%s\b)#',
preg_quote($shortenedObjectType->getShortName()),
preg_quote($fullyQualifiedName)
preg_quote($shortenedObjectType->getShortName(), '#'),
preg_quote($fullyQualifiedName, '#')
);

$isShortClassUsed = (bool) Strings::match($printedClass, $shortNameOrFullyQualifiedNamePattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function refactor(Node $node): ?Node

$dataProviderClassMethods = $this->createDataProviderClassMethodsFromRecipes();

$node->stmts = array_Merge($node->stmts, $dataProviderClassMethods);
$node->stmts = array_merge($node->stmts, $dataProviderClassMethods);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private function processExceptionMessageContains(MethodCall $methodCall, Variabl
if ($methodCall->args[0]->value instanceof String_) {
/** @var String_ $oldString */
$oldString = $methodCall->args[0]->value;
$methodCall->args[0]->value = new String_('#' . preg_quote($oldString->value) . '#');
$methodCall->args[0]->value = new String_('#' . preg_quote($oldString->value, '#') . '#');
}

$this->newExpressions[] = $expression;
Expand Down
3 changes: 2 additions & 1 deletion rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ parameters:
php_version_features: '7.1'

services:
Rector\PHPUnit\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector: ~
Rector\CodeQuality\Rector\FuncCall\AddPregQuoteDelimiterRector: ~
# Rector\PHPUnit\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector: ~