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

Support refinement of tagged unions of array shapes #6469

Closed
arnaud-lb opened this issue Jan 27, 2022 · 8 comments
Closed

Support refinement of tagged unions of array shapes #6469

arnaud-lb opened this issue Jan 27, 2022 · 8 comments

Comments

@arnaud-lb
Copy link
Contributor

Feature request

If we have some types like

@phpstan-type Pizza   array{type: 'pizza', toppings: Topping[]}
@phpstan-type Pasta   array{type: 'pasta', salsa: Salsa}
@phpstan-type Meal    Pizza|Pasta

PHPStan could be able to determine which Meal we have after looking at the type field:

/**
 * @param Meal $meal
 */
function ($meal) {
    if ($meal['type'] === 'pizza') {
        \PHPStan\dumpType($meal); // array{type: 'pizza', toppings: Topping[]} (Pizza)
    } else {
        \PHPStan\dumpType($meal); // array{type: 'pasta', salsa: Salsa} (Pasta)
    }
}

https://phpstan.org/r/cacfda2a-94de-4bb6-8da6-2c0d36732967

Flow documentation on disjoint unions: https://flow.org/en/docs/types/unions/#toc-disjointo-object-unions

@ZebulanStanphill
Copy link
Contributor

Array unions are currently collapsed improperly. See #3801.

@ondrejmirtes
Copy link
Member

Sometimes they are, but not in this case: https://phpstan.org/r/5722f56e-2429-4bfa-9ef2-78b733286287

@Seldaek
Copy link
Contributor

Seldaek commented Feb 1, 2022

One additional case from #6499 reposting it here for the bot's enjoyment :P

https://phpstan.org/r/2bb784a9-284c-458f-b001-5914ca6f2a70

@ondrejmirtes
Copy link
Member

@ondrejmirtes
Copy link
Member

See #7666

@phpstan-bot
Copy link
Contributor

@arnaud-lb After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-18: Dumped type: array{type: 'pizza', salsa: string}|array{type: 'pizza', toppings: array<string>}
-20: Dumped type: array{type: 'pasta', salsa: string}|array{type: 'pasta', toppings: array<string>}
+18: Dumped type: array{type: 'pizza', toppings: array<string>}
+20: Dumped type: array{type: 'pasta', salsa: string}
Full report
Line Error
18 Dumped type: array{type: 'pizza', toppings: array<string>}
20 Dumped type: array{type: 'pasta', salsa: string}

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
 17: Dumped type: array{type: 'pasta', salsa: string}|array{type: 'pizza', toppings: array<string>}
-19: Dumped type: array{type: 'pizza', salsa: string}|array{type: 'pizza', toppings: array<string>}
-21: Dumped type: array{type: 'pasta', salsa: string}|array{type: 'pasta', toppings: array<string>}
+19: Dumped type: array{type: 'pizza', toppings: array<string>}
+21: Dumped type: array{type: 'pasta', salsa: string}
Full report
Line Error
17 `Dumped type: array{type: 'pasta', salsa: string}
19 Dumped type: array{type: 'pizza', toppings: array<string>}
21 Dumped type: array{type: 'pasta', salsa: string}

ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue Jul 26, 2022
ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue Aug 26, 2022
ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue Aug 27, 2022
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants