From 75c0b27d37ee34844b0eb5faf35742c6631d5ab2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 26 Jan 2022 12:52:30 +0100 Subject: [PATCH] [types] Add HasPropertyTypeMapepr and HasMethodTypeMapper (#1731) * Add HasPropertyTypeMapper * add has Method type mapper --- build/target-repository/composer.json | 2 +- composer.json | 2 +- .../TypeMapper/HasMethodTypeMapper.php | 48 +++++++++++++++++++ .../TypeMapper/HasPropertyTypeMapper.php | 48 +++++++++++++++++++ phpstan.neon | 3 ++ .../Fixture/skip_has_property_type.php.inc | 26 ++++++++++ 6 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 packages/PHPStanStaticTypeMapper/TypeMapper/HasMethodTypeMapper.php create mode 100644 packages/PHPStanStaticTypeMapper/TypeMapper/HasPropertyTypeMapper.php create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/Fixture/skip_has_property_type.php.inc diff --git a/build/target-repository/composer.json b/build/target-repository/composer.json index 6b52d3545dc..57df7acc4c8 100644 --- a/build/target-repository/composer.json +++ b/build/target-repository/composer.json @@ -7,7 +7,7 @@ ], "require": { "php": "^7.1|^8.0", - "phpstan/phpstan": "^1.4" + "phpstan/phpstan": "^1.4.2" }, "autoload": { "files": [ diff --git a/composer.json b/composer.json index e303d72cfde..6607444b5b4 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "nette/utils": "^3.2", "nikic/php-parser": "^4.13.2", "phpstan/phpdoc-parser": "^1.2", - "phpstan/phpstan": "^1.4", + "phpstan/phpstan": "^1.4.2", "phpstan/phpstan-phpunit": "^1.0", "psr/log": "^2.0", "react/child-process": "^0.6.4", diff --git a/packages/PHPStanStaticTypeMapper/TypeMapper/HasMethodTypeMapper.php b/packages/PHPStanStaticTypeMapper/TypeMapper/HasMethodTypeMapper.php new file mode 100644 index 00000000000..1c12aa3671d --- /dev/null +++ b/packages/PHPStanStaticTypeMapper/TypeMapper/HasMethodTypeMapper.php @@ -0,0 +1,48 @@ + + */ +final class HasMethodTypeMapper implements TypeMapperInterface +{ + public function __construct( + private readonly ObjectWithoutClassTypeMapper $objectWithoutClassTypeMapper + ) { + } + + /** + * @return class-string + */ + public function getNodeClass(): string + { + return HasMethodType::class; + } + + /** + * @param HasMethodType $type + */ + public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode + { + return new IdentifierTypeNode('object'); + } + + /** + * @param HasMethodType $type + */ + public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node + { + return $this->objectWithoutClassTypeMapper->mapToPhpParserNode($type, $typeKind); + } +} diff --git a/packages/PHPStanStaticTypeMapper/TypeMapper/HasPropertyTypeMapper.php b/packages/PHPStanStaticTypeMapper/TypeMapper/HasPropertyTypeMapper.php new file mode 100644 index 00000000000..cd87073bdca --- /dev/null +++ b/packages/PHPStanStaticTypeMapper/TypeMapper/HasPropertyTypeMapper.php @@ -0,0 +1,48 @@ + + */ +final class HasPropertyTypeMapper implements TypeMapperInterface +{ + public function __construct( + private readonly ObjectWithoutClassTypeMapper $objectWithoutClassTypeMapper + ) { + } + + /** + * @return class-string + */ + public function getNodeClass(): string + { + return HasPropertyType::class; + } + + /** + * @param HasPropertyType $type + */ + public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode + { + return new IdentifierTypeNode('object'); + } + + /** + * @param HasPropertyType $type + */ + public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node + { + return $this->objectWithoutClassTypeMapper->mapToPhpParserNode($type, $typeKind); + } +} diff --git a/phpstan.neon b/phpstan.neon index d09a87943fb..6a9a124e785 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -612,3 +612,6 @@ parameters: - path: src/Bootstrap/ExtensionConfigResolver.php message: '#Offset (.*?)includes(.*?) always exists and is not nullable#' + + # mapper re-use + - '#Parameter \#1 \$type of method Rector\\PHPStanStaticTypeMapper\\TypeMapper\\ObjectWithoutClassTypeMapper\:\:mapToPhpParserNode\(\) expects PHPStan\\Type\\ObjectWithoutClassType, PHPStan\\Type\\Accessory\\Has(Property|Method)Type given#' diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/Fixture/skip_has_property_type.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/Fixture/skip_has_property_type.php.inc new file mode 100644 index 00000000000..a3ceb5af64c --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/Fixture/skip_has_property_type.php.inc @@ -0,0 +1,26 @@ +type)) { + return; + } + + if ($data->type === 'value') { + $this->runData($data); + } + } + + private function runData($data) + { + } +}