Problem/Motivation
\Drupal\Tests\user\Traits\UserCreationTrait::createUser returns User|false. If you use phpstan level 7 or higher that means you need to always check if it was successful or not. It would be better to simply throw an exception instead.
In fact, it can never even return false. The first place it should return false is if it failed to create a role, but there is an assertion in the createRole method that will fail before we can return false. The second place is if $valid_user is FALSE then assertTrue will throw an exception and we'll never reach the return.
Steps to reproduce
Proposed resolution
Remove |false from the signature of the following:
- UserCreationTrait::createUser
- UserCreationTrait::createAdminRole
- UserCreationTrait::createRole
- WorkspacesMediaLibraryIntegrationTest::drupalCreateUser
Remove any unreachable returns
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3560202
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:
- 3560202-stop-returning-false
changes, plain diff MR !13982
Comments
Comment #2
longwaveAdding the BC layer seems like a lot of busy work for not much gain, as this is tests only and in 99% of cases (maybe 100% in contrib/custom tests?) the creation will be expected to succeed then perhaps we just start throwing the exception?
Comment #3
mstrelan commentedSounds good to me.
Comment #4
ritarshi_chakraborty commentedWorking on it.
Comment #6
ritarshi_chakraborty commentedHey @longwave, I implemented the changes you requested but tests are failing. Shall we go with the previous logic?
Comment #7
mstrelan commentedcreateRole returns false not null, assertNotNull will pass if it returns false
Comment #8
longwavecreateRole()also will never fail, because this assertion will stop the test if it does:Reclassifying this as a bug, as far as I can see it's never worked since the migration from SimpleTest to PHPUnit.
I wonder if there is a PHPStan extension that can catch dead code following assertions?
Comment #9
longwaveComment #10
longwaveThe IS is out of date compared to the actual problem.
Comment #11
mstrelan commentedUpdated the IS. Left one comment on the MR.
Comment #12
mstrelan commentedDidn't mean to change the category
Comment #13
longwaveThanks for the review, fixed that type in the docs.
Comment #14
mstrelan commentedThe changes look good to me.
I suspect there are a few projects that are handling this already that may be affected by this, but I haven't thought through exactly how this works with method signatures in traits, base classes etc. A couple links below:
https://git.drupalcode.org/project/autocomplete_mixed_matching/-/blob/1....
https://git.drupalcode.org/project/content_entity_base/-/blob/8.x-1.x/te...
Comment #15
smustgrave commentedSeems pretty easy enough. I did a search in core to see if there were other instances and all seem good.
I looked at the 2 examples in #14 and don't believe this change will break this? autcomplete_mixed_matching even has an assert that it's not false, so guess that would be dead code for them.
Comment #20
catchCommitted/pushed to main and 11.x, thanks!