Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict int/float comparison for conditional types, improve range return type #5601

Merged
merged 1 commit into from
Apr 9, 2021

Conversation

AndrolGenhald
Copy link
Collaborator

Fixes incorrect conditional return type due to int/float comparison.
int|string works as expected: https://psalm.dev/r/b87cd02f3a
but int|float produces very odd results: https://psalm.dev/r/993cd83eb3

Also improve range return type to be list<float> if $step is float (I didn't find any PHP documentation addressing this but it seems to be empirically true: https://3v4l.org/3tcIK).

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b87cd02f3a
<?php

/**
 * @psalm-suppress InvalidReturnType
 * @psalm-suppress MismatchingDocblockReturnType
 * @return ($bar is int ? list<int> : list<string>)
 */
function foo($bar): string {}

/** @var int */
$baz = 1;
$a = foo($baz);

/** @var string */
$baz = '1';
$b = foo($baz);

/** @var int|string */
$baz = 1;
$c = foo($baz);

/** @psalm-trace $a */;
/** @psalm-trace $b */;
/** @psalm-trace $c */;
Psalm output (using commit 5f4a211):

INFO: Trace - 22:23 - $a: list<int>

INFO: Trace - 23:23 - $b: list<string>

INFO: Trace - 24:23 - $c: list<int|string>
https://psalm.dev/r/993cd83eb3
<?php

/**
 * @psalm-suppress InvalidReturnType
 * @psalm-suppress MismatchingDocblockReturnType
 * @return ($bar is int ? list<int> : list<float>)
 */
function foo($bar): string {}

/** @var int */
$baz = 1;
$a = foo($baz);

/** @var float */
$baz = 1.;
$b = foo($baz);

/** @var int|float */
$baz = 1;
$c = foo($baz);

/** @psalm-trace $a */;
/** @psalm-trace $b */;
/** @psalm-trace $c */;
Psalm output (using commit 5f4a211):

INFO: Trace - 22:23 - $a: list<int>

INFO: Trace - 23:23 - $b: list<float|int>

INFO: Trace - 24:23 - $c: list<int>

@muglug muglug merged commit 518d0af into vimeo:master Apr 9, 2021
@muglug
Copy link
Collaborator

muglug commented Apr 9, 2021

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants