@use@use BoxTrait<int> on the use statement should
behave as if the trait were specialised to int. @extends /
@implements are measured in generics_extends_implements.
References:
- PHPStan phpdoc-types: @use on generic traits
- Psalm templated_annotations: @use / @template-use
- Phan Generic-Types: @use| Analyzer | Version | Result | Diagnostics |
|---|---|---|---|
| phan | 6.0.11Phan 6.0.11 php-ast version 1.1.3 PHP version used to run Phan: 8.5.12 | Unrecognized | generics_use.php:40: Class \Conformance\Tests\GenericsUse\IntHolder must substitute all 1 template parameters when inheriting \Conformance\Tests\GenericsUse\BoxTrait (found 0) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/generics_use.php:23 (use @extends or @inherit) [PhanGenericMissingParameters]
|
| phpstan | 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 | Enforced reported Lv.4+ | generics_use.php:44: Call to method Conformance\Tests\GenericsUse\IntHolder::wrap() on a separate line has no effect. [reported-from-level=4] generics_use.php:47: Call to method Conformance\Tests\GenericsUse\IntHolder::wrap() on a separate line has no effect. [reported-from-level=4] generics_use.php:47: Parameter #1 $value of method Conformance\Tests\GenericsUse\IntHolder::wrap() expects int, string given. [reported-from-level=5] With strict-rulesgenerics_use.php:44: Call to method Conformance\Tests\GenericsUse\IntHolder::wrap() on a separate line has no effect. generics_use.php:47: Call to method Conformance\Tests\GenericsUse\IntHolder::wrap() on a separate line has no effect. generics_use.php:47: Parameter #1 $value of method Conformance\Tests\GenericsUse\IntHolder::wrap() expects int, string given.
|
| phpstan-strict | 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 | Enforced | generics_use.php:44: Call to method Conformance\Tests\GenericsUse\IntHolder::wrap() on a separate line has no effect. generics_use.php:47: Call to method Conformance\Tests\GenericsUse\IntHolder::wrap() on a separate line has no effect. generics_use.php:47: Parameter #1 $value of method Conformance\Tests\GenericsUse\IntHolder::wrap() expects int, string given.
|
| psalm | 6.21.4Psalm 6.21.4@72023213487628df0bc5b2e02d68cbac614b7ef5 | Enforced (pzoom≠) | generics_use.php:27: The declared return type 'int' for Conformance\Tests\GenericsUse\BoxTrait::wrap is more specific than the inferred return type 'T:Conformance\Tests\GenericsUse\BoxTrait as mixed' [MixedReturnTypeCoercion] generics_use.php:47: Argument 1 of Conformance\Tests\GenericsUse\IntHolder::wrap expects int, but 'x' provided [InvalidArgument] pzoom (Psalm port)generics_use.php:47: Argument 1 of Conformance\Tests\GenericsUse\IntHolder::wrap expects int, but 'x' provided [InvalidArgument]
|
| psalm 7 | 7.21.0Psalm 7.21.0@1a665f0114bd40cf12ad57a1a82066549d92f1b6 | Enforced | generics_use.php:27: The declared return type 'int' for Conformance\Tests\GenericsUse\BoxTrait::wrap is more specific than the inferred return type 'T:Conformance\Tests\GenericsUse\BoxTrait as mixed' [MixedReturnTypeCoercion] generics_use.php:47: Argument 1 of Conformance\Tests\GenericsUse\IntHolder::wrap expects int, but 'x' provided [InvalidArgument]
|
| mago | 1.61.0mago 1.61.0 | Incidental (0/1) ⚠ 1 false positive | generics_use.php:48: Invalid argument type for argument #1 of `Conformance\Tests\GenericsUse\BoxTrait::wrap`: expected `int`, but found `string('x')`. [invalid-argument]
Expectation diffLine 48: Unexpected errors ["Invalid argument type for argument #1 of `Conformance\\Tests\\GenericsUse\\BoxTrait::wrap`: expected `int`, but found `string('x')`. [invalid-argument]"] |
| mir | 0.101.0mir 0.101.0 | Not enforced | No diagnostics reported.
|
| phpantom | 1.1.0phpantom_lsp 1.1.0 | Enforced | generics_use.php:47: Argument 1 ($value) expects int, got 'x' [type_mismatch_argument]
|
| pun | 0.4.3pun 0.4.3 | Enforced | generics_use.php:47: Parameter #1 $value of method Conformance\Tests\GenericsUse\IntHolder::wrap() expects int, string given. [argument.type]
|
| intelephense | 1.18.5intelephense 1.18.5 | Not enforced | No diagnostics reported.
|
| phpactor | 2026.11.22.0Phpactor 2026.11.22.0 | Not enforced | No diagnostics reported.
|
| phpy | 1.0.19566phpy 1.0.19566 | Enforced | generics_use.php:47: Argument '1' passed to wrap() is expected to be of type int, string given
|
| qodana | 263.7307.215PhpStorm 263.7307.215 | Enforced | generics_use.php:47: Parameter ''x'' type is not compatible with declaration when using strict type matching [PhpParamsInspection]
|
| noverify | 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 | Not enforced | generics_use.php:44: Expression evaluated but not used [discardExpr] generics_use.php:47: Expression evaluated but not used [discardExpr]
|
| steins | 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) | Not enforced | No diagnostics reported.
|
<?php
declare(strict_types=1);
namespace Conformance\Tests\GenericsUse;
/**
* Generic trait import via `@use`.
*
* A class that documents `@use BoxTrait<int>` on the `use` statement should
* behave as if the trait were specialised to `int`. `@extends` /
* `@implements` are measured in `generics_extends_implements`.
*
* References:
* - PHPStan phpdoc-types: `@use` on generic traits
* - Psalm templated_annotations: `@use` / `@template-use`
* - Phan Generic-Types: `@use`
*/
/**
* @template T
*/
trait BoxTrait
{
/**
* @param T $value
* @return T // E?[noise]
*/
public function wrap($value) // E?[noise]
{
return $value; // E?[noise]
}
}
final class IntHolder
{
/**
* @use BoxTrait<int>
*/
use BoxTrait; // T: @use
}
$holder = new IntHolder();
$holder->wrap(1); // V
// IntHolder is BoxTrait<int>, not BoxTrait<string>.
$holder->wrap('x'); // E?: @use BoxTrait<int> should reject string