Skip to content

Commit

Permalink
Add test fixture and fix for AnnotationToAttributeRector (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainallanot committed Jun 4, 2021
1 parent 4812b85 commit d34b846
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function getSilentValue()

/**
* Useful for attributes
* @return array<string, mixed>
* @return array<int|string, mixed>
*/
public function getValuesWithExplicitSilentAndWithoutQuotes(): array
{
Expand All @@ -148,7 +148,7 @@ public function getValuesWithExplicitSilentAndWithoutQuotes(): array
if (is_int($key) && $this->silentKey !== null) {
$explicitKeysValues[$this->silentKey] = $valueWithoutQuotes;
} else {
$explicitKeysValues[$key] = $valueWithoutQuotes;
$explicitKeysValues[$this->removeQuotes($key)] = $valueWithoutQuotes;
}
}

Expand All @@ -161,6 +161,10 @@ public function getValuesWithExplicitSilentAndWithoutQuotes(): array
*/
protected function removeQuotes($value)
{
if (\is_array($value)) {
return $this->removeQuotesFromArray($value);
}

if (! is_string($value)) {
return $value;
}
Expand All @@ -173,6 +177,22 @@ protected function removeQuotes($value)
return $matches['content'];
}

/**
* @param mixed[] $values
* @return array<int|string, mixed>
*/
protected function removeQuotesFromArray(array $values): array
{
$unquotedArray = [];
foreach ($values as $key => $value) {
$unquotedKey = $this->removeQuotes($key);
$unquotedValue = $this->removeQuotes($value);
$unquotedArray[$unquotedKey] = $unquotedValue;
}

return $unquotedArray;
}

/**
* @param mixed[] $values
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(collectionOperations={}, itemOperations={
* "get",
* "get_by_isbn"={"method"="GET", "path"="/books/by_isbn/{isbn}.{_format}", "requirements"={"isbn"=".+"}, "identifiers"="isbn"}
* })
*/
class ApiPlatformResource
{
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;

use ApiPlatform\Core\Annotation\ApiResource;

#[\ApiPlatform\Core\Annotation\ApiResource(collectionOperations: [], itemOperations: ['get', 'get_by_isbn' => ['method' => 'GET', 'path' => '/books/by_isbn/{isbn}.{_format}', 'requirements' => ['isbn' => '.+'], 'identifiers' => 'isbn']])]
class ApiPlatformResource
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

final class PostController
{
#[\Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter('post', class: 'Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Post', options: ['"id"' => 'post'])]
#[\Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter('post', class: 'Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Post', options: ['id' => 'post'])]
public function __invoke(Site $site, Post $post)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
'Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter',
'Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter'
),
new AnnotationToAttribute(
'ApiPlatform\Core\Annotation\ApiResource',
'ApiPlatform\Core\Annotation\ApiResource'
),
]),
]]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function configure(array $configuration): void
}

/**
* @param array<string, mixed> $targetValues
* @param array<int|string, mixed> $targetValues
* @return ClassConstFetch[]
*/
private function resolveFlags(array $targetValues): array
Expand Down

0 comments on commit d34b846

Please sign in to comment.