bithost-gmbh / pdfviewhelpers

TYPO3 CMS extension that provides various Fluid ViewHelpers to generate PDF documents.
GNU General Public License v3.0
44 stars 19 forks source link

PHP 8 errors in AbstractTextViewHelper when text is empty and trim or removeDoubleWhitespace is set to 1 #222

Closed rk-mxp closed 9 months ago

rk-mxp commented 1 year ago

Describe the bug The AbstractTextViewHelper throws PHP 8 errors if text is empty and trim or removeDoubleWhitespace is set to 1. This happens with the ListViewHelper that has the AbstractTextViewHelper as parent.

Environment TYPO3 version(s): 11.5 PHP: 8.2 pdfviewhelpers version: 2.5.0

Steps to reproduce Add a list to the pdf like this:

<pdf:list listElements="{
    0: 'Option 1',
    1: 'Option 2',
    3: 'Option 3'
}" />

Expected behavior The list is rendered

Actual behavior

PHP Exception:

PHP Runtime Deprecation Notice: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /.../typo3conf/ext/pdfviewhelpers/Classes/ViewHelpers/AbstractTextViewHelper.php line 111

or

PHP Runtime Deprecation Notice: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /.../typo3conf/ext/pdfviewhelpers/Classes/ViewHelpers/AbstractTextViewHelper.php line 115

Possible solution

The problem lies in the initialize function of the AbstractTextViewHelper class with the following statements:

if ($this->arguments['trim']) {
    $this->arguments['text'] = trim($this->arguments['text']);
}

if ($this->arguments['removeDoubleWhitespace']) {
    $this->arguments['text'] = preg_replace('/[ \t]+/', ' ', $this->arguments['text']);
}

Here text is null triggering the PHP 8 deprecation. Adding && !empty($this->arguments['text'] to both if statements should fix the problem.

Both trim and removeDoubleWhitespace are set to 1 per TypoScript settings of generalText per default. As workaround I have set both configs to 0 for list. This is a general problem though and should be fixed.

maechler commented 1 year ago

@Barakur Thank you very much for the detailed error report! I'll have a look at it.