Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
/composer.lock
/tests/tmp/
.phpunit.result.cache
3 changes: 3 additions & 0 deletions src/Extension/ReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ public function enterNode(Node $node): Node
throw new ShouldNotHappenException();
}

// Reset the cache to force a new computation
$node->setAttribute('phpstan_cache_printer', null);

return $scope->getType($node);
}

Expand Down
7 changes: 1 addition & 6 deletions src/Rules/ThrowsPhpDocRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,21 +698,16 @@ private function processFuncCall(FuncCall $node, Scope $scope): array
*/
private function processDiv(Expr $divisor, Scope $scope): array
{
$divisionByZero = false;
$divisorType = $scope->getType($divisor);
foreach (TypeUtils::getConstantScalars($divisorType) as $constantScalarType) {
if ($constantScalarType->getValue() === 0) {
$divisionByZero = true;
return $this->processThrowsTypes(new ObjectType(DivisionByZeroError::class));
}

$divisorType = TypeCombinator::remove($divisorType, $constantScalarType);
}

if (!$divisorType instanceof NeverType) {
return $this->processThrowsTypes(new ObjectType(ArithmeticError::class));
}

if ($divisionByZero) {
return $this->processThrowsTypes(new ObjectType(DivisionByZeroError::class));
}

Expand Down
8 changes: 4 additions & 4 deletions tests/src/Rules/data/throws-php-internal-operators.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ public function test(): void
echo $a / 1;
echo $a / 0; // error: Missing @throws DivisionByZeroError annotation
echo $a / (rand(0, 1) === 0 ? 0 : 1); // error: Missing @throws DivisionByZeroError annotation
echo $a / $b; // error: Missing @throws ArithmeticError annotation
echo $a / $b; // error: Missing @throws DivisionByZeroError annotation
echo (rand(0, 1) === 0 ? 20 : 10) / (rand(0, 1) === 0 ? 5 : 10);

echo $a /= 1;
echo $a /= 0; // error: Missing @throws DivisionByZeroError annotation
echo $a /= (rand(0, 1) === 0 ? 0 : 1); // error: Missing @throws DivisionByZeroError annotation
echo $a /= $b; // error: Missing @throws ArithmeticError annotation
echo $a /= $b; // error: Missing @throws DivisionByZeroError annotation
echo $a /= (rand(0, 1) === 0 ? 5 : 10);

echo $a % 1;
echo $a % 0; // error: Missing @throws DivisionByZeroError annotation
echo $a % (rand(0, 1) === 0 ? 0 : 1); // error: Missing @throws DivisionByZeroError annotation
echo $a % $b; // error: Missing @throws ArithmeticError annotation
echo $a % $b; // error: Missing @throws DivisionByZeroError annotation
echo (rand(0, 1) === 0 ? 20 : 10) % (rand(0, 1) === 0 ? 5 : 10);

echo $a %= 1;
echo $a %= 0; // error: Missing @throws DivisionByZeroError annotation
echo $a %= (rand(0, 1) === 0 ? 0 : 1); // error: Missing @throws DivisionByZeroError annotation
echo $a %= $b; // error: Missing @throws ArithmeticError annotation
echo $a %= $b; // error: Missing @throws DivisionByZeroError annotation
echo $a %= (rand(0, 1) === 0 ? 5 : 10);

echo $a << 0;
Expand Down