Problem/Motivation

Part of #3404246: [META] Fix strict type errors detected by phpstan

Steps to reproduce

Add checkFunctionArgumentTypes: true to parameters section of phpstan.neon.dist

$ ./vendor/bin/phpstan analyse -c core/phpstan.neon.dist --error-format=raw | grep -F "Interface, PHPUnit\Framework\MockObject\MockObject given"

There are 17 violations (attached)

Proposed resolution

The return type of TestCase::createMock is typehinted as MockObject&RealInstanceType, where RealInstanceType is the class passed to the method.

We should use the same format for properties that are set via this method, for example:

@var \PHPUnit\Framework\MockObject\MockObject&\Drupal\Core\Lock\LockBackendInterface

Note, there are some adjacent properties that are union types instead of intersection types. We need the intersection so we can call methods from MockObject (like expects) while also calling methods from the Interface.

There is also one case where we are passing the MockBuilder instead of the MockObject that should be fixed too.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3558167

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mstrelan created an issue. See original summary.

mstrelan’s picture

Status: Active » Needs review
mondrake’s picture

Commented inline.

smustgrave’s picture

Since you're fixing them shouldn't there be an update to the phpstan file so they don't come back?

mstrelan’s picture

@smustgrave ideally yes, there is a parameter listed in the parent meta, but since it's split in to many child issues we need to fix them all before updating the config.

dcam’s picture

Status: Needs review » Reviewed & tested by the community

I ran PHPStan as shown in the issue summary on my local and got the same 17 errors. After applying the MR all 17 errors were gone. I read over all the changes and didn't see anything weird. For the record, core/modules/image/tests/src/Unit/ImageStyleTest.php, which is at the top of the MR changes list, is the file where we're fixing the MockBuilder mentioned in the IS.

It looks like we're also standardizing the docblocks and type hints of all properties in the classes being edited. I think that's a good thing to help prevent something like this from happening again.

With all that said, this looks good to me.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

mondrake’s picture

Just some thoughts here:

1) Given #3561671: [meta] Refactor tests to use stubs instead of mocks where mocks do not configure expectations, I think it should be checked here whether we are really needing a mock or we can use a stub instead - that will change the typehint from FooInterface&MockObject to FooInterface&Stub. We should directionally use stubs when we are not setting expectations on the number of times a method is called.

2) Just my personal preference here, so IIWII: I think the typehint is more readable when the original class of the mock comes first. In other words, prefer FooInterface&MockObject over MockObject&FooInterface.

mstrelan’s picture

Status: Needs work » Needs review

Rebased and address #9.2. Not sure about #9.1, it's not really what the scope of this issue is about, and there are plenty of other things that could be fixed or improved at the same time, but I'm trying to keep the scope down. There should not be BC concerns about changing these from MockObject to Stub later on.

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

LGTM

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new1.43 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

Post-bot-rebellion rebase

longwave’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Backported to 11.x to keep tests in sync, not worth going further back.

Committed and pushed c325b55a02c to main and 5fc1b187915 to 11.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • longwave committed 5fc1b187 on 11.x
    test: #3558167 Fix "expects FooInterface, MockObject given" phpstan...

  • longwave committed c325b55a on main
    test: #3558167 Fix "expects FooInterface, MockObject given" phpstan...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.