Skip to content

Commit

Permalink
Improve parenthesization for union types
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen committed Jun 17, 2022
1 parent 5b2d7b6 commit 4fda900
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Type/UnionType.php
Expand Up @@ -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())) {
$typeNames[] = sprintf('(%s)', $type->describe($level));
} elseif ($type instanceof IntersectionType) {
$intersectionDescription = $type->describe($level);
if (strpos($intersectionDescription, '&') !== false) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/VerbosityLevel.php
Expand Up @@ -60,6 +60,11 @@ public function isTypeOnly(): bool
return $this->value === self::TYPE_ONLY;
}

public function isValue(): bool
{
return $this->value === self::VALUE;
}

/** @api */
public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acceptedType = null): self
{
Expand Down
10 changes: 9 additions & 1 deletion tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Expand Up @@ -591,7 +591,7 @@ public function testBug6842(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-6842.php');
$this->assertCount(1, $errors);
$this->assertSame('Generator expects value type T of DateTimeInterface, DateTime|DateTimeImmutable|T of DateTimeInterface given.', $errors[0]->getMessage());
$this->assertSame('Generator expects value type T of DateTimeInterface, DateTime|DateTimeImmutable|(T of DateTimeInterface) given.', $errors[0]->getMessage());
$this->assertSame(28, $errors[0]->getLine());
}

Expand Down Expand Up @@ -859,6 +859,14 @@ public function testBug7275(): void
$this->assertNoErrors($errors);
}

public function testBug7484(): void
{
$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());
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7484.php
@@ -0,0 +1,23 @@
<?php

namespace Bug7484;

/**
* @template K of int|string
* @template V
* @param iterable<K, V> $iterable
* @return iterable<K, V>
*/
function changeKeyCase(
iterable $iterable,
int $case = CASE_LOWER,
): iterable {
$callable = $case === CASE_LOWER ? 'strtolower' : 'strtoupper';
foreach ($iterable as $key => $value) {
if (is_string($key)) {
$key = $callable($key);
}

yield $key => $value;
}
}

0 comments on commit 4fda900

Please sign in to comment.