Skip to content

Commit

Permalink
[PHP 8.1] Keep use stmts in MyCLabsClassToEnumRector enum (#2696)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 20, 2022
1 parent 963c48c commit 49a8343
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture;

use MyCLabs\Enum\Enum;
use Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Source\ComparingTrait;

final class KeepUsedTrait extends Enum
{
use ComparingTrait;

/**
* Some comment
*/
private const VIEW = 'view';

private const EDIT = 'edit';
}

?>
-----
<?php

namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture;

use MyCLabs\Enum\Enum;
use Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Source\ComparingTrait;

enum KeepUsedTrait : string
{
use ComparingTrait;
/**
* Some comment
*/
case VIEW = 'view';
case EDIT = 'edit';
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Source;

use MyCLabs\Enum\Enum;

trait ComparingTrait
{
/**
* @param Enum
*/
public function equalsEnum(self $other): bool
{
return $other->getValue() === $this->getValue();
}
}
2 changes: 2 additions & 0 deletions rules/Php81/NodeFactory/EnumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function createFromClass(Class_ $class): Enum_

$constants = $class->getConstants();

$enum->stmts = $class->getTraitUses();

if ($constants !== []) {
$value = $this->valueResolver->getValue($constants[0]->consts[0]->value);
$enum->scalarType = is_string($value)
Expand Down

0 comments on commit 49a8343

Please sign in to comment.