Problem/Motivation
If you pass CallableResolver an array and it's not callable, it throws an exception like this:
> throw new \InvalidArgumentException(sprintf('The callable definition provided "[%s]" is not a valid callable.', implode(",", $definition)));
However, the check for is_callable() can fail and lead you to this path if you pass in `[$object, 'method']` where $method does not exist.
For example,
>getCallableFromDefinition($my_object, 'metmethodDoesNotExist']);
In this case, the implode() causes an error string. The exception is not seen by the developer, but a PHP error instead.
Steps to reproduce
- On Drupal 11.x or main, run:
\Drupal::service('callable_resolver')->getCallableFromDefinition([new \stdClass(), 'methodDoesNotExist']); - Expected: an
InvalidArgumentExceptionwith the messageThe callable definition provided "[...]" is not a valid callable. - Actual: PHP
Error"Object of class stdClass could not be converted to string", thrown byimplode()while the exception message is built.
Proposed resolution
Convert each part of the array definition to a string before implode(). Scalars are cast to string. Objects and other types are replaced by get_debug_type(). The message for the example above becomes:
The callable definition provided "[stdClass,methodDoesNotExist]" is not a valid callable.
Remaining tasks
None. MR !17360 has the fix and a test, and it is reviewed.
Introduced terminology
None.
API changes
None. Only the exception message changes.
Data model changes
None.
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 3628789_issue_after.png | 271.95 KB | nisha_j |
| #4 | 3628789_issue_before.png | 260.09 KB | nisha_j |
Issue fork drupal-3628789
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:
- 3628789-callableresolver-exception-doesnt
changes, plain diff MR !17360
Comments
Comment #4
nisha_j commentedReviewed and tested MR !17360 on current main (PHP 8.5).
Test-only run (only the test change from the MR applied, without the fix):
Failed asserting that exception of type "Error" matches expected exception "InvalidArgumentException". Message was: "Object of class stdClass could not be converted to string" at core/lib/Drupal/Core/Utility/CallableResolver.php:69
Tests: 10, Assertions: 30, Failures: 1.
So the new test case reproduces the bug described in the summary.
With the full MR: CallableResolverTest passes (10 tests, 31 assertions), including the new "Missing method on object, array notation" case.
Manual test on a site:
Also reproduced on a Drupal 12.0.0-beta1 site with a small test route that calls:
\Drupal::service('callable_resolver')->getCallableFromDefinition([new \stdClass(), 'methodDoesNotExist']);
- Before: Error: Object of class stdClass could not be converted to string in implode() (line 69 of core/lib/Drupal/Core/Utility/CallableResolver.php)
- After applying MR !17360: InvalidArgumentException: The callable definition provided "[stdClass,methodDoesNotExist]" is not a valid callable.
Screenshots of both are attached.
Other checks:
- The MR applies cleanly to current main.
- phpcs (core/phpcs.xml.dist) and phpstan (core/phpstan.neon.dist) report no errors on the changed files.
- I didn't find the same implode() pattern anywhere else in core/lib.
Marking RTBC.
Comment #5
nisha_j commentedComment #6
nisha_j commentedUpdated issue summary
Comment #7
nisha_j commentedComment #8
nicxvan commentedComment #9
alexpottCommitted and pushed 59d522ffb63 to main and 4ca1eee3e1c to 12.0.x and e439956fb5e to 11.x and 02799070b1c to 11.5.x. Thanks!
Backported this to 11.5.x as a low-risk bug fix.