Skip to content

Commit

Permalink
[TypeDeclaration] Skip key has bracket {}[] on ArrayShapeFromConstant…
Browse files Browse the repository at this point in the history
…ArrayReturnRector (#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
  • Loading branch information
samsonasik committed Mar 27, 2022
1 parent e913b61 commit 6f61a09
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture;

final class SkipEmptyStringKey
{
public function run(string $name)
{
return ['' => $name];
}
}
Original file line number Diff line number Diff line change
@@ -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];
}
}
Original file line number Diff line number Diff line change
@@ -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];
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand All @@ -29,9 +30,10 @@
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,
Expand Down Expand Up @@ -148,10 +150,14 @@ private function shouldSkipKeyType(Type $keyType): bool
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;
}
}

Expand Down

0 comments on commit 6f61a09

Please sign in to comment.