← All results

The template-type utility

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

template-type<Object, ClassName, 'TName'> extracts a named @template argument from an object type. PHPStan-specific utility for generics. References: - PHPStan phpdoc-types: Utility types for generics (template-type)

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_phpstan_template_type.php:32: Doc-block of unwrap has declared return type \Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template which is not a permitted replacement of the nullable return type mixed declared in the signature ('?T' should be documented as 'T|null' or '?T') [PhanTypeMismatchDeclaredReturnNullable]
phpdoc_advanced_phpstan_template_type.php:32: Saw a token Phan may have failed to parse after '* @return template-type<Box<T>, Box, 'T'>': after template, saw '-' [PhanUnextractableAnnotationSuffix]
phpdoc_advanced_phpstan_template_type.php:34: Template type T not used in return value of function/method unwrap() [PhanTemplateTypeNotUsedInFunctionReturn]
phpdoc_advanced_phpstan_template_type.php:34: Return type of unwrap() is undeclared type \Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template [PhanUndeclaredTypeReturnType]
  • Recognition: spelling not resolved — reported on declaration line(s) 32, 34
  • Enforcement: 0/1 of the expected violations reported — incidental, since the spelling was not resolved
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Enforced reported Lv.5+
phpdoc_advanced_phpstan_template_type.php:47: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\takesString expects string, int given. [identifier=argument.type] [reported-from-level=5]
With strict-rules
phpdoc_advanced_phpstan_template_type.php:47: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\takesString expects string, int given. [identifier=argument.type]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
Unrecognized (pzoom≠)
phpdoc_advanced_phpstan_template_type.php:32: Docblock-defined class, interface or enum named Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type does not exist [UndefinedDocblockClass]
phpdoc_advanced_phpstan_template_type.php:44: Argument 1 of Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\takesString expects string, but Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type<Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box<'ok'>, Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box, 'T'> provided [InvalidArgument]
phpdoc_advanced_phpstan_template_type.php:47: Argument 1 of Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\takesString expects string, but Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type<Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box<1>, Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box, 'T'> provided [InvalidArgument]
pzoom (Psalm port)
phpdoc_advanced_phpstan_template_type.php:34: Invalid return docblock type [InvalidDocblock]
phpdoc_advanced_phpstan_template_type.php:44: Argument 1 of takesString cannot be mixed, expecting string [MixedArgument]
phpdoc_advanced_phpstan_template_type.php:47: Argument 1 of takesString cannot be mixed, expecting string [MixedArgument]
  • Recognition: spelling not resolved — reported on declaration line(s) 32
  • Enforcement: 1/1 of the expected violations reported — incidental, since the spelling was not resolved
Expectation diff
Line 44: Expected valid value to be accepted, got ["Argument 1 of Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\takesString expects string, but Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\template-type<Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\Box<'ok'>, Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\Box, 'T'> provided [InvalidArgument]"]
mago 1.46.0mago 1.46.0 Enforced
phpdoc_advanced_phpstan_template_type.php:47: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\takesString`: expected `string`, but found `int`. [invalid-argument]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
mir 0.70.1mir 0.70.1 Incidental (1/1)
phpdoc_advanced_phpstan_template_type.php:44: InvalidArgument: Argument $value of takesString() expects 'string', got 'Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type<Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box<string>, Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box, "T">' [MIR0201]
phpdoc_advanced_phpstan_template_type.php:47: InvalidArgument: Argument $value of takesString() expects 'string', got 'Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type<Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box<int>, Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box, "T">' [MIR0201]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported — incidental, since values the type admits were also rejected on line(s) 44
Expectation diff
Line 44: Expected valid value to be accepted, got ["InvalidArgument: Argument $value of takesString() expects 'string', got 'Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\template-type<Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\Box<string>, Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\Box, \"T\">' [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 Incidental (1/1)
phpdoc_advanced_phpstan_template_type.php:44: Expected type 'string'. Found 'Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type<Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box<string>, Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box, 'T'>'. [P1006]
phpdoc_advanced_phpstan_template_type.php:47: Expected type 'string'. Found 'Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type<Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box<int>, Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box, 'T'>'. [P1006]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported — incidental, since values the type admits were also rejected on line(s) 44
Expectation diff
Line 44: Expected valid value to be accepted, got ["Expected type 'string'. Found 'Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\template-type<Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\Box<string>, Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\Box, 'T'>'. [P1006]"]
phpy 0.2.0phpy 0.2.0 Enforced
phpdoc_advanced_phpstan_template_type.php:47: Argument '1' passed to Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\takesString() is expected to be of type string, int given
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
qodana 262.8665.325Qodana 262.8665.325 Unrecognized
phpdoc_advanced_phpstan_template_type.php:32: Undefined class 'template-type' [PhpUndefinedClassInspection]
  • Recognition: spelling not resolved — reported on declaration line(s) 32
  • Enforcement: 0/1 of the expected violations reported — incidental, since the spelling was not resolved
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Unrecognized ⚠ 1 false positive
phpdoc_advanced_phpstan_template_type.php:24: param $value miss matched with phpdoc type <<T>> [funcParamTypeMissMatch]
phpdoc_advanced_phpstan_template_type.php:32: Class or interface named \Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type does not exist [undefinedClass]
phpdoc_advanced_phpstan_template_type.php:34: param $box miss matched with phpdoc type <<Box<T>>> [funcParamTypeMissMatch]
  • Recognition: spelling not resolved — reported on declaration line(s) 32, 34
  • Enforcement: 0/1 of the expected violations reported — incidental, since the spelling was not resolved
  • False positives: line(s) 24
Expectation diff
Line 24: Unexpected errors ["param $value miss matched with phpdoc type <<T>> [funcParamTypeMissMatch]"]
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Unrecognized
phpdoc_advanced_phpstan_template_type.php:34: the return of unwrap() names the generic class Box without type arguments — it declares `@template T` [untyped.generics]
  • Recognition: spelling not resolved — reported on declaration line(s) 34
  • Enforcement: 0/1 of the expected violations reported — incidental, since the spelling was not resolved
phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Enforced
phpdoc_advanced_phpstan_template_type.php:47: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\takesString expects string, int given. [identifier=argument.type]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 Unrecognized
phpdoc_advanced_phpstan_template_type.php:32: Docblock-defined class, interface or enum named Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type does not exist [UndefinedDocblockClass]
phpdoc_advanced_phpstan_template_type.php:44: Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type<Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box<'ok'>, Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box, 'T'> cannot be cast to string [InvalidCast]
phpdoc_advanced_phpstan_template_type.php:44: Argument 1 of Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\takesString expects string, but Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type<Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box<'ok'>, Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box, 'T'> provided [InvalidArgument]
phpdoc_advanced_phpstan_template_type.php:47: Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type<Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box<1>, Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box, 'T'> cannot be cast to string [InvalidCast]
phpdoc_advanced_phpstan_template_type.php:47: Argument 1 of Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\takesString expects string, but Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\template-type<Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box<1>, Conformance\Tests\PhpdocAdvancedPhpstanTemplateType\Box, 'T'> provided [InvalidArgument]
  • Recognition: spelling not resolved — reported on declaration line(s) 32
  • Enforcement: 1/1 of the expected violations reported — incidental, since the spelling was not resolved
Expectation diff
Line 44: Expected valid value to be accepted, got ["Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\template-type<Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\Box<'ok'>, Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\Box, 'T'> cannot be cast to string [InvalidCast]","Argument 1 of Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\takesString expects string, but Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\template-type<Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\Box<'ok'>, Conformance\\Tests\\PhpdocAdvancedPhpstanTemplateType\\Box, 'T'> provided [InvalidArgument]"]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\PhpdocAdvancedPhpstanTemplateType;

/**
 * Cross-tool handling of the `template-type` utility.
 *
 * `template-type<Object, ClassName, 'TName'>` extracts a named `@template`
 * argument from an object type. PHPStan-specific utility for generics.
 *
 * References:
 * - PHPStan phpdoc-types: Utility types for generics (`template-type`)
 */

/**
 * @template T
 */
final class Box
{
    /** @param T $value */
    public function __construct(
        public mixed $value,
    ) {
    }
}

/**
 * @template T
 * @param Box<T> $box
 * @return template-type<Box<T>, Box, 'T'>
 */
function unwrap(Box $box): mixed // T: template-type<Box<T>, Box, 'T'>
{
    return $box->value; // V
}

function takesString(string $value): void
{
}

// T resolves to string.
takesString(unwrap(new Box('ok'))); // V

// T resolves to int, so string is wrong.
takesString(unwrap(new Box(1))); // E?: template-type should yield int here