[composer-based] Bond version-specific rules to composer package constraints - #754
Merged
Merged
Conversation
TomasVotruba
force-pushed
the
composer-constraints-for-version-bound-rules
branch
from
August 1, 2026 08:11
d785a78 to
af6969b
Compare
Add ComposerPackageConstraintInterface to rules that emit APIs which only exist since a specific PHPUnit version, with each version verified against the phpunit/phpunit git history: * AllowMockObjectsWhereParentClassRector, AllowMockObjectsForDataProviderRector and AllowMockObjectsWithoutExpectationsAttributeRector >=12.5.2, as the #[AllowMockObjectsWithoutExpectations] attribute was added there * RemoveOverrideFinalConstructTestCaseRector >=12.0.3, as TestCase::__construct() was declared final there * InlineStubPropertyToCreateStubMethodCallRector, SingleMockPropertyTypeRector and ChangeMockObjectReturnUnionToIntersectionRector >=11.0, to match their already bonded stub/mock siblings Register the method renames of the generic RenameMethodRector in the same way, each bound to the PHPUnit version that added the target method: * setMethods() => onlyMethods() since 8.3 * expectExceptionMessageRegExp() => expectExceptionMessageMatches() since 8.4 * the assertRegExp(), assertFileNotExists() and assert*Not*() family since 9.1 * getInvocationCount() => numberOfInvocations() since 10.0 * assertObjectHasAttribute() => assertObjectHasProperty() since 10.1 * expectExceptionMessage() => expectExceptionMessageIsOrContains() since 13.2 Everything lands in the composer-based set, so it applies to any project with a matching PHPUnit version. The per-version sets keep their current registrations, so their behavior stays backward compatible.
TomasVotruba
force-pushed
the
composer-constraints-for-version-bound-rules
branch
from
August 1, 2026 08:18
af6969b to
89f684a
Compare
This was referenced Aug 1, 2026
[composer-based] Include all configured rules in the composer-based set
rectorphp/rector-symfony#980
Merged
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some rules and configurations emit APIs that only exist since a specific PHPUnit version, but carry no version bond. Applied to an older PHPUnit, they produce code that fatals.
Every version below was resolved from the
sebastianbergmann/phpunitgit history (git tag --contains <commit>), not from memory.Rules
AllowMockObjectsWhereParentClassRector>=12.5.2#[AllowMockObjectsWithoutExpectations]does not exist yet (24c208d)AllowMockObjectsForDataProviderRector>=12.5.2AllowMockObjectsWithoutExpectationsAttributeRector>=12.5.2RemoveOverrideFinalConstructTestCaseRector>=12.0.3TestCase::__construct()is only final since then (3263f4c)InlineStubPropertyToCreateStubMethodCallRector>=11.0SingleMockPropertyTypeRector>=11.0ChangeMockObjectReturnUnionToIntersectionRector>=11.0use Symfony\Component\Form\Test\TypeTestCase; +#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations] final class SomeTest extends TypeTestCase { }On PHPUnit 12.5.1 and below that attribute class does not exist, so the test class fatals. With the bond, the rule is skipped instead of producing broken code.
Method renames
The generic
RenameMethodRectorconfigurations get the same treatment, each bound to the version that added the target method:setMethods()→onlyMethods()expectExceptionMessageRegExp()→expectExceptionMessageMatches()assertRegExp(),assertNotRegExp(),assertFileNotExists(),assertFileNotIsReadable(),assertDirectoryNotExists(),assertDirectoryNotIsReadable(),assertDirectoryNotIsWritable(),assertNotIsReadable(),assertNotIsWritable()getInvocationCount()→numberOfInvocations()assertObjectHasAttribute()→assertObjectHasProperty()expectExceptionMessage()→expectExceptionMessageIsOrContains()Two versions worth calling out, as they are a minor later than one would assume:
assertObjectHasProperty()is 10.1, not 10.0, andexpectExceptionMessageIsOrContains()is 13.2. The latter is the sharpest edge - rewritingexpectExceptionMessage()on a project still on PHPUnit 11 or 12 points every call at a method that does not exist there.Set registration
Everything is registered in
composer-based.php, so it applies to any project whose installed PHPUnit matches, not only through the single upgrade set that used to carry it.The per-version sets keep their current registrations untouched, so nothing changes for anyone already using them - a rule registered by both a version set and the composer-based set is tagged only once by
RectorConfig::rule(), and rule configurations are merged.Two
tests/ComposerBasedfixtures cover the constructor rule and the bounded renames arriving through the composer-based set.