Skip to content
Closed
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
51 changes: 51 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13385c.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace Bug13385c;

use function PHPStan\Testing\assertType;

interface Operator {
public function priority(): int;
public function calculate(int $a, int $b): int;
}

class HelloWorld
{
/**
* @param list<Operator|int> $children
*/
public function calculate(array $children): int {
$operands = [];
$operators = [];

foreach ($children as $child) {
if ($child instanceof Operator) {
for(;$operators !== [] && end($operators)->priority() >= $child->priority();) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test isn't faithful enough because there are no changes in NodeScopeResolver. And the same fix I did for While_ also needs to be done for For_ and many others.

$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<int>', $operands);
}

$operators[] = $child;
} else {
$operands[] = $child;
}
}

return count($operands) === 1 ? reset($operands) : 0;
}
}
51 changes: 51 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13385d.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace Bug13385d;

use function PHPStan\Testing\assertType;

interface Operator {
public function priority(): int;
public function calculate(int $a, int $b): int;
}

class HelloWorld
{
/**
* @param list<Operator|int> $children
*/
public function calculate(array $children): int {
$operands = [];
$operators = [];

for ($i = 0; $i < count($children); $i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the type of outer loop matters. We don't need more than one test for the same change and reported bug in src/.

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<int>', $operands);
}

$operators[] = $children[$i];
} else {
$operands[] = $children[$i];
}
}

return count($operands) === 1 ? reset($operands) : 0;
}
}
Loading