diff --git a/config/nextcloud-31/nextcloud-31-deprecations.php b/config/nextcloud-31/nextcloud-31-deprecations.php new file mode 100644 index 0000000..fcaec39 --- /dev/null +++ b/config/nextcloud-31/nextcloud-31-deprecations.php @@ -0,0 +1,10 @@ +rule(ILoggerToPsrLoggerLevelsRector::class); +}; diff --git a/src/Rector/ILoggerToPsrLoggerLevelsRector.php b/src/Rector/ILoggerToPsrLoggerLevelsRector.php new file mode 100644 index 0000000..5fbbce4 --- /dev/null +++ b/src/Rector/ILoggerToPsrLoggerLevelsRector.php @@ -0,0 +1,81 @@ +> + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + public function refactor(Node $node): ?Node + { + if (!($node instanceof MethodCall)) { + return null; + } + $methodCallName = $this->getName($node->name); + if ($methodCallName !== 'log') { + return null; + } + if ( + !($node->args[0] instanceof Node\Arg) + || !($node->args[0]->value instanceof ClassConstFetch) + || $this->getName($node->args[0]->value->class) !== 'OCP\ILogger' + ) { + return null; + } + + return new MethodCall( + $node->var, + 'log', + [ + new Node\Arg(new ClassConstFetch( + new FullyQualified('Psr\Log\LogLevel'), + new Identifier(match ($this->getName($node->args[0]->value->name)) { + 'DEBUG' => 'DEBUG', + 'INFO' => 'INFO', + 'WARN' => 'WARNING', + 'FATAL' => 'EMERGENCY', + default => 'ERROR', + }), + )), + ...array_slice($node->args, 1), + ], + ); + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Change method calls from set* to change*.', + [ + new CodeSample( + '\OC::$server->get(IAppConfig::class);', + '\OCP\Server::get(IAppConfig::class);', + ), + ], + ); + } +} diff --git a/src/Set/NextcloudSets.php b/src/Set/NextcloudSets.php index ce7a26b..22fca98 100644 --- a/src/Set/NextcloudSets.php +++ b/src/Set/NextcloudSets.php @@ -15,4 +15,5 @@ final class NextcloudSets implements SetListInterface public const NEXTCLOUD_25 = __DIR__ . '/../../config/nextcloud-25/nextcloud-25-deprecations.php'; public const NEXTCLOUD_26 = __DIR__ . '/../../config/nextcloud-26/nextcloud-26-deprecations.php'; public const NEXTCLOUD_27 = __DIR__ . '/../../config/nextcloud-27/nextcloud-27-deprecations.php'; + public const NEXTCLOUD_31 = __DIR__ . '/../../config/nextcloud-31/nextcloud-31-deprecations.php'; } diff --git a/tests/Rector/ILoggerToPsrLoggerLevelsRector/Fixture/test_fixture.php.inc b/tests/Rector/ILoggerToPsrLoggerLevelsRector/Fixture/test_fixture.php.inc new file mode 100644 index 0000000..db64ad1 --- /dev/null +++ b/tests/Rector/ILoggerToPsrLoggerLevelsRector/Fixture/test_fixture.php.inc @@ -0,0 +1,35 @@ +logger->log(LogLevel::ALERT, 'alarm!'); + $this->logger->log(ILogger::ERROR, 'warn!'); + $this->logger->log(ILogger::DEBUG, 'debug!'); + } +} +?> +----- +logger->log(LogLevel::ALERT, 'alarm!'); + $this->logger->log(\Psr\Log\LogLevel::ERROR, 'warn!'); + $this->logger->log(\Psr\Log\LogLevel::DEBUG, 'debug!'); + } +} +?> diff --git a/tests/Rector/ILoggerToPsrLoggerLevelsRector/ILoggerToPsrLoggerLevelsRectorTest.php b/tests/Rector/ILoggerToPsrLoggerLevelsRector/ILoggerToPsrLoggerLevelsRectorTest.php new file mode 100644 index 0000000..e23fd39 --- /dev/null +++ b/tests/Rector/ILoggerToPsrLoggerLevelsRector/ILoggerToPsrLoggerLevelsRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/config.php'; + } +} diff --git a/tests/Rector/ILoggerToPsrLoggerLevelsRector/config/config.php b/tests/Rector/ILoggerToPsrLoggerLevelsRector/config/config.php new file mode 100644 index 0000000..a46ea42 --- /dev/null +++ b/tests/Rector/ILoggerToPsrLoggerLevelsRector/config/config.php @@ -0,0 +1,11 @@ +withRules([ + ILoggerToPsrLoggerLevelsRector::class, + ]);