Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHPStanStaticTypeMapper] FQCN for Closure type mapper #4584

Merged
merged 12 commits into from
Jul 23, 2023
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;
}
}