← All results

trait-string

Group: Advanced PHPDoc types · Category: ecosystem · File: phpdoc_advanced_fallback_trait_string.php

A class-string narrowed to the name of a trait. Traits are the odd member of the family: the name exists at compile time and ::class resolves it, but it denotes nothing instantiable, so an analyzer that checks "is this a known class" rather than "is this a known trait" lets an ordinary class name through. References: - PHPStan TypeNodeResolver trait-string resolves to ClassStringType

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.10Phan 6.0.10 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Unrecognized
phpdoc_advanced_fallback_trait_string.php:30: Saw a token Phan may have failed to parse after '* @return trait-string': after trait, saw '-' [PhanUnextractableAnnotationSuffix]
phpdoc_advanced_fallback_trait_string.php:32: Return type of returnsTraitString() is undeclared type \Conformance\Tests\PhpdocAdvancedFallbackTraitString\trait [PhanUndeclaredTypeReturnType]
phpdoc_advanced_fallback_trait_string.php:34: Returning SomeTrait::class of type string but returnsTraitString() is declared to return \Conformance\Tests\PhpdocAdvancedFallbackTraitString\trait (no real type) (the inferred real return type has nothing in common with the declared phpdoc return type) [PhanTypeMismatchReturnProbablyReal]
phpdoc_advanced_fallback_trait_string.php:42: Saw possibly unextractable annotation for a fragment of comment '* @param trait-string $value': after trait, did not see an element name (will guess based on comment order) [PhanUnextractableAnnotationElementName]
phpdoc_advanced_fallback_trait_string.php:42: Saw a token Phan may have failed to parse after '* @param trait-string $value': after trait, saw '-' [PhanUnextractableAnnotationSuffix]
phpdoc_advanced_fallback_trait_string.php:44: Parameter $value has undeclared type \Conformance\Tests\PhpdocAdvancedFallbackTraitString\trait [PhanUndeclaredTypeParameter]
phpdoc_advanced_fallback_trait_string.php:49: Argument 1 ($value) is returnsTraitString() of type \Conformance\Tests\PhpdocAdvancedFallbackTraitString\trait but \Conformance\Tests\PhpdocAdvancedFallbackTraitString\acceptsString() takes string defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_trait_string.php:37 [PhanTypeMismatchArgument]
phpdoc_advanced_fallback_trait_string.php:52: Argument 1 ($value) is SomeTrait::class of type string but \Conformance\Tests\PhpdocAdvancedFallbackTraitString\acceptsTraitString() takes \Conformance\Tests\PhpdocAdvancedFallbackTraitString\trait (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_trait_string.php:44 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
phpdoc_advanced_fallback_trait_string.php:55: Argument 1 ($value) is SomeClass::class of type string but \Conformance\Tests\PhpdocAdvancedFallbackTraitString\acceptsTraitString() takes \Conformance\Tests\PhpdocAdvancedFallbackTraitString\trait (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_trait_string.php:44 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
  • Recognition: spelling not resolved — reported on declaration line(s) 30, 32, 42, 44
  • Enforcement: 1/1 of the expected violations reported — incidental, since the spelling was not resolved
Expectation diff
Line 34: Expected valid value to be accepted, got ["Returning SomeTrait::class of type string but returnsTraitString() is declared to return \\Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\trait (no real type) (the inferred real return type has nothing in common with the declared phpdoc return type) [PhanTypeMismatchReturnProbablyReal]"]
Line 49: Expected valid value to be accepted, got ["Argument 1 ($value) is returnsTraitString() of type \\Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\trait but \\Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\acceptsString() takes string defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_trait_string.php:37 [PhanTypeMismatchArgument]"]
Line 52: Expected valid value to be accepted, got ["Argument 1 ($value) is SomeTrait::class of type string but \\Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\acceptsTraitString() takes \\Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\trait (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_trait_string.php:44 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]"]
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
Unrecognized (pzoom≠)
phpdoc_advanced_fallback_trait_string.php:30: The declared return type 'trait-string' for Conformance\Tests\PhpdocAdvancedFallbackTraitString\returnsTraitString is incorrect, got 'Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeTrait::class' [InvalidReturnType]
phpdoc_advanced_fallback_trait_string.php:34: The inferred type 'Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeTrait::class' does not match the declared return type 'trait-string' for Conformance\Tests\PhpdocAdvancedFallbackTraitString\returnsTraitString [InvalidReturnStatement]
phpdoc_advanced_fallback_trait_string.php:52: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackTraitString\acceptsTraitString expects trait-string, but Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeTrait::class provided [InvalidArgument]
phpdoc_advanced_fallback_trait_string.php:55: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackTraitString\acceptsTraitString expects trait-string, but Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeClass::class provided [InvalidArgument]
pzoom (Psalm port)

No diagnostics from pzoom.

  • Recognition: spelling not resolved — reported on declaration line(s) 30
  • Enforcement: 1/1 of the expected violations reported — incidental, since the spelling was not resolved
Expectation diff
Line 34: Expected valid value to be accepted, got ["The inferred type 'Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\SomeTrait::class' does not match the declared return type 'trait-string' for Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\returnsTraitString [InvalidReturnStatement]"]
Line 52: Expected valid value to be accepted, got ["Argument 1 of Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\acceptsTraitString expects trait-string, but Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\SomeTrait::class provided [InvalidArgument]"]
mago 1.57.0mago 1.57.0 Widened to class-string

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
mir 0.87.0mir 0.87.0 Incidental (1/1)
phpdoc_advanced_fallback_trait_string.php:34: InvalidReturnType: Return type 'class-string<Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeTrait>' is not compatible with declared 'trait-string' [MIR0200]
phpdoc_advanced_fallback_trait_string.php:52: InvalidArgument: Argument $value of acceptsTraitString() expects 'trait-string', got 'class-string<Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeTrait>' [MIR0201]
phpdoc_advanced_fallback_trait_string.php:55: InvalidArgument: Argument $value of acceptsTraitString() expects 'trait-string', got 'class-string<Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeClass>' [MIR0201]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported — incidental, since values the type admits were also rejected on line(s) 52
Expectation diff
Line 34: Expected valid value to be accepted, got ["InvalidReturnType: Return type 'class-string<Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\SomeTrait>' is not compatible with declared 'trait-string' [MIR0200]"]
Line 52: Expected valid value to be accepted, got ["InvalidArgument: Argument $value of acceptsTraitString() expects 'trait-string', got 'class-string<Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\SomeTrait>' [MIR0201]"]
phpantom 0.11.1phpantom_lsp 0.11.1 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
intelephense 1.18.5intelephense 1.18.5 Widened to class-string

No diagnostics reported.

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

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
phpy 1.0.19409phpy 1.0.19409 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
qodana 262.10968.76PhpStorm 262.10968.76 Widened to string

No diagnostics reported.

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

Notes: Checked as plain `string`: a wrong type is reported as "Expected parameter of type 'string'", and a class name passes, exactly as for `@param string`. Quick Documentation still shows `trait-string`, so the spelling is recognised and only the refinement is dropped. Unlike `class-string`, which it keeps as a type of its own, PhpStorm checks `trait-string` as a plain string.

noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Widened to string

No diagnostics reported.

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

Notes: `trait-string` lowers to `ContractTy::StrOpaque` (ADR-0038) like the rest of the `*-string` family, so Steins never checks that the string names a trait specifically — it accepts any string.

phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 Unrecognized
phpdoc_advanced_fallback_trait_string.php:30: The declared return type 'trait-string' for Conformance\Tests\PhpdocAdvancedFallbackTraitString\returnsTraitString is incorrect, got 'Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeTrait::class' [InvalidReturnType]
phpdoc_advanced_fallback_trait_string.php:34: The inferred type 'Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeTrait::class' does not match the declared return type 'trait-string' for Conformance\Tests\PhpdocAdvancedFallbackTraitString\returnsTraitString [InvalidReturnStatement]
phpdoc_advanced_fallback_trait_string.php:52: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackTraitString\acceptsTraitString expects trait-string, but Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeTrait::class provided [InvalidArgument]
phpdoc_advanced_fallback_trait_string.php:55: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackTraitString\acceptsTraitString expects trait-string, but Conformance\Tests\PhpdocAdvancedFallbackTraitString\SomeClass::class provided [InvalidArgument]
  • Recognition: spelling not resolved — reported on declaration line(s) 30
  • Enforcement: 1/1 of the expected violations reported — incidental, since the spelling was not resolved
Expectation diff
Line 34: Expected valid value to be accepted, got ["The inferred type 'Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\SomeTrait::class' does not match the declared return type 'trait-string' for Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\returnsTraitString [InvalidReturnStatement]"]
Line 52: Expected valid value to be accepted, got ["Argument 1 of Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\acceptsTraitString expects trait-string, but Conformance\\Tests\\PhpdocAdvancedFallbackTraitString\\SomeTrait::class provided [InvalidArgument]"]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\PhpdocAdvancedFallbackTraitString;

/**
 * `trait-string`
 *
 * A `class-string` narrowed to the name of a trait. Traits are the odd member
 * of the family: the name exists at compile time and `::class` resolves it, but
 * it denotes nothing instantiable, so an analyzer that checks "is this a known
 * class" rather than "is this a known trait" lets an ordinary class name
 * through.
 *
 * References:
 * - PHPStan TypeNodeResolver `trait-string` resolves to ClassStringType
 */

trait SomeTrait
{
}

final class SomeClass
{
    use SomeTrait;
}

/**
 * @return trait-string
 */
function returnsTraitString() // T: trait-string
{
    return SomeTrait::class; // V
}

function acceptsString(string $value): void
{
}

/**
 * @param trait-string $value
 */
function acceptsTraitString($value): void // T: trait-string
{
}

// A `trait-string` value always satisfies a native `string` parameter.
acceptsString(returnsTraitString()); // V

// The name of a trait satisfies the parameter.
acceptsTraitString(SomeTrait::class); // V

// The name of a class that uses it does not.
acceptsTraitString(SomeClass::class); // E?: a class-string is not a trait-string