Skip to content

AssertIsTypeMethodCallRector crashes on 'isType' method call on any other object #9765

@Levivb

Description

@Levivb

Bug Report

Subject Details
Rector version 2.4.2
PHP version 8.5.2

$someObject->isType() inside a subclass of PHPUnit\Framework\TestCase crashes the AssertIsTypeMethodCallRector when an enum case is passed in.

Error:

[ERROR] Could not process
         "thefile.php" file, due to:
         "System error: "Cannot access offset of type App\Enums\SomeEnum in isset or empty"
         Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On line: 92

Minimal PHP Code Causing Issue

The violating file:

<?php

namespace Tests;

use App\Enums\SomeEnum;
use PHPUnit\Framework\TestCase;
use stdClass;

final class MyTest extends TestCase
{
    public function test_ing(): void
    {
        (new stdClass)->isType(SomeEnum::ONE);
    }
}

With enum:

<?php

namespace App\Enums;

enum SomeEnum: string
{
    case ONE = 'one';
}

Rector.php:

<?php

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;

return RectorConfig::configure()
    ->withRules([
        \Rector\PHPUnit\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector::class
    ]);

https://getrector.com/demo/ can't reproduce it since:

  1. The Enum should be in another namespace in another file (not sure why it doesn't crash when the enum is in the same file)
  2. The demo can't check if a class is a phpunit testcase

Expected Behaviour

Rector shouldn't crash and shouldn't have anything to do with this specific case since the isType method is not called on $this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions