-
-
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 730498c
Showing
14 changed files
with
291 additions
and
8 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
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
31 changes: 31 additions & 0 deletions
31
rules-tests/Php81/Rector/FunctionLike/IntersectionTypesRector/Fixture/some_class.php.inc
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,31 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
/** | ||
* @param string&int $types | ||
*/ | ||
public function process($types) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
/** | ||
* @param string&int $types | ||
*/ | ||
public function process(int&string $types) | ||
{ | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...s-tests/Php81/Rector/FunctionLike/IntersectionTypesRector/IntersectionTypesRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class IntersectionTypesRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
rules-tests/Php81/Rector/FunctionLike/IntersectionTypesRector/config/configured_rule.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Php81\Rector\FunctionLike\IntersectionTypesRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(IntersectionTypesRector::class); | ||
}; |
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
118 changes: 118 additions & 0 deletions
118
rules/Php81/Rector/FunctionLike/IntersectionTypesRector.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,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php81\Rector\FunctionLike; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrowFunction; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\FunctionLike; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PHPStan\Type\IntersectionType; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\Php81\Rector\FunctionLike\IntersectionTypesRector\IntersectionTypesRectorTest | ||
*/ | ||
final class IntersectionTypesRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Change docs to intersection types, where possible (properties are covered by TypedPropertyRector (@todo))', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
/** | ||
* @param string&int $types | ||
*/ | ||
public function process($types) | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function process(string&int $types) | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
|
||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [FunctionLike::class]; | ||
} | ||
|
||
/** | ||
* @param FunctionLike $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); | ||
if (! $phpDocInfo instanceof PhpDocInfo) { | ||
return null; | ||
} | ||
|
||
$this->refactorParamTypes($node, $phpDocInfo); | ||
// $this->refactorReturnType($node, $phpDocInfo); | ||
|
||
return $node; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::INTERSECTION_TYPES; | ||
} | ||
|
||
private function refactorParamTypes( | ||
ArrowFunction|Closure|ClassMethod|Function_ $functionLike, | ||
PhpDocInfo $phpDocInfo | ||
): void { | ||
foreach ($functionLike->params as $param) { | ||
if ($param->type !== null) { | ||
continue; | ||
} | ||
|
||
/** @var string $paramName */ | ||
$paramName = $this->getName($param->var); | ||
$paramType = $phpDocInfo->getParamType($paramName); | ||
|
||
if (! $paramType instanceof IntersectionType) { | ||
continue; | ||
} | ||
|
||
$phpParserIntersectionType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode( | ||
$paramType, | ||
TypeKind::PARAM() | ||
); | ||
|
||
if (! $phpParserIntersectionType instanceof Node\IntersectionType) { | ||
continue; | ||
} | ||
|
||
$param->type = $phpParserIntersectionType; | ||
} | ||
} | ||
} |
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.