Skip to content

Check #lazy_builder arguments against the callback's parameters - #1101

Open
hanming5 wants to merge 1 commit into
mglaman:mainfrom
hanming5:1093-lazy-builder-arguments
Open

hanming5 wants to merge 1 commit into
mglaman:mainfrom
hanming5:1093-lazy-builder-arguments

Conversation

@hanming5

Copy link
Copy Markdown
Contributor

Fixes #1093

LazyBuilderRule now checks the #lazy_builder arguments against the callback method's parameters when the arguments are a constant array. The @todo in RenderCallbackRule is removed.

What it reports

  • lazyBuilder.argumentCount
    • Fewer arguments than the method requires. Drupal throws an ArgumentCountError when it renders the placeholder.
    • More arguments than a non-variadic method accepts. PHP ignores them, and the message says so.
  • lazyBuilder.argumentType: an argument the parameter type doesn't accept. Drupal throws a TypeError when it renders the placeholder.

Arguments are matched by position, because DoTrustedCallbackTrait calls array_values() first. Mixed arguments, untyped parameters, and methods with more than one variant are skipped. A method that calls func_get_args() counts as variadic, as it does in PHPStan, so extra arguments to it aren't reported. Arguments already reported by lazyBuilder.nonScalarArgument aren't reported again.

Differences from the spec

  1. Coercion. $parameterType->accepts($argumentType, false) alone doesn't model PHP's scalar coercion. PHPStan's IntegerType rejects '5' even with $strictTypes = false. The rule uses $parameterType->accepts($argumentType->toCoercedArgumentType(false), false) and only reports a no(). I compared the result with PHP's behaviour for int, string, bool and float parameters against '5', 'teaser', 1, 1.5, true and null. It reports exactly the combinations that throw a TypeError.
  2. Native types. The type check uses the parameter's native type, not the PHPDoc type, because only the native type causes the TypeError. For example, @param positive-int $node_id with [0] isn't reported.

Tests

tests/src/Rules/data/lazy-builder.php covers 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, NULL for an int, a variadic argument of the wrong type, a nullable argument, mixed arguments, a PHPDoc-only type, and a method that reads its arguments with func_get_args().

buildUntyped() in the fixture got three optional parameters, so the existing untyped_callback case stays valid. The existing non_scalar_arguments case now also reports that build() 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.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LazyBuilderRule: check #lazy_builder arguments against the callback's parameters

1 participant