Skip to content

Commit 9445db9

Browse files
committed
improve collection handling
1 parent dbe6357 commit 9445db9

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/Rector/ToNativeUsagesRector.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BenSampo\Enum\Enum;
66
use BenSampo\Enum\Tests\Enums\UserType;
77
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Enumerable;
89
use PhpParser\Node;
910
use PhpParser\Node\Arg;
1011
use PhpParser\Node\Expr;
@@ -565,7 +566,9 @@ protected function refactorMaybeMagicStaticCall(StaticCall $call): ?Node
565566
*/
566567
protected function refactorIsOrIsNot(MethodCall|NullsafeMethodCall $call, bool $is): ?Node
567568
{
568-
$comparison = $is ? Identical::class : NotIdentical::class;
569+
$comparison = $is
570+
? Identical::class
571+
: NotIdentical::class;
569572

570573
if ($call->isFirstClassCallable()) {
571574
$param = new Variable('value');
@@ -601,7 +604,7 @@ protected function refactorInOrNotIn(MethodCall|NullsafeMethodCall $call, bool $
601604
{
602605
$args = $call->args;
603606
if (isset($args[0]) && $args[0] instanceof Arg) {
604-
$needleArg = new Arg($call->var);
607+
$enumArg = new Arg($call->var);
605608
$valuesArg = $args[0];
606609

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

617+
if ($this->isObjectType($valuesValue, new ObjectType(Enumerable::class))) {
618+
return new MethodCall(
619+
$valuesValue,
620+
new Identifier($in
621+
? 'contains'
622+
: 'doesntContain'),
623+
[$enumArg],
624+
);
625+
}
626+
614627
$haystackArg = $this->getType($valuesValue)->isArray()->yes()
615628
? $valuesArg
616629
: new Arg(
@@ -622,7 +635,7 @@ protected function refactorInOrNotIn(MethodCall|NullsafeMethodCall $call, bool $
622635

623636
$inArray = new FuncCall(
624637
new Name('in_array'),
625-
[$needleArg, $haystackArg],
638+
[$enumArg, $haystackArg],
626639
[self::COMPARED_AGAINST_ENUM_INSTANCE => true],
627640
);
628641

tests/Rector/Usages/in.php.inc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use BenSampo\Enum\Tests\Enums\UserType;
55
/** @var UserType $userType */
66
$userType->in([UserType::Administrator, UserType::Subscriber(), null]);
77
$userType?->in([UserType::Administrator, $userType, null]);
8-
/** @var iterable<UserType> $userTypes */
9-
$userType->in($userTypes);
8+
/** @var iterable<UserType> $iterable */
9+
$userType->in($iterable);
10+
/** @var \Illuminate\Support\Collection $collection */
11+
$userType->in($collection);
1012
-----
1113
<?php
1214

@@ -15,5 +17,7 @@ use BenSampo\Enum\Tests\Enums\UserType;
1517
/** @var UserType $userType */
1618
in_array($userType, [UserType::Administrator, UserType::Subscriber, null]);
1719
in_array($userType, [UserType::Administrator, $userType, null]);
18-
/** @var iterable<UserType> $userTypes */
19-
in_array($userType, iterator_to_array($userTypes));
20+
/** @var iterable<UserType> $iterable */
21+
in_array($userType, iterator_to_array($iterable));
22+
/** @var \Illuminate\Support\Collection $collection */
23+
$collection->contains($userType);

tests/Rector/Usages/notIn.php.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ $userType->notIn([UserType::Administrator]);
77
$userType?->notIn([UserType::Administrator, $userType]);
88
/** @var iterable<UserType> $userTypes */
99
$userType->notIn($userTypes);
10+
/** @var \Illuminate\Support\Collection $collection */
11+
$userType->notIn($collection);
1012
-----
1113
<?php
1214

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

0 commit comments

Comments
 (0)