This commit is contained in:
TomasVotruba 2017-09-07 06:10:54 +02:00
parent a532b5306f
commit ea51ca3c71
5 changed files with 45 additions and 35 deletions

View File

@ -47,3 +47,6 @@ parameters:
PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff:
# long FQN classes that might not exist
- src/Rector/Contrib/Symfony/FrameworkBundleClassReplacementsRector.php
Symplify\CodingStandard\Sniffs\Classes\EqualInterfaceImplementationSniff:
# empty parent interface, disable for now
- packages/TriggerExtractor/src/Deprecation/ClassMethodDeprecation.php

View File

@ -4,5 +4,4 @@ namespace Rector\TriggerExtractor\Contract\Deprecation;
interface DeprecationInterface
{
}

View File

@ -17,18 +17,13 @@ final class DeprecationFactory
/**
* @var string
*/
private const CLASS_REGEX = '([A-Za-z]+(\\\\[A-Za-z]+)+)';
/**
* @var string
*/
private const CLASS_PATTERN = '#^' . self::CLASS_REGEX . '#s';
private const CLASS_PART_PATTERN = '([A-Za-z]+(\\\\[A-Za-z]+)+)';
/**
* @var string
* @see https://regex101.com/r/WdGoyd/1
*/
private const CLASS_WITH_METHOD_PATTER = '#^' . self::CLASS_REGEX . '::[A-Za-z]+\(\)#s';
private const CLASS_WITH_METHOD_PATTERN = '#^' . self::CLASS_PART_PATTERN . '::[A-Za-z]+\(\)#s';
/**
* Probably resolve by recursion, similar too
@ -45,6 +40,37 @@ final class DeprecationFactory
return $this->createFromMesssage($message);
}
public function tryToCreateClassMethodDeprecation(string $oldMessage, string $newMessage): ?DeprecationInterface
{
// try to find "SomeClass::methodCall()"
$matches = Strings::matchAll($oldMessage, self::CLASS_WITH_METHOD_PATTERN);
if (isset($matches[0][0])) {
$oldClassWithMethod = $matches[0][0];
}
// try to find "SomeClass::methodCall()"
$matches = Strings::matchAll($newMessage, self::CLASS_WITH_METHOD_PATTERN);
if (isset($matches[0][0])) {
$newClassWithMethod = $matches[0][0];
}
if (isset($oldClassWithMethod, $newClassWithMethod)) {
[$oldClass, $oldMethod] = explode('::', $oldClassWithMethod);
[$newClass, $newMethod] = explode('::', $newClassWithMethod);
if ($oldClass === $newClass) {
// simple method replacement
return new ClassMethodDeprecation(
$oldClass,
rtrim($oldMethod, '()'),
rtrim($newMethod, '()')
);
}
}
return null;
}
private function processConcatNode(Node $node): string
{
if ($node instanceof Method) {
@ -114,32 +140,11 @@ final class DeprecationFactory
{
// format: don't use this, use that
if (Strings::contains($message, ' use ')) {
[$old, $new] = explode(' use ', $message);
[$oldMessage, $newMessage] = explode(' use ', $message);
// try to find SomeClass::methodCall()
$matches = Strings::matchAll($old, self::CLASS_WITH_METHOD_PATTER);
if (isset($matches[0][0])) {
$oldClassWithMethod = $matches[0][0];
}
// try to find SomeClass::methodCall()
$matches = Strings::matchAll($new, self::CLASS_WITH_METHOD_PATTER);
if (isset($matches[0][0])) {
$newClassWithMethod = $matches[0][0];
}
if (isset($oldClassWithMethod) && isset($newClassWithMethod)) {
[$oldClass, $oldMethod] = explode('::', $oldClassWithMethod);
[$newClass, $newMethod] = explode('::', $newClassWithMethod);
if ($oldClass === $newClass) {
// simple method replacement
return new ClassMethodDeprecation(
$oldClass,
rtrim($oldMethod, '()'),
rtrim($newMethod, '()')
);
}
$deprecation = $this->tryToCreateClassMethodDeprecation($oldMessage, $newMessage);
if ($deprecation) {
return $deprecation;
}
}

View File

@ -11,6 +11,9 @@ final class ConfigurableChangeMethodNameRector extends AbstractChangeMethodNameR
*/
private $perClassOldToNewMethod;
/**
* @param string[][] $perClassOldToNewMethod
*/
public function setPerClassOldToNewMethods(array $perClassOldToNewMethod): void
{
$this->perClassOldToNewMethod = $perClassOldToNewMethod;

View File

@ -51,8 +51,8 @@ final class RectorFactory
$configurableChangeMethodNameRector = clone $this->configurableChangeMethodNameRector;
$configurableChangeMethodNameRector->setPerClassOldToNewMethods([
$deprecation->getClass() => [
$deprecation->getOldMethod() => $deprecation->getNewMethod()
]
$deprecation->getOldMethod() => $deprecation->getNewMethod(),
],
]);
return $configurableChangeMethodNameRector;