mago icon indicating copy to clipboard operation
mago copied to clipboard

Analyzer doesn't support using `class-string` type with interfaces

Open ADmad opened this issue 3 months ago • 0 comments

Example code:

<?php
interface MyInterface
{
    public function myMethod(string $param): int;
}

/**
 * @param class-string<MyInterface> $classname
 */
function myFunction(string $classname): MyInterface
{
    return new $classname();
}

Mago output:

 mago analyse test.php
error[never-return]: Cannot return value with type 'never' from this function.
   ┌─ test.php:12:12
   │
12 │     return new $classname();
   │     -------^^^^^^^^^^^^^^^^-
   │     │      │
   │     │      This expression has type 'never'.
   │     This return statement is effectively unreachable.
   │
   = A 'never' return type indicates that a function is guaranteed to exit the script, throw an exception, or loop indefinitely. Code following a call to such a function is unreachable.
   = Help: Since the preceding expression never returns, this 'return' statement cannot be reached. You can likely remove the 'return' keyword entirely.

error: found 1 issues: 1 error(s)

Expectation

No error should be reported. phpstan supports above usage.

Mago version: 1.0.0-rc.2

ADmad avatar Nov 15 '25 12:11 ADmad