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

  1. On Drupal 11.x or main, run:
    \Drupal::service('callable_resolver')->getCallableFromDefinition([new \stdClass(), 'methodDoesNotExist']);
  2. Expected: an InvalidArgumentException with the message The callable definition provided "[...]" is not a valid callable.
  3. Actual: PHP Error "Object of class stdClass could not be converted to string", thrown by implode() 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

CommentFileSizeAuthor
#4 3628789_issue_after.png271.95 KBnisha_j
#4 3628789_issue_before.png260.09 KBnisha_j

Issue fork drupal-3628789

Command icon 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:

Comments

joachim created an issue. See original summary.

burcu.sogut@drupart.com.tr made their first commit to this issue’s fork.

nisha_j’s picture

StatusFileSize
new260.09 KB
new271.95 KB

Reviewed 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.

nisha_j’s picture

Status: Active » Reviewed & tested by the community
nisha_j’s picture

Issue summary: View changes

Updated issue summary

nisha_j’s picture

Issue summary: View changes
nicxvan’s picture

Issue summary: View changes
alexpott’s picture

Version: main » 11.5.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 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.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • alexpott committed 02799070 on 11.5.x
    fix: #3628789 CallableResolver exception doesn't properly handle a bad...

  • alexpott committed e439956f on 11.x
    fix: #3628789 CallableResolver exception doesn't properly handle a bad...

  • alexpott committed 4ca1eee3 on 12.0.x
    fix: #3628789 CallableResolver exception doesn't properly handle a bad...

  • alexpott committed 59d522ff on main
    fix: #3628789 CallableResolver exception doesn't properly handle a bad...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.