Skip to content

Commit

Permalink
Fix #485: submenu() and sitemap throw TypeError with PHP 8
Browse files Browse the repository at this point in the history
If `$this->st` is a non numeric string (such as "sitemaplevel" or
"submenu"), subtracting `1` will throw a TypeError with PHP 8.  We fix
this by explicitly casting to int.

While we're at it, we also switch to type safe `===` comparisons, and
document that "search" is longer used by the core.
  • Loading branch information
cmb69 committed Jan 26, 2021
1 parent adef433 commit 49ed061
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions cmsimple/classes/Li.php
Expand Up @@ -25,7 +25,7 @@ class Li
/**
* The menu level to start with or the type of menu.
*
* @var mixed
* @var int|string
*/
protected $st;

Expand Down Expand Up @@ -54,7 +54,8 @@ class Li
* Renders a menu structure of certain pages.
*
* @param array $ta The indexes of the pages.
* @param mixed $st The menu level to start with or the type of menu.
* @param int|string $st The menu level to start with or the type of menu.
* 'search' is no longer used by the core since ~ 1.5.
*
* @return string HTML
*/
Expand All @@ -69,23 +70,23 @@ public function render(array $ta, $st)
return '';
}
$t = '';
if ($this->st == 'submenu' || $this->st == 'search') {
if ($this->st === 'submenu' || $this->st === 'search') {
$t .= '<ul class="' . $this->st . '">' . "\n";
}
$this->b = 0;
if ($this->st > 0) {
$this->b = $this->st - 1;
if ((int) $this->st > 0) {
$this->b = (int) $this->st - 1;
$this->st = 'menulevel';
}
$this->lf = array();
for ($i = 0; $i < $tl; $i++) {
$this->tf = ($s != $this->ta[$i]);
if ($this->st == 'menulevel' || $this->st == 'sitemaplevel') {
if ($this->st === 'menulevel' || $this->st === 'sitemaplevel') {
$t .= $this->renderULStartTags($i);
}
$t .= '<li class="' . $this->getClassName($i) . '">';
$t .= $this->renderMenuItem($i);
if ($this->st == 'menulevel' || $this->st == 'sitemaplevel') {
if ($this->st === 'menulevel' || $this->st === 'sitemaplevel') {
if ($this->getMenuLevel($i + 1) > $this->getMenuLevel($i)) {
$this->lf[$this->getMenuLevel($i)] = true;
} else {
Expand All @@ -97,7 +98,7 @@ public function render(array $ta, $st)
$t .= '</li>' . "\n";
}
}
if ($this->st == 'submenu' || $this->st == 'search') {
if ($this->st === 'submenu' || $this->st === 'search') {
$t .= '</ul>' . "\n";
}
return $t;
Expand Down

0 comments on commit 49ed061

Please sign in to comment.