diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 5ef63e2d..08aff60a 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -5,15 +5,14 @@ on: jobs: tests: - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest strategy: fail-fast: false matrix: - os: [ubuntu-latest] - php: [8.0, 8.1] + php: [8.0, 8.1, 8.2] dependency-versions: [lowest, highest] - name: Tests - P${{ matrix.php }} - ${{ matrix.dependency-versions }} - ${{ matrix.os }} + name: Tests - PHP ${{ matrix.php }} - ${{ matrix.dependency-versions }} dependencies steps: - uses: actions/checkout@v2 @@ -41,10 +40,10 @@ jobs: strategy: fail-fast: false matrix: - php: [8.0, 8.1] + php: [8.0, 8.1, 8.2] dependency-versions: [lowest, highest] - name: Static Analysis - P${{ matrix.php }} - ${{ matrix.dependency-versions }} + name: Static Analysis - PHP ${{ matrix.php }} - ${{ matrix.dependency-versions }} dependencies steps: - uses: actions/checkout@v2 diff --git a/CHANGELOG.md b/CHANGELOG.md index f24b3093..b2eedc23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## 6.7.0 + +### Added + +- Convert `asArray()` to native enum automatically + ## 6.6.2 ### Fixed diff --git a/src/Rector/ToNativeUsagesRector.php b/src/Rector/ToNativeUsagesRector.php index 9761c7ea..35fd1282 100644 --- a/src/Rector/ToNativeUsagesRector.php +++ b/src/Rector/ToNativeUsagesRector.php @@ -25,6 +25,7 @@ use PhpParser\Node\Expr\Cast; use PhpParser\Node\Expr\Cast\String_; use PhpParser\Node\Expr\ClassConstFetch; +use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Match_; use PhpParser\Node\Expr\MethodCall; @@ -43,6 +44,7 @@ use PhpParser\Node\Scalar\EncapsedStringPart; use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt\Case_; +use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\VariadicPlaceholder; @@ -181,6 +183,10 @@ public function refactor(Node $node): ?Node return $this->refactorGetValues($node); } + if ($this->isName($node->name, 'asArray')) { + return $this->refactorAsArray($node); + } + if ($this->isName($node->name, 'getRandomInstance')) { return $this->refactorGetRandomInstance($node); } @@ -390,6 +396,49 @@ protected function refactorGetValues(StaticCall $node): ?Node return null; } + /** @see Enum::asArray() */ + protected function refactorAsArray(StaticCall $node): ?Node + { + $class = $node->class; + if ($class instanceof Name) { + $args = $node->args; + if ($args === []) { + $resultVariable = new Variable('result'); + + $itemName = lcfirst($class->getLast()); + $itemVariable = new Variable($itemName); + + return new FuncCall( + new Name('array_reduce'), + [ + new Arg( + new StaticCall($class, 'cases') + ), + new Arg( + new Closure([ + 'static' => true, + 'params' => [ + new Param($resultVariable, null, 'array'), + new Param($itemVariable, null, $class), + ], + 'stmts' => [ + new Expression(new Assign( + new ArrayDimFetch($resultVariable, new PropertyFetch($itemVariable, 'name')), + new PropertyFetch($itemVariable, 'value'), + )), + new Return_($resultVariable), + ], + ]) + ), + new Arg(new Array_()), + ], + ); + } + } + + return null; + } + /** @see Enum::getRandomInstance() */ protected function refactorGetRandomInstance(StaticCall $staticCall): ?Node { diff --git a/tests/Rector/Usages/asArray.php.inc b/tests/Rector/Usages/asArray.php.inc new file mode 100644 index 00000000..6cd8b598 --- /dev/null +++ b/tests/Rector/Usages/asArray.php.inc @@ -0,0 +1,14 @@ +name] = $userType->value; + return $result; +}, []);