-
-
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.
Merge pull request #98 from neos/feature/signal-slot-warning
FEATURE: Add warnings for removed signals
- Loading branch information
Showing
12 changed files
with
684 additions
and
1 deletion.
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,107 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
|
||
namespace Neos\Rector\Generic\Rules; | ||
|
||
use Neos\Flow\SignalSlot\Dispatcher; | ||
use Neos\Rector\Generic\ValueObject\MethodCallToWarningComment; | ||
use Neos\Rector\Generic\ValueObject\SignalSlotToWarningComment; | ||
use Neos\Rector\Utility\CodeSampleLoader; | ||
use PhpParser\Node; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\PostRector\Collector\NodesToAddCollector; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub; | ||
|
||
final class SignalSlotToWarningCommentRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
use AllTraits; | ||
|
||
/** | ||
* @var SignalSlotToWarningComment[] | ||
*/ | ||
private array $signalSlotToWarningComments = []; | ||
|
||
public function __construct( | ||
private readonly NodesToAddCollector $nodesToAddCollector | ||
) | ||
{ | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return CodeSampleLoader::fromFile('"Warning comments for various non-supported signals', __CLASS__, [ | ||
new SignalSlotToWarningComment(Node::class, 'beforeMove', '!! This signal "beforeMove" on Node doesn\'t exist anymore') | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [\PhpParser\Node\Expr\MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Expr\MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
assert($node instanceof Node\Expr\MethodCall); | ||
|
||
if (!$this->isName($node->name, 'connect')) { | ||
return null; | ||
} | ||
|
||
if (!$this->isObjectType($node->var, new ObjectType(Dispatcher::class))) { | ||
return null; | ||
} | ||
|
||
foreach ($this->signalSlotToWarningComments as $signalSlotToWarningComment) { | ||
$className = null; | ||
if ($node->args[0]->value instanceof Node\Expr\ClassConstFetch) { | ||
$className = (string) $node->args[0]->value->class; | ||
} elseif ($node->args[0]->value instanceof Node\Scalar) { | ||
$className = (string)$node->args[0]->value->value; | ||
} | ||
|
||
if ($className !== $signalSlotToWarningComment->className){ | ||
continue; | ||
} | ||
|
||
$methodName = null; | ||
if ($node->args[1]->value instanceof Node\Scalar\String_) { | ||
$methodName = (string)$node->args[1]->value->value; | ||
} | ||
|
||
if ($methodName !== $signalSlotToWarningComment->signalName) { | ||
continue; | ||
} | ||
|
||
$this->nodesToAddCollector->addNodesBeforeNode( | ||
[ | ||
self::todoComment($signalSlotToWarningComment->warningMessage) | ||
], | ||
$node | ||
); | ||
|
||
return $node; | ||
} | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* @param mixed[] $configuration | ||
*/ | ||
public function configure(array $configuration) : void | ||
{ | ||
Assert::allIsAOf($configuration, SignalSlotToWarningComment::class); | ||
$this->signalSlotToWarningComments = $configuration; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neos\Rector\Generic\ValueObject; | ||
|
||
class SignalSlotToWarningComment | ||
{ | ||
public function __construct( | ||
public readonly string $className, | ||
public readonly string $signalName, | ||
public readonly string $warningMessage, | ||
) | ||
{ | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
tests/Generic/Rules/SignalSlotToWarningCommentRector/Fixture/some_class.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,95 @@ | ||
<?php | ||
|
||
use Neos\Flow\Core\Bootstrap; | ||
use Neos\Flow\Package\Package as BasePackage; | ||
use Neos\Flow\SignalSlot\Dispatcher; | ||
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub; | ||
|
||
class Package extends BasePackage | ||
{ | ||
public function boot(Bootstrap $bootstrap) | ||
{ | ||
/** @var Dispatcher $dispatcher */ | ||
$dispatcher = $bootstrap->getSignalSlotDispatcher(); | ||
|
||
$dispatcher->connect( | ||
NodeLegacyStub::class, | ||
'beforeMove', | ||
SomeOtherClass::class, | ||
'someMethod' | ||
); | ||
|
||
$dispatcher->connect( | ||
'Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub', | ||
'afterMove', | ||
SomeOtherClass::class, | ||
'someMethod' | ||
); | ||
|
||
$dispatcher->connect( | ||
NodeLegacyStub::class, | ||
'otherMethod', | ||
SomeOtherClass::class, | ||
'someMethod' | ||
); | ||
|
||
$dispatcher->connect( | ||
OtherClass::class, | ||
'afterMove', | ||
SomeOtherClass::class, | ||
'someMethod' | ||
); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use Neos\Flow\Core\Bootstrap; | ||
use Neos\Flow\Package\Package as BasePackage; | ||
use Neos\Flow\SignalSlot\Dispatcher; | ||
use Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub; | ||
|
||
class Package extends BasePackage | ||
{ | ||
public function boot(Bootstrap $bootstrap) | ||
{ | ||
/** @var Dispatcher $dispatcher */ | ||
$dispatcher = $bootstrap->getSignalSlotDispatcher(); | ||
// TODO 9.0 migration: Signal "beforeMove" doesn't exist anymore | ||
|
||
|
||
$dispatcher->connect( | ||
NodeLegacyStub::class, | ||
'beforeMove', | ||
SomeOtherClass::class, | ||
'someMethod' | ||
); | ||
// TODO 9.0 migration: Signal "afterMove" doesn't exist anymore | ||
|
||
|
||
$dispatcher->connect( | ||
'Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub', | ||
'afterMove', | ||
SomeOtherClass::class, | ||
'someMethod' | ||
); | ||
|
||
$dispatcher->connect( | ||
NodeLegacyStub::class, | ||
'otherMethod', | ||
SomeOtherClass::class, | ||
'someMethod' | ||
); | ||
|
||
$dispatcher->connect( | ||
OtherClass::class, | ||
'afterMove', | ||
SomeOtherClass::class, | ||
'someMethod' | ||
); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...s/Generic/Rules/SignalSlotToWarningCommentRector/SignalSlotToWarningCommentRectorTest.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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Rector\Tests\Generic\Rules\ToStringToPropertyFetchRector; | ||
|
||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class SignalSlotToWarningCommentRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $fileInfo): void | ||
{ | ||
$this->doTestFile($fileInfo); | ||
} | ||
|
||
/** | ||
* @return \Iterator<string> | ||
*/ | ||
public function provideData(): \Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/Generic/Rules/SignalSlotToWarningCommentRector/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,14 @@ | ||
<?php | ||
|
||
declare (strict_types=1); | ||
|
||
use Neos\Rector\Generic\Rules\SignalSlotToWarningCommentRector; | ||
use Neos\Rector\Generic\ValueObject\SignalSlotToWarningComment; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig) : void { | ||
$rectorConfig->ruleWithConfiguration(SignalSlotToWarningCommentRector::class, [ | ||
new SignalSlotToWarningComment(Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class, 'beforeMove', 'Signal "beforeMove" doesn\'t exist anymore'), | ||
new SignalSlotToWarningComment(Neos\Rector\ContentRepository90\Legacy\NodeLegacyStub::class, 'afterMove', 'Signal "afterMove" doesn\'t exist anymore'), | ||
]); | ||
}; |
Oops, something went wrong.