Skip to content

Commit

Permalink
Prevent mixed|null
Browse files Browse the repository at this point in the history
  • Loading branch information
robchett committed Feb 7, 2024
1 parent 4a5dbca commit 0546788
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Psalm/Type/MutableUnion.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Psalm\Type\Atomic\TTemplateParamClass;
use Psalm\Type\Atomic\TTrue;

use function array_key_exists;
use function count;
use function get_class;
use function get_object_vars;
Expand Down Expand Up @@ -269,6 +270,12 @@ public function setTypes(array $types): self
*/
public function addType(Atomic $type): self
{
$mixed = new TMixed();
$mixedKey = $mixed->getKey();
if ($type->getKey() === $mixedKey || array_key_exists($mixedKey, $this->types)) {
$this->types = [$mixedKey => $mixed];
return $this;
}
$this->types[$type->getKey()] = $type;

if ($type instanceof TLiteralString) {
Expand Down
14 changes: 14 additions & 0 deletions tests/ArgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,20 @@ function foo($a, ...$b) {}
var_caller("foo");',
],
'mixedNullable' => [
'code' => '<?php
class A {
public function __construct(public mixed $default = null) {
}
}
$a = new A;
$_v = $a->default;',
'assertions' => [
'$_v===' => 'mixed',
],
'ignored_issues' => [],
'php_version' => '8.0',
],
];
}

Expand Down

0 comments on commit 0546788

Please sign in to comment.