mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 03:40:50 +00:00
[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:
parent
e913b61221
commit
6f61a09243
|
@ -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];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture;
|
||||
|
||||
final class SkipEmptyStringKey
|
||||
{
|
||||
public function run(string $name)
|
||||
{
|
||||
return ['' => $name];
|
||||
}
|
||||
}
|
|
@ -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];
|
||||
}
|
||||
}
|
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user