iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌿

Adding void Return Types with Rector

に公開

Introduction

While void is specified when there is no return value, manually adding it to every single method that lacks it is quite tedious. So, I checked if Rector has a rule to apply it in bulk, and...

I found it!! Surprisingly, there were two!!

  • AddVoidReturnTypeWhereNoReturnRector
  • AddTestsVoidReturnTypeWhereNoReturnRector

So, in this article, I decided to try out both.

Environment

  • PHP 8.4.4

Rules

First, let's examine the differences between the two rules.

I wonder if AddTestsVoidReturnTypeWhereNoReturnRector is meant to be used when test classes and non-test classes coexist in the same directory (under tests) and you want to narrow the target down only to the test classes?

In this case, the goal of adding void can be achieved by using AddVoidReturnTypeWhereNoReturnRector, so using just that one shouldn't be a problem.

Trying it out

From here, I will try out the process up to the point where the fix is actually applied using Rector.

Installation

Install it following the steps described in the README.

composer require rector/rector --dev

After installation, try running the command below to check the version.

vendor/bin/rector --version

% ./vendor/bin/rector --version
Rector 2.0.9

The version at the time of writing this article is 2.0.9.

Initializing Rector

First, run the vendor/bin/rector command to create the rector.php configuration file.
When you run it, you'll be told that rector.php is missing, so type yes to create it.

% ./vendor/bin/rector

 No "rector.php" config found. Should we generate it for you? [yes]:
 > yes


 [OK] The config is added now. Re-run command to make Rector do the work!

Update the created rector.php as follows.

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()
    ->withPaths([
        __DIR__ . '/src',
        __DIR__ . '/tests',
    ])
    ->withRules([
        AddVoidReturnTypeWhereNoReturnRector::class,
    ]);

Execution

I will try running it against the following sample code I've prepared.

src/A.php
<?php

class A {
    public function test()
    {
        return;
    }
}
tests/ATest.php
<?php

class ATest extends \PHPUnit\Framework\TestCase
{
    public function test()
    {
        $this->assertSame(1, 1);
    }
}

Use the following command for execution.

vendor/bin/rector process --dry-run

First, I'll check if the fixes will be applied by adding --dry-run.
The directories to be inspected are already specified in rector.php, so they are omitted from the command.

% vendor/bin/rector process --dry-run
 2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
2 files with changes
====================

1) src/A.php:0

    ---------- begin diff ----------
@@ @@
 <?php

 class A {
-    public function test()
+    public function test(): void
     {
         return;
     }
    ----------- end diff -----------

Applied rules:
 * AddVoidReturnTypeWhereNoReturnRector


2) tests/ATest.php:1

    ---------- begin diff ----------
@@ @@

 class ATest extends \PHPUnit\Framework\TestCase
 {
-    public function test()
+    public function test(): void
     {
         $this->assertSame(1, 1);
     }
    ----------- end diff -----------

Applied rules:
 * AddVoidReturnTypeWhereNoReturnRector

 [OK] 2 files would have been changed (dry-run) by Rector

If the results are as expected, run it without --dry-run to apply the fixes.

% vendor/bin/rector process
 2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
2 files with changes
====================

1) src/A.php:0

    ---------- begin diff ----------
@@ @@
 <?php

 class A {
-    public function test()
+    public function test(): void
     {
         return;
     }
    ----------- end diff -----------

Applied rules:
 * AddVoidReturnTypeWhereNoReturnRector


2) tests/ATest.php:1

    ---------- begin diff ----------
@@ @@

 class ATest extends \PHPUnit\Framework\TestCase
 {
-    public function test()
+    public function test(): void
     {
         $this->assertSame(1, 1);
     }
    ----------- end diff -----------

Applied rules:
 * AddVoidReturnTypeWhereNoReturnRector

 [OK] 2 files have been changed by Rector

Once the fixes are applied, running vendor/bin/rector process again will show no targets remaining.

% vendor/bin/rector process
 2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 [OK] Rector is done!

This looks good; it's fixed, and the methods now have void attached.

Summary

By using Rector, you can handle updates in bulk without having to perform manual tasks one by one.
In this article, I tried adding void, but other rules can also set argument types or return types based on the content of PHPDoc, so it might be worth exploring those as well.

GitHubで編集を提案

Discussion