From 0a7485367e423470a0efe1995ec7b635a86166aa Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Mon, 30 Dec 2019 22:17:28 +0100 Subject: [PATCH 1/2] [TypeDeclaration] Add support for PhpStan's class-string type in PropertyTypeDeclarationRector. --- .../NodeTypeResolver/src/StaticTypeMapper.php | 5 +++++ .../Fixture/phpstan_class_string_type.php.inc | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_class_string_type.php.inc diff --git a/packages/NodeTypeResolver/src/StaticTypeMapper.php b/packages/NodeTypeResolver/src/StaticTypeMapper.php index d88882f7b4fb..d1c036b1a861 100644 --- a/packages/NodeTypeResolver/src/StaticTypeMapper.php +++ b/packages/NodeTypeResolver/src/StaticTypeMapper.php @@ -28,6 +28,7 @@ use PHPStan\Type\ArrayType; use PHPStan\Type\BooleanType; use PHPStan\Type\CallableType; +use PHPStan\Type\ClassStringType; use PHPStan\Type\ClosureType; use PHPStan\Type\ConstantType; use PHPStan\Type\FloatType; @@ -116,6 +117,10 @@ public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType): TypeNo return new IdentifierTypeNode('int'); } + if ($phpStanType instanceof ClassStringType) { + return new IdentifierTypeNode('class-string'); + } + if ($phpStanType instanceof StringType) { return new IdentifierTypeNode('string'); } diff --git a/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_class_string_type.php.inc b/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_class_string_type.php.inc new file mode 100644 index 000000000000..6a365da9353f --- /dev/null +++ b/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_class_string_type.php.inc @@ -0,0 +1,21 @@ +someFQCN = $someFQCN; + } +} From 2c3724bf015c7da768ee3216a916095818560be6 Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Mon, 30 Dec 2019 22:34:58 +0100 Subject: [PATCH 2/2] Add PHPStan Generic test too. --- .../Fixture/phpstan_class_string_type.php.inc | 2 -- .../Fixture/phpstan_generics_type.php.inc | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_generics_type.php.inc diff --git a/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_class_string_type.php.inc b/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_class_string_type.php.inc index 6a365da9353f..f2622da16869 100644 --- a/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_class_string_type.php.inc +++ b/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_class_string_type.php.inc @@ -2,8 +2,6 @@ namespace Rector\TypeDeclaration\Tests\Rector\FunctionLike\PropertyTypeDeclarationRector\Fixture; -use Doctrine\Common\Collections\ArrayCollection; - class PhpstanClassStringType { /** diff --git a/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_generics_type.php.inc b/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_generics_type.php.inc new file mode 100644 index 000000000000..50e7a92fbf7b --- /dev/null +++ b/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/phpstan_generics_type.php.inc @@ -0,0 +1,16 @@ + + */ + private $twoOfMe; + + public function __construct() + { + $this->twoOfMe = new \ArrayIterator([new self(), new self()]); + } +}