fix pattern miss matching (#2639)

fix pattern miss matching
This commit is contained in:
Tomas Votruba 2020-01-11 11:10:55 +01:00 committed by GitHub
commit 3f805d93c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 1 deletions

View File

@ -0,0 +1,45 @@
<?php
namespace Rector\Renaming\Tests\Rector\MethodCall\RenameMethodCallRector\Fixture;
use Rector\Renaming\Tests\Rector\MethodCall\RenameMethodCallRector\Source\ClassMethodToBeSkipped;
use Rector\Renaming\Tests\Rector\MethodCall\RenameMethodCallRector\Source\SomeTranslator;
class UnderScoreOnly
{
/**
* @var SomeTranslator
*/
private $translator;
private function createHtml()
{
$this->translator->__('...');
$this->translator->getLocale();
}
}
?>
-----
<?php
namespace Rector\Renaming\Tests\Rector\MethodCall\RenameMethodCallRector\Fixture;
use Rector\Renaming\Tests\Rector\MethodCall\RenameMethodCallRector\Source\ClassMethodToBeSkipped;
use Rector\Renaming\Tests\Rector\MethodCall\RenameMethodCallRector\Source\SomeTranslator;
class UnderScoreOnly
{
/**
* @var SomeTranslator
*/
private $translator;
private function createHtml()
{
$this->translator->trans('...');
$this->translator->getLocale();
}
}
?>

View File

@ -8,6 +8,7 @@ use Iterator;
use Nette\Utils\Html;
use Rector\Renaming\Rector\MethodCall\RenameMethodCallRector;
use Rector\Renaming\Tests\Rector\MethodCall\RenameMethodCallRector\Source\ClassMethodToBeSkipped;
use Rector\Renaming\Tests\Rector\MethodCall\RenameMethodCallRector\Source\SomeTranslator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class RenameMethodCallRectorTest extends AbstractRectorTestCase
@ -43,6 +44,10 @@ final class RenameMethodCallRectorTest extends AbstractRectorTestCase
ClassMethodToBeSkipped::class => [
'createHtml' => 'testHtml',
],
SomeTranslator::class => [
'__' => 'trans',
'__t' => 'trans',
],
],
],
];

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Rector\Renaming\Tests\Rector\MethodCall\RenameMethodCallRector\Source;
final class SomeTranslator
{
}

View File

@ -59,7 +59,7 @@ final class NameResolver
}
// is probably regex pattern
if (($name[0] === $name[strlen($name) - 1]) && ! ctype_alpha($name[0])) {
if ($this->isRegexPattern($name)) {
return (bool) Strings::match($resolvedName, $name);
}
@ -224,4 +224,22 @@ final class NameResolver
return (string) $functionName;
}
private function isRegexPattern(string $name): bool
{
if (Strings::length($name) <= 2) {
return false;
}
$firstChar = $name[0];
$lastChar = $name[strlen($name) - 1];
if ($firstChar !== $lastChar) {
return false;
}
// this prevents miss matching like "aMethoda"
$possibleDelimiters = ['#', '~', '/'];
return in_array($firstChar, $possibleDelimiters, true);
}
}