Problem/Motivation

We currently throw a runtime exception of Twig\Error\RuntimeError when our Twig filter's target isn't what we expect, this is what Twig core does.

But we aren't checking the validity of parameters in any of our Twig filters or functions. The latest version of Drupal core uses PHP's assert to check they are the expected type.

Proposed resolution

Start checking params with assert.

Issue fork components-3209487

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

JohnAlbin created an issue. See original summary.

johnalbin’s picture

Title: Check filter/function parameters using assert » Check Twig filter/function parameters using assert
Issue summary: View changes
johnalbin’s picture

PHP type hinting is preferred over assert() and this only leaves one use parameter needing an assert, the first positional parameter of the template function.

According to https://github.com/sebastianbergmann/phpunit/issues/1897, its impossible to write a test to check if the assert get triggered because if you write a test case to do that, the PHP assert will fail the test even if you are expecting assert to be triggered.

johnalbin’s picture

Status: Active » Needs review
johnalbin’s picture

Assigned: johnalbin » Unassigned

  • JohnAlbin committed 2a603b7 on 3.x
    Issue #3209487 by JohnAlbin: Check Twig filter/function parameters using...
johnalbin’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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