Skip to content

Commit

Permalink
[PHP 8.0] Add always class to AnnotationToAttribute to include string…
Browse files Browse the repository at this point in the history
… to ::class reference conversion (#5619)

* cleanup

* add fixture

* try without corrections

* remove unused ExprParameterReflectionTypeCorrector, handle in expected way
  • Loading branch information
TomasVotruba committed Feb 14, 2024
1 parent 435d125 commit 2ff0f08
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 191 deletions.
Expand Up @@ -3,9 +3,9 @@
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Doctrine;

/**
* @\Doctrine\ORM\Mapping\Embeddable
* @\Doctrine\ORM\Mapping\Entity(repositoryClass="App\Some\Class")
*/
class PreserveIntKeyDefined
class DoctrineEntity
{
}

Expand All @@ -15,8 +15,8 @@ class PreserveIntKeyDefined

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

#[\Doctrine\ORM\Mapping\Embeddable]
class PreserveIntKeyDefined
#[\Doctrine\ORM\Mapping\Entity(repositoryClass: \App\Some\Class::class)]
class DoctrineEntity
{
}

Expand Down
Expand Up @@ -5,6 +5,7 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\D
use Doctrine\ORM\Mapping as ORM;

/**
* @\Doctrine\ORM\Mapping\Embeddable
* @ORM\DiscriminatorMap({ 1 = "CostDetailEntity" })
*/
class PreserveIntKeyDefined
Expand All @@ -19,6 +20,7 @@ namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\D

use Doctrine\ORM\Mapping as ORM;

#[\Doctrine\ORM\Mapping\Embeddable]
#[ORM\DiscriminatorMap([1 => 'CostDetailEntity'])]
class PreserveIntKeyDefined
{
Expand Down

This file was deleted.

Expand Up @@ -11,11 +11,8 @@
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\FutureAttribute;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericSingleImplicitAnnotation;
use Rector\ValueObject\PhpVersionFeature;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->phpVersion(PhpVersionFeature::ATTRIBUTES);

$rectorConfig
->ruleWithConfiguration(AnnotationToAttributeRector::class, [
new AnnotationToAttribute('Doctrine\ORM\Mapping\Embeddable'),
Expand All @@ -27,21 +24,17 @@
new AnnotationToAttribute(GenericAnnotation::class),
new AnnotationToAttribute(GenericSingleImplicitAnnotation::class),

new AnnotationToAttribute('inject', 'Nette\DI\Attributes\Inject'),
new AnnotationToAttribute('Symfony\Component\Routing\Annotation\Route'),

// doctrine
new AnnotationToAttribute('Doctrine\ORM\Mapping\Entity'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\ManyToMany'),
new AnnotationToAttribute('Doctrine\ORM\Mapping\Entity', null, ['repositoryClass']),
new AnnotationToAttribute('Doctrine\ORM\Mapping\DiscriminatorMap'),

// validation
new AnnotationToAttribute('Symfony\Component\Validator\Constraints\All'),
new AnnotationToAttribute('Symfony\Component\Validator\Constraints\Choice'),
new AnnotationToAttribute('Symfony\Component\Validator\Constraints\Length'),

// JMS + Symfony
new AnnotationToAttribute('JMS\Serializer\Annotation\AccessType'),
new AnnotationToAttribute('Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter'),

// test for alias used
Expand Down
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\Php80\Rector\Switch_\ChangeSwitchToMatchRector\Fixture;

final class MultipleCases
{
public function run($value)
{
switch ($value) {
case 'v1':
case 'v2':
case 'v3':
return 100;
default:
return 1000;
}
}
}

?>
17 changes: 16 additions & 1 deletion rules/Php80/ValueObject/AnnotationToAttribute.php
Expand Up @@ -6,18 +6,25 @@

use Rector\Php80\Contract\ValueObject\AnnotationToAttributeInterface;
use Rector\Validation\RectorAssert;
use Webmozart\Assert\Assert;

final readonly class AnnotationToAttribute implements AnnotationToAttributeInterface
{
/**
* @param string[] $classReferenceFields
*/
public function __construct(
private string $tag,
private ?string $attributeClass = null
private ?string $attributeClass = null,
private array $classReferenceFields = []
) {
RectorAssert::className($tag);

if (is_string($attributeClass)) {
RectorAssert::className($attributeClass);
}

Assert::allString($classReferenceFields);
}

public function getTag(): string
Expand All @@ -33,4 +40,12 @@ public function getAttributeClass(): string

return $this->attributeClass;
}

/**
* @return string[]
*/
public function getClassReferenceFields(): array
{
return $this->classReferenceFields;
}
}
4 changes: 2 additions & 2 deletions src/PhpAttribute/AnnotationToAttributeMapper.php
Expand Up @@ -30,9 +30,9 @@ public function __construct(
}

/**
* @return Expr|DocTagNodeState::REMOVE_ARRAY
* @return mixed|DocTagNodeState::REMOVE_ARRAY
*/
public function map(mixed $value): Expr|string
public function map(mixed $value): mixed
{
foreach ($this->annotationToAttributeMappers as $annotationToAttributeMapper) {
if ($annotationToAttributeMapper->isCandidate($value)) {
Expand Down
3 changes: 2 additions & 1 deletion src/PhpAttribute/AttributeArrayNameInliner.php
Expand Up @@ -18,9 +18,10 @@ final class AttributeArrayNameInliner
{
/**
* @param Array_|Arg[] $array
* @param string[] $classReferenceFields
* @return Arg[]
*/
public function inlineArrayToArgs(Array_|array $array): array
public function inlineArrayToArgs(Array_|array $array, array $classReferenceFields = []): array
{
if (is_array($array)) {
return $this->inlineArray($array);
Expand Down
109 changes: 0 additions & 109 deletions src/PhpAttribute/NodeAnalyzer/ExprParameterReflectionTypeCorrector.php

This file was deleted.

0 comments on commit 2ff0f08

Please sign in to comment.