Skip to content

Commit 0326171

Browse files
committed
Added regression test
1 parent e9e1c03 commit 0326171

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13385b;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
interface Operator {
8+
public function priority(): int;
9+
public function calculate(int $a, int $b): int;
10+
}
11+
12+
class HelloWorld
13+
{
14+
/**
15+
* @param list<Operator|int> $children
16+
*/
17+
public function calculate(array $children): int {
18+
$operands = [];
19+
$operators = [];
20+
21+
foreach ($children as $child) {
22+
if ($child instanceof Operator) {
23+
while ($operators !== []) {
24+
$op = array_pop($operators);
25+
$left = array_pop($operands) ?? 0;
26+
$right = array_pop($operands) ?? 0;
27+
28+
assert(is_int($left));
29+
assert(is_int($right));
30+
31+
$value = $op->calculate($left, $right);
32+
33+
assertType(Operator::class, $op);
34+
assertType('int', $left);
35+
assertType('int', $right);
36+
assertType('int', $value);
37+
38+
$operands[] = $value;
39+
40+
assertType('non-empty-list<int>', $operands);
41+
}
42+
43+
$operators[] = $child;
44+
} else {
45+
$operands[] = $child;
46+
}
47+
}
48+
49+
return count($operands) === 1 ? reset($operands) : 0;
50+
}
51+
}

0 commit comments

Comments
 (0)