-
-
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.
[TypeDeclaration] Add ArrayShapeFromConstantArrayReturnRector (#1908)
- Loading branch information
1 parent
55a13c0
commit 8057e33
Showing
33 changed files
with
446 additions
and
29 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
57 changes: 57 additions & 0 deletions
57
packages/PHPStanStaticTypeMapper/TypeMapper/ArrayShapeTypeMapper.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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPStanStaticTypeMapper\TypeMapper; | ||
|
||
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode; | ||
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\NeverType; | ||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper; | ||
|
||
final class ArrayShapeTypeMapper | ||
{ | ||
public function __construct( | ||
private readonly PHPStanStaticTypeMapper $phpStanStaticTypeMapper | ||
) { | ||
} | ||
|
||
public function mapConstantArrayType(ConstantArrayType $constantArrayType): ArrayShapeNode|ArrayType|null | ||
{ | ||
// empty array | ||
if ($constantArrayType->getKeyType() instanceof NeverType) { | ||
return new ArrayType(new MixedType(), new MixedType()); | ||
} | ||
|
||
$arrayShapeItemNodes = []; | ||
|
||
foreach ($constantArrayType->getKeyTypes() as $index => $keyType) { | ||
// not real array shape | ||
if (! $keyType instanceof ConstantStringType) { | ||
return null; | ||
} | ||
|
||
$keyDocTypeNode = new IdentifierTypeNode($keyType->getValue()); | ||
$valueType = $constantArrayType->getValueTypes()[$index]; | ||
|
||
$valueDocTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode( | ||
$valueType, | ||
TypeKind::RETURN() | ||
); | ||
|
||
$arrayShapeItemNodes[] = new ArrayShapeItemNode( | ||
$keyDocTypeNode, | ||
$constantArrayType->isOptionalKey($index), | ||
$valueDocTypeNode | ||
); | ||
} | ||
|
||
return new ArrayShapeNode($arrayShapeItemNodes); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
...d/ArrayShapeFromConstantArrayReturnRector/ArrayShapeFromConstantArrayReturnRectorTest.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\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ArrayShapeFromConstantArrayReturnRectorTest 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'; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...tor/ClassMethod/ArrayShapeFromConstantArrayReturnRector/Fixture/include_constants.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,34 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture; | ||
|
||
final class IncludeConstants | ||
{ | ||
public const NAME = 'name'; | ||
|
||
public function run(string $name) | ||
{ | ||
return [self::NAME => $name]; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture; | ||
|
||
final class IncludeConstants | ||
{ | ||
public const NAME = 'name'; | ||
|
||
/** | ||
* @return array{name: string} | ||
*/ | ||
public function run(string $name) | ||
{ | ||
return [self::NAME => $name]; | ||
} | ||
} | ||
|
||
?> |
43 changes: 43 additions & 0 deletions
43
...Method/ArrayShapeFromConstantArrayReturnRector/Fixture/include_external_constants.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,43 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture; | ||
|
||
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Source\ExternalConstant; | ||
|
||
final class IncludeExternalConstants | ||
{ | ||
/** | ||
* @return array<string, string|int> | ||
*/ | ||
public function run(int $line, string $file): array | ||
{ | ||
return [ | ||
ExternalConstant::LINE => $line, | ||
ExternalConstant::FILE => $file, | ||
]; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture; | ||
|
||
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Source\ExternalConstant; | ||
|
||
final class IncludeExternalConstants | ||
{ | ||
/** | ||
* @return array{line: int, file: string} | ||
*/ | ||
public function run(int $line, string $file): array | ||
{ | ||
return [ | ||
ExternalConstant::LINE => $line, | ||
ExternalConstant::FILE => $file, | ||
]; | ||
} | ||
} | ||
|
||
?> |
Oops, something went wrong.