[CodeQuality] Fix AddInstanceofAssertForNullableArgumentRector duplicate assertInstanceOf inside traits - #773
Merged
Conversation
…ate assertInstanceOf on every run inside traits
Member
Author
|
Let's ship it 👍 |
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.
Fixes rectorphp/rector#9863
Problem
Inside a test trait,
AddInstanceofAssertForNullableArgumentRectoris not idempotent. Everyrector processrun appends one more identical$this->assertInstanceOf(...)line, forever.The rule's only cross-run guard was implicit: the inserted
$this->assertInstanceOf()narrows the variable's type, soSimpleTypeAnalyzer::isNullableType()stops matching on the next run. In a class extendingTestCasethat holds. In a trait$thisis not aPHPUnit\Framework\Assert, so the assert never narrows, the variable stays nullable, and a fresh assert is appended each run.$someObject = $someFactory->create(); $this->assertInstanceOf(SomeClass::class, $someObject); +$this->assertInstanceOf(SomeClass::class, $someObject); // added again every run $someFactory->process($someObject);Fix
Stop depending on type narrowing for idempotency. While scanning statements, detect a pre-existing
assertInstanceOf(<Type>::class, $variable)and drop that variable from the nullable collection, so no second assert is spliced in. Works in both classes and traits, and hardens the class case when the PHPUnit type-specifying extension is unavailable.Tests
skip_already_asserted_in_trait.php.inc— trait already carrying the assert stays unchanged (fails before the fix: a duplicate assert is added).nullable_arg_in_trait.php.inc— the rule still fires once in a trait.