Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Optimizer/FuncCallOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,16 @@ protected function doFoldSsaType(Node\Expr\FuncCall $expr, mixed $expectType): s
if (count($expr->args) !== 1 || !($expr->args[0] instanceof Node\Arg)) {
return false;
}
return ($this->detectTypeOfExpr($expr->args[0]->value) === $expectType) ? 'true' : false;
$value = $expr->args[0]->value;
if ($this->detectTypeOfExpr($value) !== $expectType) {
return false;
}
if ($value instanceof Node\Expr\Variable || $value instanceof Node\Scalar) {
return 'true';
}
// The argument can carry side effects (a call, an increment). Keep
// evaluating it, as genIsNull does for native scalar operands.
return '((void) (' . $this->parseExprAsValue($value) . '), true)';
}

// =========================================================================
Expand Down
44 changes: 44 additions & 0 deletions tests/compiler/optimizations/is-type-fold-side-effects.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
Folded is_int/is_float/is_bool must keep evaluating side-effect arguments
--FILE--
<?php
function intSource(): int
{
echo "int-called\n";
return 42;
}

function floatSource(): float
{
echo "float-called\n";
return 1.5;
}

function boolSource(): bool
{
echo "bool-called\n";
return true;
}

function main(): void
{
if (is_int(intSource())) {
echo "is-int\n";
}
echo is_float(floatSource()) ? "is-float\n" : "not-float\n";
$r = is_bool(boolSource());
echo $r ? "is-bool\n" : "not-bool\n";

// Plain variables still fold without extra evaluation.
$n = 7;
echo is_int($n) ? "var-int\n" : "var-not-int\n";
}
?>
--EXPECT--
int-called
is-int
float-called
is-float
bool-called
is-bool
var-int
Loading