1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Do not add callable as a native property type

It's invalid in all PHP versions: https://3v4l.org/bXWo2

Also see php.net/manual/en/language.types.declarations.php#language.types.declarations.base.function

Fixes vimeo/psalm#10650
This commit is contained in:
Bruce Weirdan 2024-02-04 18:52:20 +01:00
parent 59df6a728f
commit e8b47f7d42
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 39 additions and 1 deletions

View File

@ -1649,7 +1649,9 @@ final class ClassAnalyzer extends ClassLikeAnalyzer
$allow_native_type = !$docblock_only $allow_native_type = !$docblock_only
&& $codebase->analysis_php_version_id >= 7_04_00 && $codebase->analysis_php_version_id >= 7_04_00
&& $codebase->allow_backwards_incompatible_changes; && $codebase->allow_backwards_incompatible_changes
&& !$inferred_type->hasCallableType() // PHP does not support callable properties
;
$manipulator->setType( $manipulator->setType(
$allow_native_type $allow_native_type

View File

@ -294,6 +294,42 @@ class MissingPropertyTypeTest extends FileManipulationTestCase
'issues_to_fix' => ['MissingPropertyType'], 'issues_to_fix' => ['MissingPropertyType'],
'safe_types' => true, 'safe_types' => true,
], ],
'doNotAddCallablePropertyTypes' => [
'input' => <<<'PHP'
<?php
class A {
public $u;
public $v;
public function __construct(?callable $u, callable $v) {
$this->u = $u;
$this->v = $v;
}
}
PHP,
'output' => <<<'PHP'
<?php
class A {
/**
* @var callable|null
*/
public $u;
/**
* @var callable
*/
public $v;
public function __construct(?callable $u, callable $v) {
$this->u = $u;
$this->v = $v;
}
}
PHP,
'php_version' => '7.4',
'issues_to_fix' => ['MissingPropertyType'],
'safe_types' => true,
],
]; ];
} }
} }