Skip to content

Commit

Permalink
Merge branch '1.4.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 10, 2022
2 parents c0c9175 + 28776ff commit 9048226
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PhpDoc/TypeNodeResolver.php
Expand Up @@ -384,7 +384,7 @@ private function resolveThisTypeNode(ThisTypeNode $typeNode, NameScope $nameScop

private function resolveNullableTypeNode(NullableTypeNode $typeNode, NameScope $nameScope): Type
{
return TypeCombinator::addNull($this->resolve($typeNode->type, $nameScope));
return TypeCombinator::union($this->resolve($typeNode->type, $nameScope), new NullType());
}

private function resolveUnionTypeNode(UnionTypeNode $typeNode, NameScope $nameScope): Type
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -786,6 +786,10 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6591.php');
}

if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6790.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6584.php');
}

Expand Down
66 changes: 66 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6790.php
@@ -0,0 +1,66 @@
<?php declare(strict_types = 1); // lint >= 8.0

namespace Bug6790;

use function PHPStan\Testing\assertType;

/**
* @template T
*/
class Repository
{
/**
* @param array<T> $items
*/
public function __construct(private array $items) {}

/**
* @return ?T
*/
public function find(string $id)
{
return $this->items[$id] ?? null;
}
}

/**
* @template T
*/
class Repository2
{
/**
* @param array<T> $items
*/
public function __construct(private array $items) {}

/**
* @return T|null
*/
public function find(string $id)
{
return $this->items[$id] ?? null;
}
}

class Foo
{

/**
* @param Repository<string> $r
* @return void
*/
public function doFoo(Repository $r): void
{
assertType('string|null', $r->find('foo'));
}

/**
* @param Repository2<string> $r
* @return void
*/
public function doFoo2(Repository2 $r): void
{
assertType('string|null', $r->find('foo'));
}

}

0 comments on commit 9048226

Please sign in to comment.