Phpstan: request for feasibility of type checking on sprintf args

Created on 4 Jul 2018  路  3Comments  路  Source: phpstan/phpstan

Summary of a problem or a feature request

I'm wondering how much of a pain it would be to support type safety with sprintf, i.e. if a string arg is passed to the position for a %u, or a numeric arg is passed to the position for a %s.

Code snippet that reproduces the problem

sprintf('%u %s', 'test', 1);

https://phpstan.org/r/3e8698758181316ef2d943761e6b184d

Expected output

The preference (with the example above) would be errors to the effect of :

  • Argument 2 passed to sprintf on file.php:123 should be an integer
  • Argument 3 passed to sprintf on file.php:123 should be a string

Most helpful comment

Hi, I think that it's valid to pass an integer to %s - it just says "I want to format this as a string". But let's wait for other opinions 馃槉

All 3 comments

Hi, I think that it's valid to pass an integer to %s - it just says "I want to format this as a string". But let's wait for other opinions 馃槉

A different way of phrasing it:

should sprintf with argument 1 of '%u %s' be considered roughly equivalent to this:

function foo(int $a, string $b) : string {
    return (string) $a . ' ' . $b; // sidestepping the whole unsigned integer representation issue
}

Closing since the people have spoken with their thumbs 馃槉

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dingo-d picture dingo-d  路  3Comments

eclipxe13 picture eclipxe13  路  4Comments

jkuchar picture jkuchar  路  3Comments

pretzlaw picture pretzlaw  路  3Comments

llissssss picture llissssss  路  3Comments