Skip to content

Commit

Permalink
Fix conversion of in() and notIn() to native enums when called wi…
Browse files Browse the repository at this point in the history
…th non-arrays
  • Loading branch information
spawnia committed Aug 21, 2024
1 parent 0f82b99 commit bc94b6a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- Fix conversion of `in()` and `notIn()` to native enums when called with non-arrays

## 6.11.0

### Added
Expand Down
21 changes: 15 additions & 6 deletions src/Rector/ToNativeUsagesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,19 +601,28 @@ protected function refactorInOrNotIn(MethodCall|NullsafeMethodCall $call, bool $
{
$args = $call->args;
if (isset($args[0]) && $args[0] instanceof Arg) {
$needle = new Arg($call->var);
$haystack = $args[0];
$needleArg = new Arg($call->var);
$valuesArg = $args[0];

$haystackValue = $haystack->value;
if ($haystackValue instanceof Array_) {
foreach ($haystackValue->items as $item) {
$valuesValue = $valuesArg->value;
if ($valuesValue instanceof Array_) {
foreach ($valuesValue->items as $item) {
$item->setAttribute(self::COMPARED_AGAINST_ENUM_INSTANCE, true);
}
}

$haystackArg = $this->getType($valuesValue)->isArray()->yes()
? $valuesArg
: new Arg(
new FuncCall(
new Name('iterator_to_array'),
[$valuesArg],
),
);

$inArray = new FuncCall(
new Name('in_array'),
[$needle, $haystack],
[$needleArg, $haystackArg],
[self::COMPARED_AGAINST_ENUM_INSTANCE => true],
);

Expand Down
4 changes: 4 additions & 0 deletions tests/EnumAnnotateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace BenSampo\Enum\Tests;

use Illuminate\Filesystem\Filesystem;
use PHPUnit\Framework\Attributes\DataProvider;

final class EnumAnnotateCommandTest extends ApplicationTestCase
{
/** @dataProvider classes */
#[DataProvider('classes')]

Check failure on line 11 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 11 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8 - highest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 11 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.1 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 11 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.2 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 11 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.3 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.
public function testAnnotateClass(string $class): void
{
$filesystem = $this->filesystem();
Expand All @@ -19,6 +21,7 @@ public function testAnnotateClass(string $class): void
}

/** @dataProvider classes */
#[DataProvider('classes')]

Check failure on line 24 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 24 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8 - highest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 24 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.1 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 24 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.2 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 24 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.3 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.
public function testAnnotateClassAlreadyAnnotated(string $class): void
{
$filesystem = $this->filesystem();
Expand Down Expand Up @@ -48,6 +51,7 @@ public static function sources(): iterable
}

/** @dataProvider sources */
#[DataProvider('sources')]

Check failure on line 54 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 54 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8 - highest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 54 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.1 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 54 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.2 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 54 in tests/EnumAnnotateCommandTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.3 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.
public function testAnnotateFolder(string $source): void
{
$filesystem = $this->filesystem();
Expand Down
2 changes: 2 additions & 0 deletions tests/Rector/ToNativeImplementationRectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace BenSampo\Enum\Tests\Rector;

use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/** @see \BenSampo\Enum\Rector\ToNativeImplementationRector */
final class ToNativeImplementationRectorTest extends AbstractRectorTestCase
{
/** @dataProvider provideData */
#[DataProvider('provideData')]

Check failure on line 12 in tests/Rector/ToNativeImplementationRectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 12 in tests/Rector/ToNativeImplementationRectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8 - highest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 12 in tests/Rector/ToNativeImplementationRectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.1 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 12 in tests/Rector/ToNativeImplementationRectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.2 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 12 in tests/Rector/ToNativeImplementationRectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.3 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.
public function test(string $filePath): void
{
$this->doTestFile($filePath);
Expand Down
2 changes: 2 additions & 0 deletions tests/Rector/ToNativeUsagesRectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace BenSampo\Enum\Tests\Rector;

use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/** @see \BenSampo\Enum\Rector\ToNativeUsagesRector */
final class ToNativeUsagesRectorTest extends AbstractRectorTestCase
{
/** @dataProvider provideData */
#[DataProvider('provideData')]

Check failure on line 12 in tests/Rector/ToNativeUsagesRectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 12 in tests/Rector/ToNativeUsagesRectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8 - highest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 12 in tests/Rector/ToNativeUsagesRectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.1 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 12 in tests/Rector/ToNativeUsagesRectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.2 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.

Check failure on line 12 in tests/Rector/ToNativeUsagesRectorTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis - P8.3 - lowest

Attribute class PHPUnit\Framework\Attributes\DataProvider does not exist.
public function test(string $filePath): void
{
$this->doTestFile($filePath);
Expand Down
4 changes: 4 additions & 0 deletions tests/Rector/Usages/in.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use BenSampo\Enum\Tests\Enums\UserType;
/** @var UserType $userType */
$userType->in([UserType::Administrator, UserType::Subscriber(), null]);
$userType?->in([UserType::Administrator, $userType, null]);
/** @var iterable<UserType> $userTypes */
$userType->in($userTypes);
-----
<?php

Expand All @@ -13,3 +15,5 @@ use BenSampo\Enum\Tests\Enums\UserType;
/** @var UserType $userType */
in_array($userType, [UserType::Administrator, UserType::Subscriber, null]);
in_array($userType, [UserType::Administrator, $userType, null]);
/** @var iterable<UserType> $userTypes */
in_array($userType, iterator_to_array($userTypes));
4 changes: 4 additions & 0 deletions tests/Rector/Usages/notIn.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use BenSampo\Enum\Tests\Enums\UserType;
/** @var UserType $userType */
$userType->notIn([UserType::Administrator]);
$userType?->notIn([UserType::Administrator, $userType]);
/** @var iterable<UserType> $userTypes */
$userType->notIn($userTypes);
-----
<?php

Expand All @@ -13,3 +15,5 @@ use BenSampo\Enum\Tests\Enums\UserType;
/** @var UserType $userType */
!in_array($userType, [UserType::Administrator]);
!in_array($userType, [UserType::Administrator, $userType]);
/** @var iterable<UserType> $userTypes */
!in_array($userType, iterator_to_array($userTypes));

0 comments on commit bc94b6a

Please sign in to comment.