Skip to content

Commit

Permalink
[types] Add HasPropertyTypeMapepr and HasMethodTypeMapper (#1731)
Browse files Browse the repository at this point in the history
* Add HasPropertyTypeMapper

* add has Method type mapper
  • Loading branch information
TomasVotruba committed Jan 26, 2022
1 parent a26c59a commit 75c0b27
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build/target-repository/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.1|^8.0",
"phpstan/phpstan": "^1.4"
"phpstan/phpstan": "^1.4.2"
},
"autoload": {
"files": [
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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#'
Original file line number Diff line number Diff line change
@@ -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)
{
}
}

0 comments on commit 75c0b27

Please sign in to comment.