Skip to content

Commit

Permalink
[PHPStanStaticTypeMapper] FQCN for Closure type mapper (#4584)
Browse files Browse the repository at this point in the history
* [PHPStanStaticTypeMapper] FQCN for Closure type mapper

* [PHPStanStaticTypeMapper] FQCN for Closure type mapper

* Fix

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* remove replace

* add auto import test

* revert auto import test

* skip already nullable closure var

* [ci-review] Rector Rectify

* rename fxture

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 23, 2023
1 parent 079cd51 commit 578d637
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\PhpDocParser\Ast\Node as AstNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ClosureType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;

Expand All @@ -30,7 +34,22 @@ public function getNodeClass(): string
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return $type->toPhpDocNode();
$typeNode = $type->toPhpDocNode();

$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->traverseWithCallable($typeNode, '', static function (AstNode $astNode) : ?FullyQualifiedIdentifierTypeNode {
if (! $astNode instanceof IdentifierTypeNode) {
return null;
}

if ($astNode->name !== 'Closure') {
return null;
}

return new FullyQualifiedIdentifierTypeNode('Closure');
});

return $typeNode;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNullableRector\Fixture;

final class FqcnVarClosureVarAnnotationMissingNull
{
/** @var \Closure(string, int, \Closure(float):int):string */
private ?\Closure $tooltipGenerator = null;
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNullableRector\Fixture;

final class FqcnVarClosureVarAnnotationMissingNull
{
/** @var null|\Closure(string, int, \Closure(float): int): string */
private ?\Closure $tooltipGenerator = null;
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNullableRector\Fixture;

use Closure;

final class SkipAlreadyNullableClosureVar
{
/** @var null|Closure(string, int, Closure(float):int):string */
private ?Closure $tooltipGenerator = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use Closure;

final class VarClosureAnnotationMissingNullPreferPrepend
{
/** @var null|Closure(string, int, Closure(float): int): string */
/** @var null|\Closure(string, int, \Closure(float): int): string */
private ?Closure $tooltipGenerator = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@ private function shouldSkipProperty(
}
}

if (! $isReadOnlyByPhpdoc) {
return true;
}

return false;
return ! $isReadOnlyByPhpdoc;
}
}

0 comments on commit 578d637

Please sign in to comment.