Skip to content

Commit 6b82f45

Browse files
committed
Extend driver stat class to fix a compatibility bug with PHP 8.1
1 parent 0ab0e46 commit 6b82f45

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* DISCLAIMER
4+
*
5+
* Do not edit or add to this file if you wish to upgrade this module
6+
* to newer versions in the future.
7+
*/
8+
declare(strict_types=1);
9+
10+
namespace Smile\DebugToolbar\Model\Profiler\Driver\Standard;
11+
12+
use Magento\Framework\Profiler;
13+
use Magento\Framework\Profiler\Driver\Standard\Stat as BaseStat;
14+
15+
/**
16+
* Overridden because core class is not compatible with PHP 8.1...
17+
*/
18+
class Stat extends BaseStat
19+
{
20+
/**
21+
* @inheritdoc
22+
*/
23+
protected function _getOrderedTimerIds()
24+
{
25+
$timerIds = array_keys($this->_timers);
26+
if (count($timerIds) <= 2) {
27+
/* No sorting needed */
28+
return $timerIds;
29+
}
30+
31+
/* Prepare PCRE once to use it inside the loop body */
32+
$nestingSep = preg_quote(Profiler::NESTING_SEPARATOR, '/');
33+
$patternLastTimerId = '/' . $nestingSep . '(?:.(?!' . $nestingSep . '))+$/';
34+
35+
$prevTimerId = $timerIds[0];
36+
$result = [$prevTimerId];
37+
for ($i = 1; $i < count($timerIds); $i++) {
38+
$timerId = $timerIds[$i];
39+
/* Skip already added timer */
40+
if (!$timerId) {
41+
continue;
42+
}
43+
/* Loop over all timers that need to be closed under previous timer */
44+
while (strpos($timerId, $prevTimerId . Profiler::NESTING_SEPARATOR) !== 0) {
45+
/* Add to result all timers nested in the previous timer */
46+
for ($j = $i + 1; $j < count($timerIds); $j++) {
47+
// REWRITE: check null value before calling strpos
48+
if ($timerIds[$j] !== null
49+
&& strpos($timerIds[$j], $prevTimerId . Profiler::NESTING_SEPARATOR) === 0
50+
) {
51+
$result[] = $timerIds[$j];
52+
/* Mark timer as already added */
53+
$timerIds[$j] = null;
54+
}
55+
}
56+
/* Go to upper level timer */
57+
$count = 0;
58+
$prevTimerId = preg_replace($patternLastTimerId, '', $prevTimerId, -1, $count);
59+
/* Break the loop if no replacements was done. It is possible when we are */
60+
/* working with top level (root) item */
61+
if (!$count) {
62+
break;
63+
}
64+
}
65+
/* Add current timer to the result */
66+
$result[] = $timerId;
67+
$prevTimerId = $timerId;
68+
}
69+
return $result;
70+
}
71+
}

registration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
use Magento\Framework\Component\ComponentRegistrar;
1010
use Magento\Framework\Profiler;
11-
use Magento\Framework\Profiler\Driver\Standard\Stat;
1211
use Smile\DebugToolbar\Helper\Profiler as SmileProfiler;
12+
use Smile\DebugToolbar\Model\Profiler\Driver\Standard\Stat;
1313

1414
if (PHP_SAPI !== 'cli') {
1515
// We need to declare the stat profiler manually, to use it after

0 commit comments

Comments
 (0)