Skip to content

Commit 29d203f

Browse files
TomasVotrubaclaude
andauthored
fix(AssertIsTypeMethodCallRector): guard against non-string arg crashing isset on enum case (#674)
When `isType()` is called with an enum case argument, `ValueResolver::getValue()` returns an enum object. Using it as an array key in `isset()` caused a fatal error: "Cannot access offset of type SomeEnum in isset or empty". Add `is_string()` guard before the array lookup so non-string args are safely skipped. Fixes rectorphp/rector#9765 Co-authored-by: Claude <noreply@anthropic.com>
1 parent d4fd79d commit 29d203f

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
// skip: non-string argument (enum case) should not crash
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
enum SkipNonStringArgEnum
10+
{
11+
case ONE;
12+
}
13+
14+
final class SkipNonStringArg extends TestCase
15+
{
16+
public function testMethod(): void
17+
{
18+
$this->assertThat([], $this->isType(SkipNonStringArgEnum::ONE));
19+
}
20+
}

rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public function refactor(Node $node): Node|null
114114
}
115115

116116
$argValue = $this->valueResolver->getValue($arg);
117+
if (! is_string($argValue)) {
118+
return null;
119+
}
120+
117121
if (isset(self::IS_TYPE_VALUE_TO_METHOD[$argValue])) {
118122
if ($node instanceof MethodCall) {
119123
return new MethodCall($node->var, self::IS_TYPE_VALUE_TO_METHOD[$argValue]);

0 commit comments

Comments
 (0)