False positive on the type of the result of an arithmetic operation

Open
#46 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
devtools

Research direction

Start by locating checkAlwaysTrueCheckTypeFunctionCall and reproduce the PHP example with PHP_INT_MAX + 1. Trace how the checker infers the arithmetic result type. Done means it no longer reports is_int($result) as always true for this case, with regression coverage for the example.

Written by the indexing model from the issue text.

Description

The check checkAlwaysTrueCheckTypeFunctionCall incorrectly assumes that the result of adding two integers is always an integer:

function addExact(int $augend, int $addend) : int
{
    $result = $augend + $addend;

    // Call to function is_int() with int will always evaluate to true.  
    if (!\is_int($result)) {
        throw new OverflowException('The result overflows the range of an integer.');
    }

    return $result;
}

addExact(\PHP_INT_MAX, 1);
Dominant language
PHP
Stars
709
Forks
62
Avg merge
5d 19h
Merged PRs (30d)
3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-strict-rules

All issues in phpstan/phpstan-strict-rules

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.