1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-03 10:07:52 +01:00

Merge pull request #10654 from weirdan/10650-do-not-add-callable-as-native-property-type

This commit is contained in:
Bruce Weirdan 2024-02-04 15:32:59 -04:00 committed by GitHub
commit 9ac2383efd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 2 deletions

View File

@ -1649,7 +1649,11 @@ final class ClassAnalyzer extends ClassLikeAnalyzer
$allow_native_type = !$docblock_only
&& $codebase->analysis_php_version_id >= 7_04_00
&& $codebase->allow_backwards_incompatible_changes;
&& $codebase->allow_backwards_incompatible_changes
// 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(
$allow_native_type

View File

@ -50,7 +50,8 @@ final class TClosure extends TNamedObject
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

@ -294,6 +294,65 @@ class MissingPropertyTypeTest extends FileManipulationTestCase
'issues_to_fix' => ['MissingPropertyType'],
'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,
],
'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,
],
];
}
}