Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: throw exception on null value or cast it into string #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AlGoore
Copy link
Collaborator

@AlGoore AlGoore commented Nov 24, 2022

Due to new behaviour of PHP 8.1 it is no longer allowed to pass null values into string functions like htmlspecialchars().
So this causes errors while rendering TextTemplate with a message like: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated.
But this type of message is not really useful to find the root of the cause...

This PR changes the method TextTemplate::_parseValueOfTags().
Before a value is passed to the filters it will be checked and a TemplateParsingException is thrown or the value is just set to an empty string otherwise.
It depends on the new constructor's boolean parameter $castNullIntoString which action will be taken.

I'm not sure about the default behaviour...
I personally prefer throwing an exception 'cause there might be something wrong.
Others may prefer the casting variant / overwriting null with an empty string.
@dermatthes please take a look at the changes before merging.

@dermatthes
Copy link
Owner

Isn't it easier to just cast $input variables to string in buildinFilters.php?

TextTemplate::$__DEFAULT_FILTER["_DEFAULT_"] = function ($input) { return htmlspecialchars($input); };

// Raw is only a pseudo-filter. If it is not in the chain of filters, __DEFAULT__ will be appended to the filter
TextTemplate::$__DEFAULT_FILTER["html"] = function ($input) { return htmlspecialchars($input); };
TextTemplate::$__DEFAULT_FILTER["raw"] = function ($input) { return $input; };
TextTemplate::$__DEFAULT_FILTER["singleLine"] = function ($input) { return str_replace("\n", " ", $input); };
TextTemplate::$__DEFAULT_FILTER["inivalue"] = function ($input) { return addslashes(str_replace("\n", " ", $input)); };

To

TextTemplate::$__DEFAULT_FILTER["_DEFAULT_"] = function ($input) { return htmlspecialchars( (string) $input); };
...

Or do you see a special benefit from introducing a dedicated configuration parameter?

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.

None yet

2 participants