Closes #5451: Replace @var annotations with assert()#5464
Open
Closes #5451: Replace @var annotations with assert()#5464
Conversation
Contributor
|
Tugboat has finished building the preview for this pull request! Link: Dashboard: |
Member
|
I think my only concern would be exceptions / warnings getting triggered by |
joeparsons
approved these changes
Apr 3, 2026
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.
Description
#5451 : I can't find exactly what about PHPStan 2.1.45 caused the errors from the issue:
Even though AZAccordionItem, AZRankingItem, and AZCardItem all correctly extend FieldItemBase, it seems PHPStan 2.x couldn't confirm the relationship at analysis time. Either that or it became stricter against inline @var's OR on usage of its lie detector.
But in general it seems PHPStan 2.x discourages use of inline @var's:
And here:
It also says:
Similar (though maybe not exact) issue here says they solved it using assert() statements: https://drupal.stackexchange.com/questions/321256/phpstan-2-vartag-type-phpdoc-tag-var-with-type-userbundle-is-not-subtype-of-ty#:~:text=Sorted%20by:,asserted%20or%20checked%20with%20instanceof%20.
So this PR changes them to assert() statements, but this means we need to make sure the paragraph types in question:
az_card,az_ranking,az_accordion, andaz_publicationdon't break at runtime. I've done the tests below on these paragraph types and have found no errors so far.For this:
assert($item instanceof AZEntityRoleReferenceItem);resolves the undefined property error because AZEntityRoleReferenceItem extends EntityReferenceItem and the phpstan-drupal stub forEntityReferenceItemdeclares@property ?T $entity: https://github.com/mglaman/phpstan-drupal/blob/main/stubs/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.stubRelease notes
Related issues
How to test
Test by Running PHPStan
lando phpstanorddev phpstanALSO test
az_accordion,az_card,az_ranking,az_publication:az_accordion,az_card,az_ranking— all go through the same scenarios in their respective formatters:az_publication(AZEntityRolelnlineFormComplex) — the assert is in prepareFormState, which runs when the widget initializes:How to check for failures:
If an assert () fails you'll see a PHP fatal AssertionError in the Drupal error log (/admin/reports/dblog). If pages render and forms save normally, the assertions are passing as expected.
Types of changes
Arizona Quickstart (install profile, custom modules, custom theme)
Drupal core
Drupal contrib projects
Checklist