← 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.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 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.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
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.46.0mago 1.46.0 Widened to class-string

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
mir 0.70.1mir 0.70.1 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.9.0phpantom_lsp 0.9.0 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
phpy 0.2.0phpy 0.2.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
qodana 262.8665.325Qodana 262.8665.325 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/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

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) 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.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 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