[CodeQuality] Add AddIntersectionParamToMockObjectParamRector - #755
Merged
Conversation
Add a MockObject intersection @param docblock to a private test method param typed as a bare MockObject, based on the mock passed at the call sites within the same class. This complements ExpectsParamToMockObjectRector, that narrows such a param to a native MockObject type and thereby loses the mocked class for static analysis. The mocked class is resolved from the $this->createMock(Some::class) call the passed variable is assigned from. The docblock is only added when every call site passes a mock of the very same class, so the type is never guessed. Register it in the composer-based set, bonded to phpunit/phpunit >=11.0 to match its stub/mock siblings, and in the mock-to-stub set.
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.
Private test methods that take a mock end up with a bare
MockObjectparam type — either handwritten, or produced byExpectsParamToMockObjectRector. The mocked class is then lost for static analysis, which is what forces@phpstan-ignore-next-lineon every call on that param.This rule puts the class back into an intersection
@param, resolved from the mock actually passed at the call sites in the same class.final class SomeTest extends TestCase { public function testBuildForm(): void { $builder = $this->createMock(FormBuilderInterface::class); $eventListener = $this->getEventListenerFromBuildForm($type, $builder); } + /** + * @param \FormBuilderInterface&\PHPUnit\Framework\MockObject\MockObject $builder + */ private function getEventListenerFromBuildForm(SomeType $type, MockObject $builder): callable { $builder->expects($this->once()) ->method('addEventListener'); } }How the type is resolved
Only private methods are handled, as those can only be called from the very same class, so all call sites are visible.
The mocked class comes from the
$this->createMock(Some::class)/self::createMock(Some::class)assignment of the passed variable, or from acreateMock()call passed inline as an arg.Skipped when the type would be a guess:
createStub()source, that is aStub, not aMockObject@paramtag for that param already exists, to not contradict itSets
Registered in the
composer-basedset next toExpectsParamToMockObjectRector, bonded tophpunit/phpunit >=11.0to match its stub/mock siblings, and in thephpunit-mock-to-stubset.Note on the intersection order: this prints
\Some&\MockObject, while the existingAddIntersectionVarToMockObjectPropertyRectorprints\MockObject&\Some. Happy to flip either one for consistency.