diff --git a/tests/PHPStan/Analyser/nsrt/bug-13385c.php b/tests/PHPStan/Analyser/nsrt/bug-13385c.php new file mode 100644 index 0000000000..a2feada0dc --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-13385c.php @@ -0,0 +1,51 @@ + $children + */ + public function calculate(array $children): int { + $operands = []; + $operators = []; + + foreach ($children as $child) { + if ($child instanceof Operator) { + for(;$operators !== [] && end($operators)->priority() >= $child->priority();) { + $op = array_pop($operators); + $left = array_pop($operands) ?? 0; + $right = array_pop($operands) ?? 0; + + assert(is_int($left)); + assert(is_int($right)); + + $value = $op->calculate($left, $right); + + assertType(Operator::class, $op); + assertType('int', $left); + assertType('int', $right); + assertType('int', $value); + + $operands[] = $value; + + assertType('non-empty-list', $operands); + } + + $operators[] = $child; + } else { + $operands[] = $child; + } + } + + return count($operands) === 1 ? reset($operands) : 0; + } +} diff --git a/tests/PHPStan/Analyser/nsrt/bug-13385d.php b/tests/PHPStan/Analyser/nsrt/bug-13385d.php new file mode 100644 index 0000000000..8825d6ffd5 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-13385d.php @@ -0,0 +1,51 @@ + $children + */ + public function calculate(array $children): int { + $operands = []; + $operators = []; + + for ($i = 0; $i < count($children); $i++) { + if ($children[$i] instanceof Operator) { + while ($operators !== [] && end($operators)->priority() >= $children[$i]->priority()) { + $op = array_pop($operators); + $left = array_pop($operands) ?? 0; + $right = array_pop($operands) ?? 0; + + assert(is_int($left)); + assert(is_int($right)); + + $value = $op->calculate($left, $right); + + assertType(Operator::class, $op); + assertType('int', $left); + assertType('int', $right); + assertType('int', $value); + + $operands[] = $value; + + assertType('non-empty-list', $operands); + } + + $operators[] = $children[$i]; + } else { + $operands[] = $children[$i]; + } + } + + return count($operands) === 1 ? reset($operands) : 0; + } +}