← All results

Generic trait import via @use

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

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

Analyzer results

AnalyzerVersionResultDiagnostics
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]
  • Recognition: spelling not resolved — reported on declaration line(s) 40
  • Enforcement: 0/1 of the expected violations reported — incidental, since the spelling was not resolved
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-rules
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.
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
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.
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
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]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
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]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
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]
  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported — incidental, since values the type admits were also rejected on line(s) 48
  • False positives: line(s) 48
Expectation diff
Line 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.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
phpantom 1.1.0phpantom_lsp 1.1.0 Enforced
generics_use.php:47: Argument 1 ($value) expects int, got 'x' [type_mismatch_argument]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
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]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
intelephense 1.18.5intelephense 1.18.5 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
phpactor 2026.11.22.0Phpactor 2026.11.22.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations 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
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
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]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
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]
  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported

Source

<?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