Skip to content

Difference between string increment typing and reality #10187

Closed
@zonuexe

Description

@zonuexe

Bug report

The result of incrementing a string is not necessarily a string.

function inc(string $n): string
{
	$before = $n;
	$after = ++$n;
	assertType('array{string|int, string, string|int}', [$n, $before, $after]));

	return (string)$after; // No warnings expected here
}

inc('10'); // => '11'
inc('a');  // => 'b'

This behavior is consistent between PHP 8.3 and earlier: https://3v4l.org/i4rru

There seems to be a problem with InitializerExprTypeResolver::getPlusType().

Code snippet that reproduces the problem

https://phpstan.org/r/110b3926-f4c5-4f51-b4ab-537fa2be3fb6

Expected output

No errors.

Did PHPStan help you today? Did it make you happy in any way?

I'm excited to see that PHPStan is working consistently even with PHP 8.3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions