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:
commit
9ac2383efd
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user