Skip to content

Commit

Permalink
improve collection handling
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Aug 21, 2024
1 parent dbe6357 commit 9445db9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
19 changes: 16 additions & 3 deletions src/Rector/ToNativeUsagesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BenSampo\Enum\Enum;
use BenSampo\Enum\Tests\Enums\UserType;
use Illuminate\Support\Arr;
use Illuminate\Support\Enumerable;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
Expand Down Expand Up @@ -565,7 +566,9 @@ protected function refactorMaybeMagicStaticCall(StaticCall $call): ?Node
*/
protected function refactorIsOrIsNot(MethodCall|NullsafeMethodCall $call, bool $is): ?Node
{
$comparison = $is ? Identical::class : NotIdentical::class;
$comparison = $is
? Identical::class
: NotIdentical::class;

if ($call->isFirstClassCallable()) {
$param = new Variable('value');
Expand Down Expand Up @@ -601,7 +604,7 @@ protected function refactorInOrNotIn(MethodCall|NullsafeMethodCall $call, bool $
{
$args = $call->args;
if (isset($args[0]) && $args[0] instanceof Arg) {
$needleArg = new Arg($call->var);
$enumArg = new Arg($call->var);
$valuesArg = $args[0];

$valuesValue = $valuesArg->value;
Expand All @@ -611,6 +614,16 @@ protected function refactorInOrNotIn(MethodCall|NullsafeMethodCall $call, bool $
}
}

if ($this->isObjectType($valuesValue, new ObjectType(Enumerable::class))) {
return new MethodCall(
$valuesValue,
new Identifier($in
? 'contains'
: 'doesntContain'),
[$enumArg],
);
}

$haystackArg = $this->getType($valuesValue)->isArray()->yes()
? $valuesArg
: new Arg(
Expand All @@ -622,7 +635,7 @@ protected function refactorInOrNotIn(MethodCall|NullsafeMethodCall $call, bool $

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

Expand Down
12 changes: 8 additions & 4 deletions tests/Rector/Usages/in.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ 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);
/** @var iterable<UserType> $iterable */
$userType->in($iterable);
/** @var \Illuminate\Support\Collection $collection */
$userType->in($collection);
-----
<?php

Expand All @@ -15,5 +17,7 @@ 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));
/** @var iterable<UserType> $iterable */
in_array($userType, iterator_to_array($iterable));
/** @var \Illuminate\Support\Collection $collection */
$collection->contains($userType);
4 changes: 4 additions & 0 deletions tests/Rector/Usages/notIn.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ $userType->notIn([UserType::Administrator]);
$userType?->notIn([UserType::Administrator, $userType]);
/** @var iterable<UserType> $userTypes */
$userType->notIn($userTypes);
/** @var \Illuminate\Support\Collection $collection */
$userType->notIn($collection);
-----
<?php

Expand All @@ -17,3 +19,5 @@ use BenSampo\Enum\Tests\Enums\UserType;
!in_array($userType, [UserType::Administrator, $userType]);
/** @var iterable<UserType> $userTypes */
!in_array($userType, iterator_to_array($userTypes));
/** @var \Illuminate\Support\Collection $collection */
$collection->doesntContain($userType);

0 comments on commit 9445db9

Please sign in to comment.