← All results

Generic inheritance via @extends / @implements

Group: PHPDoc generics · Category: phpdoc · File: generics_extends_implements.php

A subclass that documents @extends Box<int> should behave as Box<int> at call sites: accepted where Box<int> is required, rejected where Box<string> is required. References: - PHPStan generics-in-php-using-phpdocs - Psalm templated_annotations (@extends / @implements)

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 Pass
generics_extends_implements.php:61: Argument 1 ($box) is new IntBox(1) of type \Conformance\Tests\GenericsExtendsImplements\Box|\Conformance\Tests\GenericsExtendsImplements\Box<int>|\Conformance\Tests\GenericsExtendsImplements\IntBox but \Conformance\Tests\GenericsExtendsImplements\takesStringBox() takes \Conformance\Tests\GenericsExtendsImplements\Box<string> (real type \Conformance\Tests\GenericsExtendsImplements\Box) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/generics_extends_implements.php:54 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass reported Lv.5+
generics_extends_implements.php:61: Parameter #1 $box of function Conformance\Tests\GenericsExtendsImplements\takesStringBox expects Conformance\Tests\GenericsExtendsImplements\Box<string>, Conformance\Tests\GenericsExtendsImplements\IntBox given. [identifier=argument.type] [reported-from-level=5]
With strict-rules
generics_extends_implements.php:61: Parameter #1 $box of function Conformance\Tests\GenericsExtendsImplements\takesStringBox expects Conformance\Tests\GenericsExtendsImplements\Box<string>, Conformance\Tests\GenericsExtendsImplements\IntBox given. [identifier=argument.type]
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
Pass
generics_extends_implements.php:61: Argument 1 of Conformance\Tests\GenericsExtendsImplements\takesStringBox expects Conformance\Tests\GenericsExtendsImplements\Box<string>, but Conformance\Tests\GenericsExtendsImplements\IntBox provided [InvalidArgument]
mago 1.46.0mago 1.46.0 Pass
generics_extends_implements.php:61: Invalid argument type for argument #1 of `Conformance\Tests\GenericsExtendsImplements\takesStringBox`: expected `Conformance\Tests\GenericsExtendsImplements\Box<string>`, but found `Conformance\Tests\GenericsExtendsImplements\IntBox`. [invalid-argument]
mir 0.70.1mir 0.70.1 Pass
generics_extends_implements.php:61: InvalidArgument: Argument $box of takesStringBox() expects 'Conformance\Tests\GenericsExtendsImplements\Box<string>', got 'Conformance\Tests\GenericsExtendsImplements\IntBox<int>' [MIR0201]
phpantom 0.9.0phpantom_lsp 0.9.0 Fail

No diagnostics reported.

Expectation diff
Line 61: Expected 1 error(s)
intelephense 1.18.5intelephense 1.18.5 Fail

No diagnostics reported.

Expectation diff
Line 61: Expected 1 error(s)
phpy 0.2.0phpy 0.2.0 Fail

No diagnostics reported.

Expectation diff
Line 61: Expected 1 error(s)
qodana 262.8665.325Qodana 262.8665.325 Fail

No diagnostics reported.

Expectation diff
Line 61: Expected 1 error(s)
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Fail
generics_extends_implements.php:28: param $value miss matched with phpdoc type <<T>> [funcParamTypeMissMatch]
generics_extends_implements.php:47: param $box miss matched with phpdoc type <<Box<int>>> [funcParamTypeMissMatch]
generics_extends_implements.php:54: param $box miss matched with phpdoc type <<Box<string>>> [funcParamTypeMissMatch]
Expectation diff
Line 61: Expected 1 error(s)
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Pass
generics_extends_implements.php:61: argument new IntBox() to takesStringBox() violates declared @param Box<string> $box — declared contract violation [phpdoc.param-mismatch]
phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass
generics_extends_implements.php:61: Parameter #1 $box of function Conformance\Tests\GenericsExtendsImplements\takesStringBox expects Conformance\Tests\GenericsExtendsImplements\Box<string>, Conformance\Tests\GenericsExtendsImplements\IntBox given. [identifier=argument.type]
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 Pass
generics_extends_implements.php:61: Argument 1 of Conformance\Tests\GenericsExtendsImplements\takesStringBox expects Conformance\Tests\GenericsExtendsImplements\Box<string>, but Conformance\Tests\GenericsExtendsImplements\IntBox provided [InvalidArgument]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\GenericsExtendsImplements;

/**
 * Generic inheritance via `@extends` / `@implements`.
 *
 * A subclass that documents `@extends Box<int>` should behave as `Box<int>`
 * at call sites: accepted where `Box<int>` is required, rejected where
 * `Box<string>` is required.
 *
 * References:
 * - PHPStan generics-in-php-using-phpdocs
 * - Psalm templated_annotations (@extends / @implements)
 */

/**
 * @template T
 */
class Box
{
    /**
     * @param T $value
     */
    public function __construct(
        public mixed $value, // E<noverify>: NoVerify does not understand class-level template parameters
    ) {
    }
}

/**
 * @extends Box<int>
 */
final class IntBox extends Box
{
    public function __construct(int $value)
    {
        parent::__construct($value);
    }
}

/**
 * @param Box<int> $box
 */
function takesIntBox(Box $box): void // E<noverify>: NoVerify does not accept Box<int> parameter annotations
{
}

/**
 * @param Box<string> $box
 */
function takesStringBox(Box $box): void // E<noverify>: NoVerify does not accept Box<string> parameter annotations
{
}

takesIntBox(new IntBox(1));

// IntBox is Box<int>, not Box<string>.
takesStringBox(new IntBox(1)); // E: Box<int> subclass should not satisfy Box<string>