Skip to content

Commit

Permalink
Allow adding Closure as a native property type
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Feb 4, 2024
1 parent e8b47f7 commit b2a2cd7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,9 @@ private static function addOrUpdatePropertyType(
$allow_native_type = !$docblock_only
&& $codebase->analysis_php_version_id >= 7_04_00
&& $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(
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
23 changes: 23 additions & 0 deletions tests/FileManipulation/MissingPropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,29 @@ public function __construct(?callable $u, callable $v) {
'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 b2a2cd7

Please sign in to comment.