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

Improve parenthesization for union types #1433

Merged
merged 1 commit into from Jun 17, 2022

Conversation

rvanvelzen
Copy link
Contributor

@@ -206,6 +206,8 @@ public function describe(VerbosityLevel $level): string
foreach ($types as $type) {
if ($type instanceof ClosureType || $type instanceof CallableType || $type instanceof TemplateUnionType) {
$typeNames[] = sprintf('(%s)', $type->describe($level));
} elseif ($type instanceof TemplateType && ($level->isTypeOnly() || $level->isValue())) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was doubting to do this regardless of level, but for precise/cache the template scope and variance is appended which already makes it unambiguous.

$errors = $this->runAnalyse(__DIR__ . '/data/bug-7484.php');
$this->assertCount(1, $errors);
$this->assertSame('Generator expects key type K of int|string, (K of int)|string given.', $errors[0]->getMessage());
$this->assertSame(21, $errors[0]->getLine());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I usually keep AnalyserIntegrationTest for things that crashed PHPStan/caused infinite loop. It's better to put the test in a specific rule test case. (or a specific rule test case + NodeScopeResolverTest for type inference verification).

But I'm gonna merge it like this so that the fix isn't belated :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep it in mind :)

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh actually, it looks lit's adding parentheses in more cases where it isn't necessary, and breaking people's baselines: https://github.com/phpstan/phpstan-src/runs/6933410671?check_suite_focus=true, https://github.com/phpstan/phpstan-src/runs/6933409913?check_suite_focus=true, https://github.com/phpstan/phpstan-src/runs/6933409824?check_suite_focus=true

I'd say that if the template type is last, the parenthesis isn't necessary...

@ondrejmirtes
Copy link
Member

Even though there are failures, I'm happy with the results now!

@ondrejmirtes ondrejmirtes merged commit 99ddeaf into phpstan:1.7.x Jun 17, 2022
@ondrejmirtes
Copy link
Member

Thank you!

@rvanvelzen rvanvelzen deleted the union-parens branch June 17, 2022 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants