[TypeDeclaration] Skip key has bracket {}[] on ArrayShapeFromConstantArrayReturnRector (#1968)

* [TypeDeclaration] Skip key has bracket {}[] on ArrayShapeFromConstantArrayReturnRector

* more test

* final touch: using \W regex

* add unicode support

* update url regex

* final touch: regex name

* final touch: skip empty string key

* really final touch: re-use variable
This commit is contained in:
Abdul Malik Ikhsan 2022-03-27 18:12:35 +07:00 committed by GitHub
parent e913b61221
commit 6f61a09243
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 6 deletions

View File

@ -0,0 +1,11 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture;
final class SkipBracketKeys
{
public function run(string $name)
{
return ['{someKey}' => $name, '[someKey]' => $name];
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture;
final class SkipEmptyStringKey
{
public function run(string $name)
{
return ['' => $name];
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture;
final class SkipKeyHasAt
{
public function run(string $name)
{
return ['some@key' => $name];
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture;
final class UnicodeKey
{
public function run(string $name)
{
return ['äÄ' => $name];
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture;
final class UnicodeKey
{
/**
* @return array{äÄ: string}
*/
public function run(string $name)
{
return ['äÄ' => $name];
}
}
?>

View File

@ -17,6 +17,7 @@ use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Symplify\Astral\TypeAnalyzer\ClassMethodReturnTypeResolver;
@ -29,9 +30,10 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class ArrayShapeFromConstantArrayReturnRector extends AbstractRector
{
/**
* @var string[]
* @see https://regex101.com/r/WvUD0m/2
* @var string
*/
private const SKIPPED_CHARS = [':', '@'];
private const SKIPPED_CHAR_REGEX = '#\W#u';
public function __construct(
private readonly ClassMethodReturnTypeResolver $classMethodReturnTypeResolver,
@ -148,10 +150,14 @@ CODE_SAMPLE
continue;
}
foreach (self::SKIPPED_CHARS as $skippedChar) {
if (str_contains($type->getValue(), $skippedChar)) {
return true;
}
$value = $type->getValue();
if (trim($value) === '') {
return true;
}
if (StringUtils::isMatch($value, self::SKIPPED_CHAR_REGEX)) {
return true;
}
}