1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 16:59:45 +01:00

Allow adding Closure as a native property type

This commit is contained in:
Bruce Weirdan 2024-02-04 19:16:42 +01:00
parent e8b47f7d42
commit b2a2cd7884
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
3 changed files with 28 additions and 2 deletions

View File

@ -1650,7 +1650,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 // PHP does not support callable properties, but does allow Closure properties
// hasCallableType() treats Closure as a callable, but getCallableTypes() does not
&& $inferred_type->getCallableTypes() === []
; ;
$manipulator->setType( $manipulator->setType(

View File

@ -50,7 +50,8 @@ final class TClosure extends TNamedObject
public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{ {
return false; // it can, if it's just 'Closure'
return $this->params === null && $this->return_type === null && $this->is_pure === null;
} }
/** /**

View File

@ -330,6 +330,29 @@ class MissingPropertyTypeTest extends FileManipulationTestCase
'issues_to_fix' => ['MissingPropertyType'], 'issues_to_fix' => ['MissingPropertyType'],
'safe_types' => true, 'safe_types' => true,
], ],
'addClosurePropertyType' => [
'input' => <<<'PHP'
<?php
class A {
public $u;
public function __construct(Closure $u) {
$this->u = $u;
}
}
PHP,
'output' => <<<'PHP'
<?php
class A {
public Closure $u;
public function __construct(Closure $u) {
$this->u = $u;
}
}
PHP,
'php_version' => '7.4',
'issues_to_fix' => ['MissingPropertyType'],
'safe_types' => true,
],
]; ];
} }
} }