-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Rector to rewrite System::log to Monolog
- Loading branch information
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Contao\Rector\Rector; | ||
|
||
use Contao\CoreBundle\Monolog\ContaoContext; | ||
use Contao\System; | ||
use PhpParser\Node; | ||
use Psr\Log\LogLevel; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class SystemLogToMonologRector extends AbstractLegacyFrameworkCallRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Rewrites deprecated System::log() calls to Monolog', [ | ||
new CodeSample( | ||
<<<'CODE_BEFORE' | ||
\Contao\System::log('generic log message', __METHOD__, TL_ACCESS); | ||
\Contao\System::log('error message', __METHOD__, TL_ERROR); | ||
CODE_BEFORE | ||
, | ||
<<<'CODE_AFTER' | ||
\Contao\System::getContainer()->get('logger')->log(\Psr\Log\LogLevel::INFO, 'generic log message', ['contao' => new \Contao\CoreBundle\Monolog\ContaoContext(__METHOD__, \Contao\CoreBundle\Monolog\ContaoContext::ACCESS)]); | ||
\Contao\System::getContainer()->get('logger')->log(\Psr\Log\LogLevel::ERROR, 'error message', ['contao' => new \Contao\CoreBundle\Monolog\ContaoContext(__METHOD__, \Contao\CoreBundle\Monolog\ContaoContext::ERROR)]); | ||
CODE_AFTER | ||
), | ||
]); | ||
} | ||
|
||
public function refactor(Node $node): ?Node | ||
{ | ||
assert($node instanceof Node\Expr\StaticCall || $node instanceof Node\Expr\MethodCall); | ||
|
||
if (!$this->isParentStaticOrMethodClassCall($node, System::class, 'log')) { | ||
return null; | ||
} | ||
|
||
$args = $node->getArgs(); | ||
$message = $args[0]; | ||
$method = $args[1]; | ||
$level = $args[2]->value; | ||
|
||
if ($level instanceof Node\Expr\ConstFetch) { | ||
$name = $this->getName($level->name); | ||
|
||
if (\in_array($name, [ | ||
'TL_ERROR', | ||
'TL_ACCESS', | ||
'TL_GENERAL', | ||
'TL_FILES', | ||
'TL_CRON', | ||
'TL_FORMS', | ||
'TL_EMAIL', | ||
'TL_CONFIGURATION', | ||
'TL_NEWSLETTER', | ||
'TL_REPOSITORY', | ||
])) { | ||
$name = substr($name, 3); | ||
$level = new Node\Expr\ClassConstFetch(new Node\Name\FullyQualified(ContaoContext::class), $name); | ||
} | ||
} | ||
|
||
$logLevel = 'INFO'; | ||
if ('Contao\CoreBundle\Monolog\ContaoContext::ERROR' === $this->getName($level) || 'ERROR' === $level->value) { | ||
$logLevel = 'ERROR'; | ||
} | ||
|
||
$context = new Node\Expr\New_(new Node\Name\FullyQualified(ContaoContext::class), $this->nodeFactory->createArgs([$method, $level])); | ||
$levelConst = new Node\Expr\ClassConstFetch(new Node\Name\FullyQualified(LogLevel::class), $logLevel); | ||
|
||
$container = new Node\Expr\StaticCall(new Node\Name\FullyQualified(System::class), 'getContainer'); | ||
$service = new Node\Expr\MethodCall($container, 'get', [new Node\Arg(new Node\Scalar\String_('monolog.logger.contao'))]); | ||
$node = new Node\Expr\MethodCall($service, 'log', $this->nodeFactory->createArgs([$levelConst, $message, ['contao' => $context]])); | ||
|
||
return $node; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/Rector/SystemLogToMonologRector/SystemLogToMonologRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Contao\Rector\Tests\Rector\SystemLogToMonologRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class SystemLogToMonologRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/config.php'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Contao\Rector\Rector\SystemLogToMonologRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(SystemLogToMonologRector::class); | ||
}; |
33 changes: 33 additions & 0 deletions
33
tests/Rector/SystemLogToMonologRector/fixture/test.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Contao\Controller; | ||
|
||
class Foo extends Controller | ||
{ | ||
public function bar() | ||
{ | ||
\Contao\System::log('generic log message', __METHOD__, TL_ACCESS); | ||
\Contao\System::log('error message', __METHOD__, TL_ERROR); | ||
\Contao\System::log('error message', __METHOD__, 'ERROR'); | ||
\Contao\System::log('error message', 'some_method', TL_ERROR); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use Contao\Controller; | ||
|
||
class Foo extends Controller | ||
{ | ||
public function bar() | ||
{ | ||
\Contao\System::getContainer()->get('monolog.logger.contao')->log(\Psr\Log\LogLevel::INFO, 'generic log message', ['contao' => new \Contao\CoreBundle\Monolog\ContaoContext(__METHOD__, \Contao\CoreBundle\Monolog\ContaoContext::ACCESS)]); | ||
\Contao\System::getContainer()->get('monolog.logger.contao')->log(\Psr\Log\LogLevel::ERROR, 'error message', ['contao' => new \Contao\CoreBundle\Monolog\ContaoContext(__METHOD__, \Contao\CoreBundle\Monolog\ContaoContext::ERROR)]); | ||
\Contao\System::getContainer()->get('monolog.logger.contao')->log(\Psr\Log\LogLevel::ERROR, 'error message', ['contao' => new \Contao\CoreBundle\Monolog\ContaoContext(__METHOD__, 'ERROR')]); | ||
\Contao\System::getContainer()->get('monolog.logger.contao')->log(\Psr\Log\LogLevel::ERROR, 'error message', ['contao' => new \Contao\CoreBundle\Monolog\ContaoContext('some_method', \Contao\CoreBundle\Monolog\ContaoContext::ERROR)]); | ||
} | ||
} | ||
|
||
?> |