github rectorphp/rector 2.5.6
Released Rector 2.5.6

3 hours ago

New Features ๐Ÿฅณ

  • [DeadCode] Add RemoveReturnTagIncompatibleWithNativeTypeRector (#8172)
 final class SomeClass
 {
-    /**
-     * @return SomeObject
-     */
     public function getName(): string
     {
         return $this->someObject->getName();
     }
 }
  • [TypeDeclarationDocblocks] Add MergePhpstanDocTagIntoNativeRector to merge @phpstan-* doc tags into native tags (#8171)
 final class SomeClass
 {
     /**
-     * @var Collection
-     *
-     * @phpstan-var Collection<int, string>
+     * @var Collection<int, string>
      */
     private $items;
 }

Bugfixes ๐Ÿ›

  • [Php81] Skip array dim fetch args in NullToStrictStringFuncCallArgRector (#8164)
  • Fix scoper to clean up prefix under getRuleDefinition() method, parts 1โ€“5 (#8165, #8166, #8167, #8168, #8169)
  • Bump phpstan/phpdoc-parser to ^2.3.3 (#8174)

PHPUnit ๐Ÿงช

New rules and changes from rector-phpunit.

New Rules

  • [CodeQuality] Add AssertClassToThisAssertRector (#707)
 use PHPUnit\Framework\Assert;
 use PHPUnit\Framework\TestCase;

 final class SomeClass extends TestCase
 {
     public function run()
     {
-        Assert::assertEquals('expected', $result);
+        $this->assertEquals('expected', $result);
     }
 }
  • [CodeQuality] Add BareCreateMockAssignToDirectUseRector โ€” inline a single-use createMock() assignment (#708)
 final class SomeTest extends TestCase
 {
     public function test()
     {
-        $someObject = $this->createMock(SomeClass::class);
-        $this->process($someObject);
+        $this->process($this->createMock(SomeClass::class));
     }

     private function process(SomeClass $someObject): void
     {
     }
 }
  • [CodeQuality] Add WillReturnCallbackFallbackToThrowRector โ€” throw on an unexpected extra consecutive call (#710)
         $this->someServiceMock->expects($matcher)
             ->method('run')
             ->willReturnCallback(function () use ($matcher) {
                 if ($matcher->numberOfInvocations() === 1) {
                     return 1;
                 }
+
+                throw new \PHPUnit\Framework\Exception(sprintf('Method should not be called for the %dth time', $matcher->numberOfInvocations()));
             });
  • [CodeQuality] Add RemoveReturnFromVoidMethodMockCallbackRector โ€” type a void mock callback and drop its value return (#711)
         $this->createMock(SomeClass::class)
             ->method('run')
-            ->willReturnCallback(function ($arg) {
+            ->willReturnCallback(function ($arg): void {
                 echo $arg;
-
-                return true;
             });

(SomeClass::run() returns void.)

  • [CodeQuality] Add CallbackSingleAssertToSimplerRector โ€” collapse a with() callback with a sole assertSame() to equalTo() (#714)
         $builder->expects($this->exactly(2))
             ->method('add')
-            ->with($this->callback(function ($type): bool {
-                $this->assertSame(TextType::class, $type);
-
-                return true;
-            }));
+            ->with($this->equalTo(TextType::class));

Improvements

  • [CodeQuality] Skip TestCase suffix classes in RemoveNeverUsedMockPropertyRector (#709)
  • [CodeQuality] Produce void callbacks with bare return in WithCallbackIdenticalToStandaloneAssertsRector (#712)
  • [CodeQuality] Handle return throw and return null in void mock callbacks (#713)

Don't miss a new rector release

NewReleases is sending notifications on new releases.