86471[BUGFIX] Prevent PHP errors / warnings on flexform resolving
Commit message
Review
-
Code review CR +2 Done
- Stefan Bürk +2
- Christian Kuhn +1
-
Verified V +2 Done
- Stefan Bürk +2
- Christian Kuhn +1
- core-ci +1 CI
Issues 1
Files 1
Review log 1617
-
Oli Bartsch uploaded patch set 1 10/08/24, 08:14
-
core-ci voted on patch set 1 V +1 10/08/24, 08:20 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/67338
-
Christian Kuhn voted on patch set 1 CR +1 V +1 10/08/24, 08:55
-
Stefan Bürk voted on patch set 1 CR +1 V +1 10/08/24, 11:32
-
Stefan Bürk voted on patch set 1 CR +2 V +2 10/08/24, 11:57
-
Stefan Bürk merged patch set 2 10/08/24, 11:57
-
Ayke Halder commented on patch set 2 10/08/24, 12:33
This still gives `PHP Notice: Undefined index: pi_flexform` for non-existent `pi_flexform`.FormPagePreviewRenderer.php:61How does `is_string` ensures that the array-key exists before accessing it. Actually `is_string` accesses it by itself without testing for existence. ``` php -r "\$row = []; echo (is_string(\$row['pi_flexform']) ? 'Yes ' . \$row['pi_flexform'] : 'No: ' . \$row['pi_flexform'] );" PHP Notice: Undefined index: pi_flexform in Command line code on line 1 PHP Notice: Undefined index: pi_flexform in Command line code on line 1 No: ``` So we actually need something like this here: ``` if (isset($row['pi_flexform'])) { $flexFormData = is_string($row['pi_flexform'] ?? null) ? $this-flexFormService->convertFlexFormContentToArray($row['pi_flexform']) : $row['pi_flexform'] ?? ''; } ``` -
Ayke Halder commented on patch set 2 10/08/24, 12:34
There is already a check before … `if (isset($row['pi_flexform'])) {`Reply
DoneFormPagePreviewRenderer.php:61ReplyDone -
Ayke Halder commented on patch set 2 10/08/24, 12:39
Why is any non-string value returned as `$flexFormData` instead of an empty array?FormPagePreviewRenderer.php:60Why didn't we implement it like this? ``` $flexFormData = []; if (is_string($row['pi_flexform'] ?? null) { $flexFormData = $this->flexFormService->convertFlexFormContentToArray($row['pi_flexform']); } ``` -
Oli Bartsch commented on patch set 2 10/08/24, 13:10
Reply
We do not return anything but update the local variable which is used once where a proper null colasing operator is used. This patch does exactly what it states: Ensures $row['pi_flexform'] is only accessed when set and only converted in case it's a string.FormPagePreviewRenderer.php:60ReplyBecause $flexFormData would be an empty array in case $row['pi_flexform'] is not a string. -
Ayke Halder commented on patch set 2 10/08/24, 13:50
This is just for the case `$row['pi_flexform']` is somehow an array, boolean, numeric … - anything that's not a string: Here `$flexFormData` can become anything - but not the intended (empty) array.FormPagePreviewRenderer.php:60Reply> Because $flexFormData would be an empty array in case $row['pi_flexform'] is not a string. This is not correct. ---- In case `$row['pi_flexform']` is not a string: `$flexFormData = is_string($row['pi_flexform']) ? … : $row['pi_flexform'];` so `$flexFormData = $row['pi_flexform'];` Thereby `$flexFormData` becomes anything that is `$row['pi_flexform']` - but not the intended empty array. -
Oli Bartsch commented on patch set 2 10/08/24, 14:29
Reply
Who states that an (empty) array should be enforced? As I wrote in the other comment we access $flexFormData only once with a null colasing operator to be safe. Sure we could improve this to make it clear that we expect an array. But as said this was not part of the patch and the commit message does also not mention this.FormPagePreviewRenderer.php:60ReplyI can't find `$flexFormData = is_string($row['pi_flexform']) ? … : $row['pi_flexform'];` in your code. ``` $flexFormData = []; if (is_string($row['pi_flexform'] ?? null) { $flexFormData = $this->flexFormService->convertFlexFormContentToArray($row['pi_flexform']); } ``` will lead to $flexFormData staying empty if $row['pi_flexform'] is not a string. -
Ayke Halder commented on patch set 2 10/08/24, 14:57
Reply
Me states that for `$flexFormData` an (empty) array should be enforced instead of an arbitrary value of `$row['pi_flexform']` - that's my whole point here. The logical intended value for `$flexFormData` is an (empty) array here: * `FlexFormService::convertFlexFormContentToArray(…)` always returns an (empty) array. * `$flexFormData` is only used like an array here: `$persistenceIdentifier = $flexFormData['settings']['persistenceIdentifier'] ?? '';` ---- So setting `$flexFormData` to * either an empty array or * the return value of `FlexFormService::convertFlexFormContentToArray(…)` which is an (empty) array will make this code easier to read and understand. That's why we do not need to implement this unneccessary complicated code here: ``` $flexFormData = []; if (isset($row['pi_flexform'])) { $flexFormData = is_string($row['pi_flexform']) ? $this->flexFormService->convertFlexFormContentToArray($row['pi_flexform']) : $row['pi_flexform']; } ```FormPagePreviewRenderer.php:60ReplyI find this line `$flexFormData = is_string($row['pi_flexform']) ? … : $row['pi_flexform'];` in the merged code of this patch. This code might use any arbitrary value of `$row['pi_flexform']`, which IMO is not good - that's my whole point here. ---- There was a closing-round-bracket `)` missing in my suggested code: ``` $flexFormData = []; if (is_string($row['pi_flexform'] ?? null)) { $flexFormData = $this->flexFormService->convertFlexFormContentToArray($row['pi_flexform']); } ``` > will lead to $flexFormData staying empty if $row['pi_flexform'] is not a string. So my code leads to `$flexFormData` not beeing altered if `$row['pi_flexform']` is either no string or empty. Thereby `$flexFormData = []` stays the 'initialized' empty array - as intended. -
Oli Bartsch commented on patch set 2 10/08/24, 20:16
Reply
So what about `$row['pi_flexform']` being an already resolved array?FormPagePreviewRenderer.php:60ReplyYes, and that's wrong as $row['pi_flexform'] might be an already resolved array. -
Ayke Halder commented on patch set 2 10/08/24, 20:35
Reply
So actually the new change really introduced a new feature: "`$row['pi_flexform']` might be an array" This new feature * was not there before and * is not mentioned in the commit message.FormPagePreviewRenderer.php:60Reply> … as $row['pi_flexform'] might be an already resolved array. If it really is an already resolved array, there MUST NOT be any call of `public function renderPageModulePreviewContent(GridColumnItem $item)` for the `$item` anymore. The old code directly called: ``` $flexFormData = $this->flexFormService->convertFlexFormContentToArray($row['pi_flexform']); ``` --> No array allowed in the old code for `$row['pi_flexform']`! -
Oli Bartsch commented on patch set 2 10/08/24, 21:41
Reply
the new "feature" is unrelated as it's due to outside code (transforming flexform fields), It's mentioned in the commit message by "and also only tries to convert it, in case it's still a string." -> So keeping it if it is not a string. However, the patch does what it says. I will stop commenting on this patch now. Please feel free to create a new patch handling your use cases and let's talk on this patch then.FormPagePreviewRenderer.php:60ReplyAnd we now react on recent changes in other parts of our code base. Those are not yet relevant, as the given row is still unresolved. However, there are plans to pass the resolved record object to the grid item in the future and this place is then already prepared while the change does no harm now, but instead unifies the handling of this field across the code base. Anyways, as mentioned in my other comment. Feel free to provide a patch as basis for further discussions. -
Ayke Halder commented on patch set 2 10/09/24, 17:38
Reply
Follow-up patch: 86541: [BUGFIX] Ensure flexform data only contains a string or array | https://review.typo3.org/c/Packages/TYPO3.CMS/+/86541FormPagePreviewRenderer.php:60ReplySo on the code itself, this might be a better readable implementation: ``` $flexFormData = $row['pi_flexform'] ?? []; if (is_string($flexFormData)) { $flexFormData = $this->flexFormService->convertFlexFormContentToArray($flexFormData); } ``` As implemented in the follow-up patch.