PHPackages                             ingenerator/risky-rector-rules - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. ingenerator/risky-rector-rules

ActiveLibrary[Testing &amp; Quality](/categories/testing)

ingenerator/risky-rector-rules
==============================

A selection of Rector refactoring rules that should be used with caution

v1.1.1(2mo ago)71921BSD-3-ClausePHPPHP &gt;=8.4 &lt;8.5CI passing

Since Oct 24Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/ingenerator/risky-rector-rules)[ Packagist](https://packagist.org/packages/ingenerator/risky-rector-rules)[ Docs](https://github.com/ingenerator/risky-rector-rules)[ GitHub Sponsors](https://github.com/sponsors/acoulton)[ RSS](/packages/ingenerator-risky-rector-rules/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (3)Dependencies (6)Versions (5)Used By (0)

This package provides automated refactoring rules for [Rector](https://getrector.com/) that are likely to produce risky updates.

The Rector project are committed to shipping rules that can be trusted to run safely (see [this discussion](https://github.com/rectorphp/rector/discussions/9398#discussioncomment-14582690) and [this blog](https://getrector.com/blog/new-in-rector-015-complete-safe-and-known-type-declarations#content-known-and-safe-types-first-only).

This library exists for the times where you **want** to perform unsafe refactorings. You can use these rules to automate the grunt work of modifying files, so you can instead spend your time carefully reviewing the changes.

When would I use these rules?
-----------------------------

[](#when-would-i-use-these-rules)

- When you are preparing a major breaking release of a library
- When you are beginning a major modernisation of a project

You should also have:

- Comprehensive test coverage.
- A static analyser (e.g. phpstan) and have resolved any issues it identifies.
- Installed and run Rector with the standard / safe rules so that your code is modernised based on concrete types wherever possible.

We **do not** recommend using this package in your project long-term. The rules are intended to be for one-time modernisation - you should install it, configure it, run it &amp; then remove it.

Installing and running the rules
--------------------------------

[](#installing-and-running-the-rules)

Install the package with composer: `composer require ingenerator/risky-rector-rules`.

Ensure you have a clean working directory, and have already run and committed the changes from your existing Rector configuration.

Choose the rules you wish to use and add them to your `rector.php` config file:

```
return RectorConfig::configure()
    // ... your existing Rector config
    ->withRules([
      \Ingenerator\RiskyRectorRules\PhpDocToStrictTypes\AddParamTypeFromPhpDocRector::class,
      \Ingenerator\RiskyRectorRules\PhpDocToStrictTypes\AddPropertyTypeFromPhpDocRector::class,
      \Ingenerator\RiskyRectorRules\PhpDocToStrictTypes\AddReturnTypeFromPhpDocRector::class,
      // We recommend running this rule in a *separate* commit, as it may add void returns to methods
      // where the return type was accidentally undocumented.
      \Ingenerator\RiskyRectorRules\PhpDocToStrictTypes\AddImplicitVoidInterfaceReturnTypeRector::class,
    ]);
```

Run Rector `vendor/bin/rector process`, carefully review the result, and commit the changes.

Rules provided
--------------

[](#rules-provided)

### PhpDocToStrictTypes

[](#phpdoctostricttypes)

This group of rules will add strict types throughout your code based on existing phpdoc tags. Any redundant phpdoc will be removed as part of this process.

If your project is a library, these rules are almost guaranteed to produce breaking changes in your public API.

If you have a reasonable level of phpstan coverage proving that the phpdoc types in your codebase are correct, these changes may be relatively safe. Relatively...!

NamePurposeAddParamTypeFromPhpDocRectorAdds strict types to method parameters based on @param tagsAddPropertyTypeFromPhpDocRectorAdds strict return types to properties based on @var tagsAddReturnTypeFromPhpDocRectorAdds strict return types to methods based on @return tagsAddImplicitVoidInterfaceReturnTypeRectorAdds strict `void` types to interface methods with no documented return#### Only add types to interface methods

[](#only-add-types-to-interface-methods)

By default, `AddParamTypeFromPhpDocRector` and `AddReturnTypeFromPhpDocRector` will add strict types to methods in all classes and interfaces.

You can configure them to only modify interfaces by setting the `interfaces_only` option to `true`:

```
use Ingenerator\RiskyRectorRules\PhpDocToStrictTypes\AddMethodTypeConfig;

return RectorConfig::configure()
    ->withConfiguredRule(AddParamTypeFromPhpDocRector::class, [AddMethodTypeConfig::INTERFACES_ONLY => true])
    ->withConfiguredRule(AddReturnTypeFromPhpDocRector::class, [AddMethodTypeConfig::INTERFACES_ONLY => true]);
```

### AddStrictTypes

[](#addstricttypes)

These rules add strict types based on runtime type information.

#### AddParamTypeBasedOnParentClassMethodRector

[](#addparamtypebasedonparentclassmethodrector)

This rule adds strict types to method parameters based on the type of the parameter in a parent class or interface method. Types will not be added or changed if the parameter is already typed.

This is similar to Rector's built-in `AddReturnTypeDeclarationBasedOnParentClassMethodRector` BUT it is risky. This is because classes are allowed to accept a wider type for method parameters than the parent class allows. Only run this rule if you want to enforce that child methods can only accept types that the parent class allows.

For example, this code is both valid and works:

```
class A {
  public function print(string $a) {
    echo $a;
  }
}

class B extends A {
{
  public function print($a) {
    echo $a ?? '{null}'
  }
}

$b = new B();
$b->print(null);
```

But this rule will change it to the following, which will break at runtime:

```
class A {
  public function print(string $a) {
    echo $a;
  }
}

class B extends A {
{
  public function print(string $a) {
    echo $a ?? '{null}'
  }
}

$b = new B();
$b->print(null);
```

Therefore you should only use this rule if you *want* to lock down the types that a child class can accept, for example as part of a breaking release of a PHP library.

Support this library
--------------------

[](#support-this-library)

If you find this library useful, [please consider sponsoring my Open Source work](https://github.com/sponsors/acoulton). One-time and recurring sponsorship helps me to spend time working on Open Source projects.

Credits
-------

[](#credits)

The initial implementation of the AddParamTypeFromPhpDocRector was based on [a contribution by Michael Strelan to the Drupal project](https://git.drupalcode.org/project/drupal/-/blob/cf322b485db62ec80680bd7148c576f20877ab36/core/lib/Drupal/Core/Rector/AddParamTypeFromPhpDocRector.php)from March 2024.

At the time, that contribution had not been accepted and the implementation was no longer functional against current Rector versions. Additionally, their implementation differed in a number of respects from my desired requirements.

The implementation shipped in this library has therefore diverged quite significantly from the original inspiration, but Michael's work provided a useful starting point and so I'm very happy to credit it.

Contributing
------------

[](#contributing)

Contributions - whether changes or new rules - are very welcome. However, we strongly recommend opening an issue to discuss your idea before working on any code. This will avoid wasting any time if your proposal does not fit with our vision for the library.

If you do decide to contribute, you should follow our coding style and add tests for every change.

Licence
-------

[](#licence)

Licensed under the [BSD-3-Clause Licence](LICENCE.md)

###  Health Score

46

—

FairBetter than 92% of packages

Maintenance86

Actively maintained with recent releases

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~84 days

Total

3

Last Release

75d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f6ebcc608a86cec9a3ce7a4932994e2b89fabbfcdde69aff43e8139f34e07a5e?d=identicon)[acoulton](/maintainers/acoulton)

---

Top Contributors

[![acoulton](https://avatars.githubusercontent.com/u/416566?v=4)](https://github.com/acoulton "acoulton (27 commits)")[![BinaryKitten](https://avatars.githubusercontent.com/u/67553?v=4)](https://github.com/BinaryKitten "BinaryKitten (2 commits)")

---

Tags

refactoringrector

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/ingenerator-risky-rector-rules/health.svg)

```
[![Health](https://phpackages.com/badges/ingenerator-risky-rector-rules/health.svg)](https://phpackages.com/packages/ingenerator-risky-rector-rules)
```

###  Alternatives

[ssch/typo3-rector

Instant fixes for your TYPO3 PHP code by using Rector.

2603.0M382](/packages/ssch-typo3-rector)[mrpunyapal/rector-pest

Rector upgrade rules for Pest - refactoring and best practices for Pest testing framework

6555.5k50](/packages/mrpunyapal-rector-pest)[tempest/framework

The PHP framework that gets out of your way.

2.2k31.1k12](/packages/tempest-framework)[denzyl/phanalist

Performant static analyzer for PHP, which is extremely easy to use. It helps you catch common mistakes in your PHP code.

15645.4k](/packages/denzyl-phanalist)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
