Skip to content

Fix inferred union types for Ds Map::get() and Map::remove()#176

Merged
ondrejmirtes merged 2 commits intophpstan:masterfrom
simPod:no-union
Apr 13, 2020
Merged

Fix inferred union types for Ds Map::get() and Map::remove()#176
ondrejmirtes merged 2 commits intophpstan:masterfrom
simPod:no-union

Conversation

@simPod
Copy link
Copy Markdown
Contributor

@simPod simPod commented Apr 10, 2020

👋 can you guide me with this please?

I'm passing Map<int, int|string> into the function.

However, return type of get(0) call on the map is resolved as int. I'd expect int|string.

/** @param Map<int, int|string> $a */
public function mapGetUnionType(Map $a) : void
{
	assertType('int|string', $a->get(0));
}
Expected :'int|string'
Actual   :'int'

Map stub is

/**
 * @template TKey
 * @template TValue
 * @implements Collection<TKey, TValue>
 */
final class Map implements Collection
{
    /**
     * @param iterable<TKey, TValue>|null $values
     */
    public function __construct(?iterable $values = null)
    {
    }

So I believe I'm saying that TValue is int|string.

Map::get()'s signature is

/**
 * @template TDefault
 * @param TKey $key
 * @param TDefault $default
 * @return TValue|TDefault
 * @throws OutOfBoundsException
 */
public function get($key, $default = null)

so get(0) should return TValue (int|string). Or not? Thanks!

@simPod simPod marked this pull request as draft April 10, 2020 11:55
@ondrejmirtes
Copy link
Copy Markdown
Member

No idea what's wrong, What happens if you pass in $a->get('foo')?

@ondrejmirtes
Copy link
Copy Markdown
Member

Oh, I realized what's wrong. There's this extension: https://github.com/phpstan/phpstan-src/blob/master/src/Type/Php/DsMapDynamicReturnTypeExtension.php

You can try to remove it and see what happens.

@simPod
Copy link
Copy Markdown
Contributor Author

simPod commented Apr 10, 2020

You're right. Would it make sense to drop it then? Like in the commit 👆

@simPod
Copy link
Copy Markdown
Contributor Author

simPod commented Apr 11, 2020

OutOfBounds exception is thrown if I remember correctly

@ondrejmirtes
Copy link
Copy Markdown
Member

You're right. So the @return TValue|TDefault is wrong if the second argument is not present. So the extension must be modified and not deleted.

@simPod simPod changed the title Add test for Map::get() with union type Fix inferred union types for Ds Map::get() and Map::remove() Apr 12, 2020
@simPod simPod marked this pull request as ready for review April 12, 2020 06:11
@simPod simPod requested a review from ondrejmirtes April 12, 2020 06:11
@ondrejmirtes
Copy link
Copy Markdown
Member

Looks fine, thank you!

@ondrejmirtes ondrejmirtes merged commit 3f76fd3 into phpstan:master Apr 13, 2020
@simPod simPod deleted the no-union branch April 13, 2020 09:37
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.

2 participants