From f79e86c0d693d27363202f983250fc2745400b7d Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Wed, 16 Aug 2023 16:59:28 +0200 Subject: [PATCH 1/3] Convert `asArray()` to native enum automatically --- CHANGELOG.md | 6 ++++ src/Rector/ToNativeUsagesRector.php | 49 +++++++++++++++++++++++++++++ tests/Rector/Usages/asArray.php.inc | 14 +++++++++ 3 files changed, 69 insertions(+) create mode 100644 tests/Rector/Usages/asArray.php.inc 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; +}, []); From fe9b614888b54e754d8eec5142939687f9ea5f58 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Thu, 17 Aug 2023 12:17:14 +0200 Subject: [PATCH 2/3] Improve CI --- .github/workflows/continuous-integration.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 5ef63e2d..98c92d7d 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -9,11 +9,10 @@ jobs: 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 From be5fdab02f52d51b3b12016b57820b22b71f2436 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Thu, 17 Aug 2023 12:19:54 +0200 Subject: [PATCH 3/3] ubuntu --- .github/workflows/continuous-integration.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 98c92d7d..08aff60a 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -5,7 +5,7 @@ on: jobs: tests: - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest strategy: fail-fast: false matrix: