Skip to content

Commit

Permalink
Merge pull request #10654 from weirdan/10650-do-not-add-callable-as-n…
Browse files Browse the repository at this point in the history
…ative-property-type
  • Loading branch information
weirdan committed Feb 4, 2024
2 parents 59df6a7 + b2a2cd7 commit 9ac2383
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,11 @@ private static function addOrUpdatePropertyType(

$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
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Type/Atomic/TClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function __construct(

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;
}

/**
Expand Down
59 changes: 59 additions & 0 deletions tests/FileManipulation/MissingPropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,65 @@ public function bar() {
'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,
],
];
}
}

0 comments on commit 9ac2383

Please sign in to comment.