-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP 8.1] Add IntersectionTypesRector
- Loading branch information
1 parent
82de366
commit c432854
Showing
38 changed files
with
454 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/StaticTypeMapper/PhpDocParser/IntersectionTypeMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\StaticTypeMapper\PhpDocParser; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\NameScope; | ||
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\Type; | ||
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface; | ||
use Rector\StaticTypeMapper\PhpDoc\PhpDocTypeMapper; | ||
use Symfony\Contracts\Service\Attribute\Required; | ||
|
||
final class IntersectionTypeMapper implements PhpDocTypeMapperInterface | ||
{ | ||
private PhpDocTypeMapper $phpDocTypeMapper; | ||
|
||
/** | ||
* @return class-string<TypeNode> | ||
*/ | ||
public function getNodeType(): string | ||
{ | ||
return IntersectionTypeNode::class; | ||
} | ||
|
||
#[Required] | ||
public function autowireUnionTypeMapper(PhpDocTypeMapper $phpDocTypeMapper): void | ||
{ | ||
$this->phpDocTypeMapper = $phpDocTypeMapper; | ||
} | ||
|
||
/** | ||
* @param IntersectionTypeNode $typeNode | ||
*/ | ||
public function mapToPHPStanType(TypeNode $typeNode, Node $node, NameScope $nameScope): Type | ||
{ | ||
$intersectionedTypes = []; | ||
foreach ($typeNode->types as $intersectionedTypeNode) { | ||
$intersectionedTypes[] = $this->phpDocTypeMapper->mapToPHPStanType( | ||
$intersectionedTypeNode, | ||
$node, | ||
$nameScope | ||
); | ||
} | ||
|
||
return new IntersectionType($intersectionedTypes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/StaticTypeMapper/PhpParser/IntersectionTypeNodeMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\StaticTypeMapper\PhpParser; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\Type; | ||
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface; | ||
use Rector\StaticTypeMapper\Mapper\PhpParserNodeMapper; | ||
use Symfony\Contracts\Service\Attribute\Required; | ||
|
||
/** | ||
* @implements PhpParserNodeMapperInterface<Node\IntersectionType> | ||
*/ | ||
final class IntersectionTypeNodeMapper implements PhpParserNodeMapperInterface | ||
{ | ||
private PhpParserNodeMapper $phpParserNodeMapper; | ||
|
||
#[Required] | ||
public function autowireUnionTypeNodeMapper(PhpParserNodeMapper $phpParserNodeMapper): void | ||
{ | ||
$this->phpParserNodeMapper = $phpParserNodeMapper; | ||
} | ||
|
||
/** | ||
* @return class-string<Node> | ||
*/ | ||
public function getNodeType(): string | ||
{ | ||
return Node\IntersectionType::class; | ||
} | ||
|
||
/** | ||
* @param Node\IntersectionType $node | ||
*/ | ||
public function mapToPHPStan(Node $node): Type | ||
{ | ||
$types = []; | ||
foreach ($node->types as $intersectionedType) { | ||
$types[] = $this->phpParserNodeMapper->mapToPHPStanType($intersectionedType); | ||
} | ||
|
||
return new IntersectionType($types); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.