Repository navigation
Conversation
Drupal calls a #lazy_builder callback with call_user_func_array() and the arguments' values, so too few arguments throw an ArgumentCountError and an argument the parameter type can't take throws a TypeError when the placeholder is rendered. LazyBuilderRule now checks a constant arguments array against the callback method's single variant: - lazyBuilder.argumentCount: fewer arguments than required, or more than a non-variadic method accepts. - lazyBuilder.argumentType: an argument the native parameter type doesn't accept after PHP's non-strict coercion, so '5' is allowed for an int parameter and 'teaser' is not. Arguments are matched by position, as Drupal calls array_values() first. Removes the matching @todo from RenderCallbackRule. Fixes mglaman#1093
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 #1093
LazyBuilderRulenow checks the#lazy_builderarguments against the callback method's parameters when the arguments are a constant array. The@todoinRenderCallbackRuleis removed.What it reports
lazyBuilder.argumentCountArgumentCountErrorwhen it renders the placeholder.lazyBuilder.argumentType: an argument the parameter type doesn't accept. Drupal throws aTypeErrorwhen it renders the placeholder.Arguments are matched by position, because
DoTrustedCallbackTraitcallsarray_values()first. Mixed arguments, untyped parameters, and methods with more than one variant are skipped. A method that callsfunc_get_args()counts as variadic, as it does in PHPStan, so extra arguments to it aren't reported. Arguments already reported bylazyBuilder.nonScalarArgumentaren't reported again.Differences from the spec
$parameterType->accepts($argumentType, false)alone doesn't model PHP's scalar coercion. PHPStan'sIntegerTyperejects'5'even with$strictTypes = false. The rule uses$parameterType->accepts($argumentType->toCoercedArgumentType(false), false)and only reports ano(). I compared the result with PHP's behaviour forint,string,boolandfloatparameters against'5','teaser',1,1.5,trueandnull. It reports exactly the combinations that throw aTypeError.TypeError. For example,@param positive-int $node_idwith[0]isn't reported.Tests
tests/src/Rules/data/lazy-builder.phpcovers the cases from the issue: too few, too many, extra arguments for a variadic method, a non-coercible string, a numeric string, and string keys matched by position. It also covers optional parameters,NULLfor anint, a variadic argument of the wrong type, a nullable argument, mixed arguments, a PHPDoc-only type, and a method that reads its arguments withfunc_get_args().buildUntyped()in the fixture got three optional parameters, so the existinguntyped_callbackcase stays valid. The existingnon_scalar_argumentscase now also reports thatbuild()accepts 1 argument but gets 4.I also ran the rule on the 20 non-test Drupal core files that contain
'#lazy_builder' =>. It reports nothing there, before or after this change.