Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert asArray() to native enum automatically #336

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions src/Rector/ToNativeUsagesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
{
Expand Down
14 changes: 14 additions & 0 deletions tests/Rector/Usages/asArray.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use BenSampo\Enum\Tests\Enums\UserType;

UserType::asArray();
-----
<?php

use BenSampo\Enum\Tests\Enums\UserType;

array_reduce(UserType::cases(), static function (array $result, UserType $userType) {
$result[$userType->name] = $userType->value;
return $result;
}, []);