Skip to content

Commit

Permalink
Only inherit docblock param type if they type was not expanded
Browse files Browse the repository at this point in the history
  • Loading branch information
robchett committed Nov 9, 2023
1 parent 61f02d8 commit 44f9440
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ public static function checkMethodArgs(
$declaring_method_id = $class_storage->declaring_method_ids[$method_name];

$declaring_fq_class_name = $declaring_method_id->fq_class_name;
$declaring_method_name = $declaring_method_id->method_name;

if ($declaring_fq_class_name !== $fq_class_name) {
$declaring_class_storage = $codebase->classlike_storage_provider->get($declaring_fq_class_name);
Expand Down
7 changes: 7 additions & 0 deletions src/Psalm/Internal/Codebase/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,13 @@ public function getMethodParams(
foreach ($params as $i => $param) {
if (isset($overridden_storage->params[$i]->type)
&& $overridden_storage->params[$i]->has_docblock_type
&& (
! $param->type
|| $param->type->equals(
$overridden_storage->params[$i]->signature_type
?? $overridden_storage->params[$i]->type,
)
)
) {
$params[$i] = clone $param;
/** @var Union $params[$i]->type */
Expand Down
26 changes: 26 additions & 0 deletions tests/DocblockInheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ function takesF(F $f) : B {
return $f->map();
}',
],
'inheritCorrectParamOnTypeChange' => [
'code' => '<?php
class A
{
/** @param array<int, int>|int $className */
public function a(array|int $className): int
{
return 0;
}
}
class B extends A
{
public function a(array|int|bool $className): int
{
return 0;
}
}
print_r((new A)->a(1));
print_r((new B)->a(true));
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.0',
],
];
}

Expand Down

0 comments on commit 44f9440

Please sign in to comment.