← All results

@psalm-consistent-templates

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

Children of a generic class must keep the same template parameters, so new static($u) returning static<U> stays sound. A child that pins the parent's T to a concrete type is the documented violation. @psalm-consistent-constructor is required by the documented example so new static is constructor-safe; this file measures the templates tag. References: - Psalm supported_annotations.md: @psalm-consistent-templates - Psalm UnsafeGenericInstantiation

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 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpstan 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpstan-strict 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
psalm 6.21.4Psalm 6.21.4@72023213487628df0bc5b2e02d68cbac614b7ef5 Partly enforced (1/2)
phpdoc_advanced_psalm_consistent_templates.php:62: Conformance\Tests\PhpdocAdvancedPsalmConsistentTemplates\PinnedChild requires the same number of template params as Conformance\Tests\PhpdocAdvancedPsalmConsistentTemplates\Container but saw 0 [MissingTemplateParam]
phpdoc_advanced_psalm_consistent_templates.php:62: Cannot extend a strictly-enforced parent template param T with a non-template type [InvalidTemplateParam]
  • Recognition: spelling resolved
  • Enforcement: 1/2 of the expected violations reported
psalm 7 7.21.0Psalm 7.21.0@1a665f0114bd40cf12ad57a1a82066549d92f1b6 Partly enforced (1/2)
phpdoc_advanced_psalm_consistent_templates.php:62: Conformance\Tests\PhpdocAdvancedPsalmConsistentTemplates\PinnedChild requires the same number of template params as Conformance\Tests\PhpdocAdvancedPsalmConsistentTemplates\Container but saw 0 [MissingTemplateParam]
phpdoc_advanced_psalm_consistent_templates.php:62: Cannot extend a strictly-enforced parent template param T with a non-template type [InvalidTemplateParam]
  • Recognition: spelling resolved
  • Enforcement: 1/2 of the expected violations reported
mago 1.61.0mago 1.61.0 Incidental (0/2) ⚠ 2 false positives
phpdoc_advanced_psalm_consistent_templates.php:28: Inconsistent template: expected a template parameter, but found a concrete type. [invalid-template-parameter]
phpdoc_advanced_psalm_consistent_templates.php:63: Template parameter count mismatch: `Conformance\Tests\PhpdocAdvancedPsalmConsistentTemplates\PinnedChild` must have 1 template parameters to match `Conformance\Tests\PhpdocAdvancedPsalmConsistentTemplates\Container`. [inconsistent-template]
  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported — incidental, since values the type admits were also rejected on line(s) 28
  • False positives: line(s) 28, 63
Expectation diff
Line 28: Unexpected errors ["Inconsistent template: expected a template parameter, but found a concrete type. [invalid-template-parameter]"]
Line 63: Unexpected errors ["Template parameter count mismatch: `Conformance\\Tests\\PhpdocAdvancedPsalmConsistentTemplates\\PinnedChild` must have 1 template parameters to match `Conformance\\Tests\\PhpdocAdvancedPsalmConsistentTemplates\\Container`. [inconsistent-template]"]
mir 0.101.0mir 0.101.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpantom 1.1.0phpantom_lsp 1.1.0 Partly enforced (1/2)
phpdoc_advanced_psalm_consistent_templates.php:70: Object of class stdClass cannot be converted to string [invalid_string_conversion]
  • Recognition: spelling resolved
  • Enforcement: 1/2 of the expected violations reported
pun 0.4.3pun 0.4.3 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
intelephense 1.18.5intelephense 1.18.5 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpactor 2026.11.22.0Phpactor 2026.11.22.0 Not enforced
phpdoc_advanced_psalm_consistent_templates.php:45: Method "getInstance" is missing docblock return type: Conformance\Tests\PhpdocAdvancedPsalmConsistentTemplates\Container<Conformance\Tests\PhpdocAdvancedPsalmConsistentTemplates\U> [docblock_missing_return_type]
  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpy 1.0.19566phpy 1.0.19566 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
qodana 263.7307.215PhpStorm 263.7307.215 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 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
phpdoc_advanced_psalm_consistent_templates.php:42: Class or interface named \Conformance\Tests\PhpdocAdvancedPsalmConsistentTemplates\U does not exist [undefinedClass]
  • Recognition: spelling resolved
  • Enforcement: 0/2 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/2 of the expected violations reported

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\PhpdocAdvancedPsalmConsistentTemplates;

/**
 * Cross-tool handling of `@psalm-consistent-templates`.
 *
 * Children of a generic class must keep the same template parameters, so
 * `new static($u)` returning `static<U>` stays sound. A child that pins the
 * parent's `T` to a concrete type is the documented violation.
 *
 * `@psalm-consistent-constructor` is required by the documented example so
 * `new static` is constructor-safe; this file measures the templates tag.
 *
 * References:
 * - Psalm supported_annotations.md: `@psalm-consistent-templates`
 * - Psalm UnsafeGenericInstantiation
 */

/**
 * @template T
 * @psalm-consistent-constructor
 * @psalm-consistent-templates
 */
class Container // T: @psalm-consistent-templates
{
    /** @var T */
    public $value;

    /**
     * @param T $value // E?[noise]
     */
    public function __construct($value) // E?[noise]
    {
        $this->value = $value;
    }

    /**
     * @template U
     * @param U $value // E?[noise]
     * @return static<U>
     */
    public function getInstance($value): static // E?[noise]
    {
        return new static($value);
    }
}

/**
 * @template T
 * @extends Container<T>
 */
class MatchingChild extends Container // V
{
}

/**
 * @extends Container<string>
 */
class PinnedChild extends Container // E?: child must keep the same generic params as Container
{
}

$matching = new MatchingChild(1); // V
echo $matching->getInstance('x')->value;

$pinned = new PinnedChild('ok');
echo $pinned->getInstance(new \stdClass())->value; // E?: new static under pinned T is unsound