DeprecationFactory: improve deprecation resolving

This commit is contained in:
TomasVotruba 2017-09-07 15:57:36 +02:00
parent 60e92328ca
commit 27c9d22d5a

View File

@ -16,14 +16,9 @@ final class DeprecationFactory
{
/**
* @var string
* @see https://regex101.com/r/WdGoyd/2
*/
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_PATTERN = '#^' . self::CLASS_PART_PATTERN . '::[A-Za-z]+\(\)#s';
private const CLASS_WITH_METHOD_PATTERN = '#^(?<classWithMethod>[A-Za-z]+[\\\\A-Za-z]+::[A-Za-z]+\([A-Za-z\']*\))#s';
/**
* Probably resolve by recursion, similar too
@ -40,37 +35,6 @@ 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) {
@ -92,6 +56,37 @@ final class DeprecationFactory
));
}
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]['classWithMethod'])) {
$oldClassWithMethod = $matches[0]['classWithMethod'];
}
// try to find "SomeClass::methodCall()"
$matches = Strings::matchAll($newMessage, self::CLASS_WITH_METHOD_PATTERN);
if (isset($matches[0]['classWithMethod'])) {
$newClassWithMethod = $matches[0]['classWithMethod'];
}
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 findParentOfType(Node $node, string $type): Node
{
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);