"Maximum execution time of 30 seconds exceeded" in hasEvenNumberOfParentheses Function (NEW)

Good day everyone! I would like to address this issue in Laravel Filament Project, and I hope someone will fix this issue. It seems like this happens to me when I have a RelationManager in my specific Filament Resource inside whenever I open one item. Can someone help me please?
protected static function hasEvenNumberOfParentheses(string $expression)

{

$tokens = token_get_all('<?php '.$expression);



if (Arr::last($tokens) !== ')') {

return false;

}



$opening = 0;

$closing = 0;



foreach ($tokens as $token) {

if ($token == ')') {

$closing++;

} elseif ($token == '(') {

$opening++;

}

}



return $opening === $closing;

}
protected static function hasEvenNumberOfParentheses(string $expression)

{

$tokens = token_get_all('<?php '.$expression);



if (Arr::last($tokens) !== ')') {

return false;

}



$opening = 0;

$closing = 0;



foreach ($tokens as $token) {

if ($token == ')') {

$closing++;

} elseif ($token == '(') {

$opening++;

}

}



return $opening === $closing;

}
Any suggestion would be appreciated. Thank you!
1 Reply
toeknee
toeknee2d ago
That doesn't make any sense to filament really. That's a custom php function. It would say your expression is too big. It also looks like you want the balance of the parentheses so you could just loop the charachters.
protected static function hasEvenNumberOfParentheses(string $expression)
{
$opening = 0;
$closing = 0;

$length = strlen($expression);
for ($i = 0; $i < $length; $i++) {
if ($expression[$i] === '(') $opening++;
elseif ($expression[$i] === ')') $closing++;
}

return $opening === $closing && $expression[$length - 1] === ')';
}
protected static function hasEvenNumberOfParentheses(string $expression)
{
$opening = 0;
$closing = 0;

$length = strlen($expression);
for ($i = 0; $i < $length; $i++) {
if ($expression[$i] === '(') $opening++;
elseif ($expression[$i] === ')') $closing++;
}

return $opening === $closing && $expression[$length - 1] === ')';
}

Did you find this page helpful?