-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Downgrade] Add class method param to DowngradeEnumToConstantListClas…
…sRector (#2417) * add param enum downgrade * [ci-review] Rector Rectify Co-authored-by: GitHub Action <[email protected]>
- Loading branch information
1 parent
340f5b9
commit e368dab
Showing
10 changed files
with
332 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
.../Rector/Enum_/DowngradeEnumToConstantListClassRector/Fixture/no_type_param_method.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Fixture; | ||
|
||
use Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Source\AnythingYouWant; | ||
|
||
final class NoTypeParamMethod | ||
{ | ||
public function with(AnythingYouWant $anythingYouWant) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Fixture; | ||
|
||
use Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Source\AnythingYouWant; | ||
|
||
final class NoTypeParamMethod | ||
{ | ||
/** | ||
* @param \Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Source\AnythingYouWant::* $anythingYouWant | ||
*/ | ||
public function with($anythingYouWant) | ||
{ | ||
} | ||
} | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...adePhp80/Rector/Enum_/DowngradeEnumToConstantListClassRector/Fixture/param_method.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Fixture; | ||
|
||
use Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Source\GearValue; | ||
|
||
final class ParamMethod | ||
{ | ||
public function changeGear(GearValue $gearValue) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Fixture; | ||
|
||
use Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Source\GearValue; | ||
|
||
final class ParamMethod | ||
{ | ||
/** | ||
* @param \Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Source\GearValue::* $gearValue | ||
*/ | ||
public function changeGear(string $gearValue) | ||
{ | ||
} | ||
} | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
...gradePhp80/Rector/Enum_/DowngradeEnumToConstantListClassRector/Source/AnythingYouWant.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Source; | ||
|
||
enum AnythingYouWant | ||
{ | ||
const LEFT = 'left'; | ||
|
||
const TWO = 5; | ||
} |
11 changes: 11 additions & 0 deletions
11
...s/DowngradePhp80/Rector/Enum_/DowngradeEnumToConstantListClassRector/Source/GearValue.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Source; | ||
|
||
enum GearValue | ||
{ | ||
case FIRST; | ||
|
||
case SECOND; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp80\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Stmt\Enum_; | ||
use PhpParser\Node\Stmt\EnumCase; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Type\FloatType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use Rector\Core\Exception\ShouldNotHappenException; | ||
use Rector\Core\PhpParser\AstResolver; | ||
use Rector\NodeTypeResolver\NodeTypeResolver; | ||
|
||
final class EnumAnalyzer | ||
{ | ||
public function __construct( | ||
private readonly AstResolver $astResolver, | ||
private readonly NodeTypeResolver $nodeTypeResolver | ||
) { | ||
} | ||
|
||
public function resolveType(ClassReflection $classReflection): ?Identifier | ||
{ | ||
$class = $this->astResolver->resolveClassFromClassReflection($classReflection, $classReflection->getName()); | ||
if (! $class instanceof Enum_) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$scalarType = $class->scalarType; | ||
if ($scalarType instanceof Identifier) { | ||
// can be only int or string | ||
return $scalarType; | ||
} | ||
|
||
$enumExprTypes = $this->resolveEnumExprTypes($class); | ||
|
||
$enumExprTypeClasses = []; | ||
|
||
foreach ($enumExprTypes as $enumExprType) { | ||
$enumExprTypeClasses[] = $enumExprType::class; | ||
} | ||
|
||
$uniqueEnumExprTypeClasses = array_unique($enumExprTypeClasses); | ||
if (count($uniqueEnumExprTypeClasses) === 1) { | ||
$uniqueEnumExprTypeClass = $uniqueEnumExprTypeClasses[0]; | ||
if (is_a($uniqueEnumExprTypeClass, StringType::class, true)) { | ||
return new Identifier('string'); | ||
} | ||
|
||
if (is_a($uniqueEnumExprTypeClass, IntegerType::class, true)) { | ||
return new Identifier('int'); | ||
} | ||
|
||
if (is_a($uniqueEnumExprTypeClass, FloatType::class, true)) { | ||
return new Identifier('float'); | ||
} | ||
} | ||
|
||
// unknown or multiple types | ||
return null; | ||
} | ||
|
||
/** | ||
* @return Type[] | ||
*/ | ||
private function resolveEnumExprTypes(Enum_ $enum): array | ||
{ | ||
$enumExprTypes = []; | ||
|
||
foreach ($enum->stmts as $classStmt) { | ||
if (! $classStmt instanceof EnumCase) { | ||
continue; | ||
} | ||
|
||
$enumExprTypes[] = $this->resolveEnumCaseType($classStmt); | ||
} | ||
|
||
return $enumExprTypes; | ||
} | ||
|
||
private function resolveEnumCaseType(EnumCase $enumCase): Type | ||
{ | ||
$classExpr = $enumCase->expr; | ||
if ($classExpr instanceof Expr) { | ||
return $this->nodeTypeResolver->getType($classExpr); | ||
} | ||
|
||
// in case of no value, fallback to string type | ||
return new StringType(); | ||
} | ||
} |
Oops, something went wrong.