Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,20 @@ public function getDivType(Expr $left, Expr $right, callable $getTypeCallback):
$leftType = $getTypeCallback($left);
$rightType = $getTypeCallback($right);

$result = $this->getDivTypeFromTypes($left, $right, $leftType, $rightType);

if ($leftType->isInteger()->yes() && $rightType->isInteger()->yes()) {
Comment thread
VincentLanglet marked this conversation as resolved.
$modType = $getTypeCallback(new BinaryOp\Mod($left, $right));
if ($modType->isInteger()->yes() && (new ConstantIntegerType(0))->isSuperTypeOf($modType)->yes()) {
Comment thread
staabm marked this conversation as resolved.
return TypeCombinator::remove($result, new FloatType());
}
}

return $result;
}

private function getDivTypeFromTypes(Expr $left, Expr $right, Type $leftType, Type $rightType): Type
{
$leftTypes = $leftType->getConstantScalarTypes();
$rightTypes = $rightType->getConstantScalarTypes();
$leftTypesCount = count($leftTypes);
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9724.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types = 1);

namespace Bug9724;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public function thisWorks(?int $limit, int $offset = 0): void
{
if ($limit && 0 === ($offset % $limit)) {
assertType('int', $offset / $limit);
assertType('int', ($offset / $limit) + 1);
}
}

public function thisDoesntWork(?int $limit, int $offset = 0): void
{
if ($limit && $offset && (0 === ($offset % $limit))) {
assertType('int<min, -1>|int<1, max>', $offset / $limit);
assertType('int<min, 0>|int<2, max>', ($offset / $limit) + 1);
}
}

/** @param int<-2, 2> $offsetRange */
public function withRange(int $limit, int $offset, int $offsetRange): void
{
if ($limit) {
assertType('(float|int)', $offset / $limit);
assertType('float|int<-2, 2>', $offsetRange / $limit);
}
}

private function expectInt(int $page): void
{
}

public function originalIssue(?int $limit, int $offset = 0): void
{
if ($limit && $offset && (0 === ($offset % $limit))) {
$this->expectInt(($offset / $limit) + 1);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this code needs to be analyzed by a RuleTest to proof it fixes the error reported in phpstan/phpstan#9724

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done. Added a CallMethodsRule test (testBug9724) with a test data file that reproduces the exact scenario from issue #9724 — calling $this->expectInt(($offset / $limit) + 1) inside a modulo-checked condition. The test expects zero errors, proving the fix resolves the originally reported type mismatch. All 12156 tests pass and make phpstan reports no errors.

}
Loading