Skip to content

bug: assert(count($classTypes) === 1) fails when model type cannot be uniquely inferred #36

Open
@maniaba

Description

@maniaba

PHP Version

8.2

PHPStan CodeIgniter Version

v1.5.3

PHPStan Version

2.1.14

What happened?

When running phpstan analyze, an AssertionError is thrown from ModelFindReturnTypeExtension.php at line 68:
Uncaught AssertionError: assert(count($classTypes) === 1) in ModelFindReturnTypeExtension.php:68

This happens if PHPStan cannot uniquely determine the model class type (e.g. when $model is resolved dynamically or lacks a docblock). Instead of failing gracefully, the extension crashes due to a strict assert() condition.

Interestingly, the same pattern using model(SomeModel::class) works fine in other parts of the codebase, and PHPStan completes analysis without any issues there. This suggests that the bug may depend on context or surrounding code, rather than being a universal issue with model() calls.

Minimum Reproduction Script

class QueryGenerator extends BaseCommand
{
    public function run(array $params)
    {
        $model = model(\App\Models\SomethingModel::class); // dynamically resolved

        // This line triggers the crash during PHPStan analysis
        $rows = $model->save([...]);
    }
}

Note: The crash also occurs even if $model is declared without a docblock to help infer the type.

Expected Output

The extension should handle ambiguous or missing type inference gracefully — by returning MixedType or issuing a warning — rather than crashing with an AssertionError. This would allow analysis to continue without halting the entire process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions