[types] Add HasPropertyTypeMapepr and HasMethodTypeMapper (#1731)

* Add HasPropertyTypeMapper

* add has Method type mapper
This commit is contained in:
Tomas Votruba 2022-01-26 12:52:30 +01:00 committed by GitHub
parent a26c59a7e9
commit 75c0b27d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 127 additions and 2 deletions

View File

@ -7,7 +7,7 @@
],
"require": {
"php": "^7.1|^8.0",
"phpstan/phpstan": "^1.4"
"phpstan/phpstan": "^1.4.2"
},
"autoload": {
"files": [

View File

@ -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",

View File

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\HasMethodType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
/**
* @implements TypeMapperInterface<HasMethodType>
*/
final class HasMethodTypeMapper implements TypeMapperInterface
{
public function __construct(
private readonly ObjectWithoutClassTypeMapper $objectWithoutClassTypeMapper
) {
}
/**
* @return class-string<Type>
*/
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);
}
}

View File

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
/**
* @implements TypeMapperInterface<HasPropertyType>
*/
final class HasPropertyTypeMapper implements TypeMapperInterface
{
public function __construct(
private readonly ObjectWithoutClassTypeMapper $objectWithoutClassTypeMapper
) {
}
/**
* @return class-string<Type>
*/
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);
}
}

View File

@ -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#'

View File

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;
final class SkipHasPropertyType
{
/**
* @param mixed $data
*/
public function run($data)
{
if (! isset($data->type)) {
return;
}
if ($data->type === 'value') {
$this->runData($data);
}
}
private function runData($data)
{
}
}