Skip to content

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

Closed
rectorphp/rector-phpunit
#674

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.

Activity

  1. TomasVotruba commented on May 21, 2026

    @TomasVotruba
    Member

    Fix, thanks for reporting 👍

  2. Levivb commented on May 21, 2026

    @Levivb
    Author

    that speed though 😳 Thank you for this insane quick fix!

    I do wonder however, why does this Rector rule process the isType method here? The method is not called on a TestCase object, so there's no assertion to replace.

    Right now the following still happens:

    - (new stdClass)->isType('array');
    + (new stdClass)->isArray();

    but rector has no business in changing this method call. It's on an instance of a class type which should be ignored
    new stdClass is obviously just for simplicity in this bugticket ;)

  3. TomasVotruba commented on May 21, 2026

    @TomasVotruba
    Member

    I'll check it

  4. TomasVotruba commented on May 21, 2026

    @TomasVotruba
    Member

    This should do : ) rectorphp/rector-phpunit#675

  5. Levivb commented on May 21, 2026

    @Levivb
    Author

    hah awesome! yea that looks nice! ❤️

    indeed 2 bugs in one 😬 probably should've been more clear about that 😅

  6. TomasVotruba commented on May 21, 2026

    @TomasVotruba
    Member

    No worries, the 2nd was hidding under 1st bigger one 😆 . Impossible to see at the start 👍

    Keep up trying Rector out and reporting 🙏

  7. github-actions commented on Oct 25, 2026

    @github-actions
    Contributor

    This issue has been automatically locked because it has been closed for 150 days. Please open a new issue if you have a similar problem.

  8. locked as resolved and limited conversation to collaborators on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions