Skip to content

Commit

Permalink
Empty is also the bottom type
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Feb 8, 2020
1 parent b26deb4 commit b439a57
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/TypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ public static function isAtomicContainedBy(
return true;
}

if ($input_type_part instanceof TNever) {
if ($input_type_part instanceof TNever || $input_type_part instanceof Type\Atomic\TEmpty) {
return true;
}

Expand Down
118 changes: 115 additions & 3 deletions tests/Template/ClassTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,49 @@ public function __construct(string $type) {
}
}'
],
'newGenericBecomesPropertyType' => [
'createEmptyArrayCollection' => [
'<?php
$a = new ArrayCollection([]);
/**
* @psalm-template TKey of array-key
* @psalm-template T
*/
class ArrayCollection
{
/**
* An array containing the entries of this collection.
*
* @psalm-var array<TKey,T>
* @var array
*/
private $elements = [];
/**
* Initializes a new ArrayCollection.
*
* @param array $elements
*
* @psalm-param array<TKey,T> $elements
*/
public function __construct(array $elements = [])
{
$this->elements = $elements;
}
/**
* @param TKey $key
* @param T $t
*/
public function add($key, $t) : void {
$this->elements[$key] = $t;
}
}',
[
'$a' => 'ArrayCollection<empty, empty>'
]
],
'newGenericBecomesPropertyTypeValidArg' => [
'<?php
class B {}
Expand All @@ -2289,7 +2331,7 @@ public function __construct() {
}
/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
class ArrayCollection
Expand Down Expand Up @@ -2323,6 +2365,76 @@ public function add($key, $t) : void {
}
}'
],
'allowPropertyCoercion' => [
'<?php
class Test
{
/**
* @var ArrayCollection<int, DateTime>
*/
private $c;
public function __construct()
{
$this->c = new ArrayCollection();
$this->c->filter(function (DateTime $dt): bool {
return $dt === $dt;
});
}
}
/**
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Collection
{
/**
* @param Closure $p
*
* @return Collection A
*
* @psalm-param Closure(T=):bool $p
* @psalm-return Collection<TKey, T>
*/
public function filter(Closure $p);
}
/**
* @psalm-template TKey of array-key
* @psalm-template T
* @template-implements Collection<TKey,T>
*/
class ArrayCollection implements Collection
{
/**
* @psalm-var array<TKey,T>
* @var array
*/
private $elements;
/**
* @param array $elements
*
* @psalm-param array<TKey,T> $elements
*/
public function __construct(array $elements = [])
{
$this->elements = $elements;
}
/**
* {@inheritDoc}
*
* @return static
*/
public function filter(Closure $p)
{
return $this;
}
}'
],
];
}

Expand Down Expand Up @@ -2770,7 +2882,7 @@ public static function foo($t) : void {}
}',
'error_message' => 'UndefinedDocblockClass'
],
'newGenericBecomesPropertyType' => [
'newGenericBecomesPropertyTypeInvalidArg' => [
'<?php
class B {}
class C {}
Expand Down

0 comments on commit b439a57

Please sign in to comment.