Undefined array key "children" in recursive call of ps_categorytree.tpl on PHP 8
Nobody has claimed this yet.
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 72/100
Research direction
Start in views/templates/hook/ps_categorytree.tpl, comparing the recursive call around line 34 with the guarded outer call around line 46. Render a page with both leaf and nested categories; done means leaf categories render without PHP 8 undefined-array-key warnings.
Written by the indexing model from the issue text.
Description
Bug description
The recursive categories function in views/templates/hook/ps_categorytree.tpl passes $node.children into its recursive call without a guard. When a node has no children (a leaf category), PHP 8 emits Undefined array key "children" on every render. The outer call site in the same template already uses a guard ({if !empty($categories.children)}), so this looks like a consistency oversight: the fix was applied once but not propagated to the recursive call.
Affected code
views/templates/hook/ps_categorytree.tpl, version 3.0.2 (current master).
{function name="categories" nodes=[] depth=0}
{strip}
{if $nodes|count}
<ul>
{foreach from=$nodes item=node}
<li>
<a href="{$node.link}">{$node.name}</a>
<div>
{categories nodes=$node.children depth=$depth+1} {* line 34, no guard on $node.children *}
</div>
</li>
{/foreach}
</ul>
{/if}
{/strip}
{/function}
<div class="category-tree">
<ul>
<li><a href="{$categories.link nofilter}">{$categories.name}</a></li>
{if !empty($categories.children)} {* line 46, guarded here *}
<li>{categories nodes=$categories.children}</li>
{/if}
</ul>
</div>
The outer {if !empty(...)} at line 46 protects the top-level call. The recursive call at line 34 does not.
Steps to reproduce
-
PS 9.x with PHP 8.1+ and
ps_categorytreeenabled in any hook position. -
Have at least one category tree where some branches have sub-categories and others do not (any non-trivial catalog).
-
Load any page that renders the widget. Each leaf category iteration emits:
PHP Warning: Undefined array key "children" in var/cache/prod/smarty/compile/<hash>_ps_categorytree.tpl.php on line 39The line in the compiled PHP corresponds to the Smarty template line 34.
Expected behavior
Leaf categories should render without warnings, same as the outer guarded branch.
Actual behavior
Every leaf iteration produces a PHP warning in the compiled Smarty template. On our production store with a standard catalog this generates ~50 warnings per day from this widget alone.
Proposed fix
Apply the same guard style already used at the outer call site (line 46) to the recursive call at line 34:
{foreach from=$nodes item=node}
<li>
<a href="{$node.link}">{$node.name}</a>
<div>
{if !empty($node.children)}
{categories nodes=$node.children depth=$depth+1}
{/if}
</div>
</li>
{/foreach}
Alternatively, using a Smarty default filter:
{categories nodes=$node.children|default:[] depth=$depth+1}
The first option matches the existing style in the same template.
Environment
- PrestaShop: 9.1.0
- PHP: 8.1
- Module
ps_categorytree: 3.0.2
- Dominant language
- PHP
- Stars
- 6
- Forks
- 29
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 1
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Similar issues
-
priority: p3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
googleapis/librarian#7636 ·
-
0. Needs triage bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nextcloud/fulltextsearch#1011 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
phpstan/phpstan-doctrine#794 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Automattic/static-site-importer#1767 ·