Skip to content

Commit

Permalink
Prevent mixed|null when function param is mixed with a null default v…
Browse files Browse the repository at this point in the history
…alue
  • Loading branch information
robchett committed Feb 7, 2024
1 parent 4a5dbca commit 53c64a8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,9 @@ private function getTranslatedFunctionParam(
$this->codebase->analysis_php_version_id,
);

if ($is_nullable) {
if ($param_type->isMixed()) {
$is_nullable = true;
} elseif ($is_nullable) {
$param_type = $param_type->getBuilder()->addType(new TNull)->freeze();
} else {
$is_nullable = $param_type->isNullable();
Expand Down
1 change: 1 addition & 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
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 53c64a8

Please sign in to comment.