-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sw6.7): Add logger to scheduled task handler (#42)
Co-authored-by: Shyim <[email protected]>
- Loading branch information
Showing
6 changed files
with
141 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
73 changes: 73 additions & 0 deletions
73
src/Rule/v67/AddLoggerToScheduledTaskConstructorRector.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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Frosh\Rector\Rule\v67; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\ValueObject\MethodName; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class AddLoggerToScheduledTaskConstructorRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Ensure that the parent constructor is called with a Psr\Log\LoggerInterface as second argument.', [ | ||
new CodeSample( | ||
<<<'PHP' | ||
class MyTaskHandler extends ScheduledTaskHandler { | ||
public function __construct(EntityRepository $scheduledTaskRepository) { | ||
parent::__construct($scheduledTaskRepository); | ||
} | ||
} | ||
PHP, | ||
<<<'PHP' | ||
class MyTaskHandler extends ScheduledTaskHandler { | ||
public function __construct(EntityRepository $scheduledTaskRepository, LoggerInterface $exceptionLogger) { | ||
parent::__construct($scheduledTaskRepository, $exceptionLogger); | ||
} | ||
} | ||
PHP | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Node\Stmt\Class_::class]; | ||
} | ||
|
||
public function refactor(Node $node): ?Node | ||
{ | ||
if (!$this->isObjectType($node, new ObjectType('Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler'))) { | ||
return null; | ||
} | ||
|
||
$constructor = $node->getMethod(MethodName::CONSTRUCT); | ||
if (!$constructor instanceof Node\Stmt\ClassMethod) { | ||
return null; | ||
} | ||
|
||
$missingScheduledTaskLogger = false; | ||
foreach ($constructor->stmts as $stmt) { | ||
if ($stmt instanceof Node\Stmt\Expression && $stmt->expr instanceof Node\Expr\StaticCall) { | ||
$staticCall = $stmt->expr; | ||
if ($this->isName($staticCall->name, MethodName::CONSTRUCT) && \count($staticCall->args) === 1) { | ||
$missingScheduledTaskLogger = true; | ||
$staticCall->args[] = $this->nodeFactory->createArg(new Node\Expr\Variable('exceptionLogger')); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
if ($missingScheduledTaskLogger) { | ||
$constructor->params[] = $this->nodeFactory->createParamFromNameAndType('exceptionLogger', new ObjectType('Psr\Log\LoggerInterface')); | ||
} | ||
|
||
return null; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
stubs/Shopware/Core/Framework/MessageQueue/ScheduledTask/ScheduledTaskHandler.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,7 @@ | ||
<?php | ||
|
||
namespace Shopware\Core\Framework\MessageQueue\ScheduledTask; | ||
|
||
class ScheduledTaskHandler | ||
{ | ||
} |
14 changes: 14 additions & 0 deletions
14
...7/AddLoggerToScheduledTaskConstructorRector/AddLoggerToScheduledTaskConstructorRector.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,14 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Frosh\Rector\Tests\Rector\v67\AddLoggerToScheduledTaskConstructorRector; | ||
|
||
use Frosh\Rector\Tests\Rector\AbstractFroshRectorTestCase; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Frosh\Rector\Rule\v67\AddLoggerToScheduledTaskConstructorRector | ||
*/ | ||
class AddLoggerToScheduledTaskConstructorRector extends AbstractFroshRectorTestCase | ||
{ | ||
} |
34 changes: 34 additions & 0 deletions
34
...ctor/v67/AddLoggerToScheduledTaskConstructorRector/Fixture/MyScheduledTaskHandler.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,34 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Frosh\Rector\Tests\Rector\v67\AddLoggerToScheduledTaskConstructorRector\Fixture; | ||
|
||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler; | ||
|
||
class MyScheduledTaskHandler extends ScheduledTaskHandler | ||
{ | ||
public function __construct(EntityRepository $scheduledTaskRepository) { | ||
parent::__construct($scheduledTaskRepository); | ||
} | ||
|
||
public function run(): void {} | ||
} | ||
?> | ||
----- | ||
<?php declare(strict_types=1); | ||
|
||
namespace Frosh\Rector\Tests\Rector\v67\AddLoggerToScheduledTaskConstructorRector\Fixture; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler; | ||
|
||
class MyScheduledTaskHandler extends ScheduledTaskHandler | ||
{ | ||
public function __construct(EntityRepository $scheduledTaskRepository, LoggerInterface $exceptionLogger) { | ||
parent::__construct($scheduledTaskRepository, $exceptionLogger); | ||
} | ||
|
||
public function run(): void {} | ||
} | ||
?> |
11 changes: 11 additions & 0 deletions
11
tests/Rector/v67/AddLoggerToScheduledTaskConstructorRector/config/configured_rule.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Frosh\Rector\Rule\v67\AddLoggerToScheduledTaskConstructorRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config_test.php'); | ||
$rectorConfig->rule(AddLoggerToScheduledTaskConstructorRector::class); | ||
}; |