|
5 | 5 | use BenSampo\Enum\Enum; |
6 | 6 | use BenSampo\Enum\Tests\Enums\UserType; |
7 | 7 | use Illuminate\Support\Arr; |
| 8 | +use Illuminate\Support\Enumerable; |
8 | 9 | use PhpParser\Node; |
9 | 10 | use PhpParser\Node\Arg; |
10 | 11 | use PhpParser\Node\Expr; |
@@ -565,7 +566,9 @@ protected function refactorMaybeMagicStaticCall(StaticCall $call): ?Node |
565 | 566 | */ |
566 | 567 | protected function refactorIsOrIsNot(MethodCall|NullsafeMethodCall $call, bool $is): ?Node |
567 | 568 | { |
568 | | - $comparison = $is ? Identical::class : NotIdentical::class; |
| 569 | + $comparison = $is |
| 570 | + ? Identical::class |
| 571 | + : NotIdentical::class; |
569 | 572 |
|
570 | 573 | if ($call->isFirstClassCallable()) { |
571 | 574 | $param = new Variable('value'); |
@@ -601,19 +604,38 @@ protected function refactorInOrNotIn(MethodCall|NullsafeMethodCall $call, bool $ |
601 | 604 | { |
602 | 605 | $args = $call->args; |
603 | 606 | if (isset($args[0]) && $args[0] instanceof Arg) { |
604 | | - $needle = new Arg($call->var); |
605 | | - $haystack = $args[0]; |
| 607 | + $enumArg = new Arg($call->var); |
| 608 | + $valuesArg = $args[0]; |
606 | 609 |
|
607 | | - $haystackValue = $haystack->value; |
608 | | - if ($haystackValue instanceof Array_) { |
609 | | - foreach ($haystackValue->items as $item) { |
| 610 | + $valuesValue = $valuesArg->value; |
| 611 | + if ($valuesValue instanceof Array_) { |
| 612 | + foreach ($valuesValue->items as $item) { |
610 | 613 | $item->setAttribute(self::COMPARED_AGAINST_ENUM_INSTANCE, true); |
611 | 614 | } |
612 | 615 | } |
613 | 616 |
|
| 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 | + |
| 627 | + $haystackArg = $this->getType($valuesValue)->isArray()->yes() |
| 628 | + ? $valuesArg |
| 629 | + : new Arg( |
| 630 | + new FuncCall( |
| 631 | + new Name('iterator_to_array'), |
| 632 | + [$valuesArg], |
| 633 | + ), |
| 634 | + ); |
| 635 | + |
614 | 636 | $inArray = new FuncCall( |
615 | 637 | new Name('in_array'), |
616 | | - [$needle, $haystack], |
| 638 | + [$enumArg, $haystackArg], |
617 | 639 | [self::COMPARED_AGAINST_ENUM_INSTANCE => true], |
618 | 640 | ); |
619 | 641 |
|
|
0 commit comments