Problem/Motivation
Writing valid Rules expressions for reaction rules and components is difficult for new Rules developers, and some expressions that execute correctly have defects that will prevent them from being saved as configuration entities (an example: #2610574: Nested expressions throw exceptions when saved as reaction rule). When these kinds of data problems get into expressions, it can be difficult to find the source of the problem, even when using a source debugger.
While documentation and good examples will help here, it's also good if bad code calls attention to itself. In #2610574: Nested expressions throw exceptions when saved as reaction rule, it turned out that calls that should take a string plugin ID like RuleInterface::addCondition() and RuleInterface::addAction() were accepting expression objects. In these cases, the right API to use is ExpressionContainerInterface::addExpressionObject(). The code would execute correctly, but in both cases would throw an exception only when the developer tried to save the expressions as configuration entities.
Proposed resolution
Throw exceptions as close to the point of the errors as possible for PHP 5.x, and consider using scalar hinting and assertions once PHP 7 becomes common, to the extent that this kind of code is compatible with PHP 5.5.
Remaining tasks
User interface changes
API changes
No API changes for now, simply make sure that parameter types are better enforced.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 2625986-4-action-condition-ids.patch | 1.13 KB | tr |
Comments
Comment #2
Torenware commentedInitial version posted as PR https://github.com/fago/rules/pull/311.
Comment #3
dasjoUpdating status based on the comments made on the PR
Comment #4
tr commentedAddressed the comment in the PR by removing the t().
Re-rolled against current HEAD.
Please follow up here in the issue queue and NOT on github.
I ran across this issue because I just did some work with the ExpressionManager, and although this is something that can only happen when creating Rules programmatically, and only when the developer passes in a wrong variable type, it does seem that we can make the code a little more robust by enforcing the type. It pains me that we still have to use isset(), empty(), is_null(), is_string() etc. in this day and age, but that's our fault for choosing PHP I guess ... When Drupal decides to require PHP 7 I'll be the first to add scalar type hinting and return type declarations to the entire Rules code base so we can eliminate structures like this.
Comment #5
tr commentedDrupal 8.7.0 is scheduled for release on May 1, 2019. With this release, Drupal will officially stop supporting PHP 5.
This means that after 1 May, we can add scalar type hints in places like this, and that will solve this problem without the need of any is_string() checks or extra code.
So I'm not going to commit this patch. Rather, I will add the type hinting as soon as 8.7.0 is released.