Skip to content

Commit

Permalink
Fix double deprecated calls when already refactored
Browse files Browse the repository at this point in the history
This will fix the double deprecated calls. In the future we might need to refactor this, since something might actually need a nested deprecated call. That would be kinda nasty, but is an edge case that could happen (tm).
  • Loading branch information
bbrala committed Jan 17, 2024
1 parent 0617aa1 commit cdc3c55
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Rector/AbstractDrupalCoreRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use DrupalRector\Contract\VersionedConfigurationInterface;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PHPStan\Reflection\MethodReflection;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;

abstract class AbstractDrupalCoreRector extends AbstractRector implements ConfigurableRectorInterface
Expand All @@ -29,6 +31,31 @@ public function configure(array $configuration): void
$this->configuration = $configuration;
}

protected function isInBackwardsCompatibleCall(Node $node): bool
{
if (!class_exists(DeprecationHelper::class)) {
return false;
}

$scope = $node->getAttribute(AttributeKey::SCOPE);

$callStack = $scope->getFunctionCallStackWithParameters();
if (count($callStack) === 0) {
return false;
}
[$function, $parameter] = $callStack[0];
if (!$function instanceof MethodReflection) {
return false;
}
if ($function->getName() !== 'backwardsCompatibleCall'
|| $function->getDeclaringClass()->getName() !== DeprecationHelper::class
) {
return false;
}

return $parameter !== null && $parameter->getName() === 'deprecatedCallable';
}

public function refactor(Node $node)
{
$drupalVersion = str_replace(['.x-dev', '-dev'], '.0', \Drupal::VERSION);
Expand All @@ -38,6 +65,10 @@ public function refactor(Node $node)
continue;
}

if ($this->isInBackwardsCompatibleCall($node)) {
continue;
}

$result = $this->refactorWithConfiguration($node, $configuration);

// Skip if no result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ function advanced() {
watchdog_exception('update', $exception, 'My custom message @foo', ['@foo' => 'bar'], RfcLogLevel::CRITICAL, 'http://example.com');

watchdog_exception('update', $exception, 'My custom message @foo', ['@foo' => 'bar']);

\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => \Drupal\Core\Utility\Error::logException(\Drupal::logger('update'), $exception), fn() => watchdog_exception('update', $exception));
}
?>
-----
Expand All @@ -27,5 +29,7 @@ function advanced() {
\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => \Drupal\Core\Utility\Error::logException(\Drupal::logger('update'), $exception, 'My custom message @foo', ['@foo' => 'bar', 'link' => 'http://example.com'], RfcLogLevel::CRITICAL), fn() => watchdog_exception('update', $exception, 'My custom message @foo', ['@foo' => 'bar', 'link' => 'http://example.com'], RfcLogLevel::CRITICAL, 'http://example.com'));

\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => \Drupal\Core\Utility\Error::logException(\Drupal::logger('update'), $exception, 'My custom message @foo', ['@foo' => 'bar']), fn() => watchdog_exception('update', $exception, 'My custom message @foo', ['@foo' => 'bar']));

\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => \Drupal\Core\Utility\Error::logException(\Drupal::logger('update'), $exception), fn() => watchdog_exception('update', $exception));
}
?>

0 comments on commit cdc3c55

Please sign in to comment.