Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ final class CatchExceptionNameMatchingTypeRectorTest extends AbstractRectorTestC
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/skip.php.inc']);
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/nested_call.php.inc',
__DIR__ . '/Fixture/skip.php.inc',
]);
}

protected function getRectorClass(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\Catch_\CatchExceptionNameMatchingTypeRector\Fixture;

class NestedCall
{
public function run()
{
try {
// ...
} catch (SomeException $typoException) {
$typoException->getMessage();

if ($typoException) {
$processMe = function () use ($typoException) {
return (bool) $typoException;
};
}
}
}
}

?>
-----
<?php

namespace Rector\CodingStyle\Tests\Rector\Catch_\CatchExceptionNameMatchingTypeRector\Fixture;

class NestedCall
{
public function run()
{
try {
// ...
} catch (SomeException $someException) {
$someException->getMessage();

if ($someException) {
$processMe = function () use ($someException) {
return (bool) $someException;
};
}
}
}
}

?>
3 changes: 2 additions & 1 deletion packages/Php/src/Rector/FuncCall/CountOnNullRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\LNumber;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -75,7 +76,7 @@ public function refactor(Node $node): ?Node
$ternaryNode = new Ternary($identicalNode, new LNumber(0), $node);
} else {
if ($this->isAtLeastPhpVersion('7.3')) {
$conditionNode = new FuncCall(new Node\Name('is_countable'), [new Arg($countedNode)]);
$conditionNode = new FuncCall(new Name('is_countable'), [new Arg($countedNode)]);
} else {
$conditionNode = new BooleanOr(
$this->createFunction('is_array', [new Arg($countedNode)]),
Expand Down
3 changes: 2 additions & 1 deletion packages/Symfony/src/Rector/Class_/MakeCommandLazyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\StringType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -83,7 +84,7 @@ public function refactor(Node $node): ?Node
return $node;
}

private function createDefaultNameProperty(Node $commandNameNode): Node\Stmt\Property
private function createDefaultNameProperty(Node $commandNameNode): Property
{
return $this->builderFactory->property('defaultName')
->makeProtected()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Rector\Symfony\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -33,9 +35,9 @@ public function getDefinition(): RectorDefinition

class SomeClass
{
public function run(EventDispatcherInterface $eventDisptacher)
public function run(EventDispatcherInterface $eventDispatcher)
{
$eventDisptacher->dispatch('event_name', new Event());
$eventDispatcher->dispatch('event_name', new Event());
}
}
CODE_SAMPLE
Expand All @@ -45,9 +47,9 @@ public function run(EventDispatcherInterface $eventDisptacher)

class SomeClass
{
public function run(EventDispatcherInterface $eventDisptacher)
public function run(EventDispatcherInterface $eventDispatcher)
{
$eventDisptacher->dispatch(new Event(), 'event_name');
$eventDispatcher->dispatch(new Event(), 'event_name');
}
}
CODE_SAMPLE
Expand Down Expand Up @@ -87,6 +89,30 @@ public function refactor(Node $node): ?Node
// swap arguments
[$node->args[0], $node->args[1]] = [$node->args[1], $node->args[0]];

if ($this->isEventNameSameAsEventObjectClass($node)) {
unset($node->args[1]);
}

return $node;
}

/**
* Is the event name just `::class`?
* We can remove it
*/
private function isEventNameSameAsEventObjectClass(MethodCall $methodCall): bool
{
if (! $methodCall->args[1]->value instanceof ClassConstFetch) {
return false;
}

$classConst = $this->getValue($methodCall->args[1]->value);
$eventStaticType = $this->getStaticType($methodCall->args[0]->value);

if (! $eventStaticType instanceof ObjectType) {
return false;
}

return $classConst === $eventStaticType->getClassName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Fixture;

use Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Source\CustomEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;

class EventClassConstant
{
public function run(EventDispatcher $eventDispatcher)
{
$customEvent = new CustomEvent();
$eventDispatcher->dispatch(CustomEvent::class, $customEvent);
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Fixture;

use Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Source\CustomEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;

class EventClassConstant
{
public function run(EventDispatcher $eventDispatcher)
{
$customEvent = new CustomEvent();
$eventDispatcher->dispatch($customEvent);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Fixture;

use Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Source\CustomEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;

class KeepStringEventConstant
{
public function run(EventDispatcher $eventDispatcher)
{
$customEvent = new CustomEvent();
$eventDispatcher->dispatch(CustomEvent::NAME, $customEvent);
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Fixture;

use Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Source\CustomEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;

class KeepStringEventConstant
{
public function run(EventDispatcher $eventDispatcher)
{
$customEvent = new CustomEvent();
$eventDispatcher->dispatch($customEvent, CustomEvent::NAME);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ final class MakeDispatchFirstArgumentEventRectorTest extends AbstractRectorTestC
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/event_class_constant.php.inc',
__DIR__ . '/Fixture/keep_string_event_constant.php.inc',
]);
}

protected function getRectorClass(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Rector\Symfony\Tests\Rector\MethodCall\MakeDispatchFirstArgumentEventRector\Source;

use Symfony\Contracts\EventDispatcher\Event;

final class CustomEvent extends Event
{
public const NAME = 'custom_event';
}
3 changes: 2 additions & 1 deletion src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ final public function enterNode(Node $node)
return null;
}

$originalNode = clone $node;
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? clone $node;
$originalComment = $node->getComments();
$originalDocComment = $node->getDocComment();
$node = $this->refactor($node);
Expand All @@ -102,6 +102,7 @@ final public function enterNode(Node $node)
$this->notifyNodeChangeFileInfo($node);
}

// if stmt ("$value;") was replaced by expr ("$value"), add the ending ";" (Expression) to prevent breaking code
if ($originalNode instanceof Stmt && $node instanceof Expr) {
return new Expression($node);
}
Expand Down