← All results

new through a class-string<T> property

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

Instantiating through a class-string<T> held in a property: new ($this->class)() is a T. The class-string generic case is already covered for parameters (phpdoc_advanced_generic_class_string); this asks whether the property read and the new (expr) syntax carry it too. References: - PHPStan / Psalm class-string<T> - https://wiki.php.net/rfc/variable_syntax_tweaks (new with an expression)

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 Enforced
generics_class_string_property_new.php:39: Call to undeclared method \Conformance\Tests\GenericsClassStringPropertyNew\User::missing [PhanUndeclaredMethod]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Enforced reported Lv.2+
generics_class_string_property_new.php:39: Call to an undefined method Conformance\Tests\GenericsClassStringPropertyNew\User::missing(). [reported-from-level=2]
generics_class_string_property_new.php:39: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10]
With strict-rules
generics_class_string_property_new.php:39: Call to an undefined method Conformance\Tests\GenericsClassStringPropertyNew\User::missing().
generics_class_string_property_new.php:39: Parameter #1 (mixed) of echo cannot be converted to string.
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
Enforced
generics_class_string_property_new.php:36: Cannot safely instantiate class Conformance\Tests\GenericsClassStringPropertyNew\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation]
generics_class_string_property_new.php:38: Cannot safely instantiate class Conformance\Tests\GenericsClassStringPropertyNew\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation]
generics_class_string_property_new.php:39: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
generics_class_string_property_new.php:39: Method Conformance\Tests\GenericsClassStringPropertyNew\User::missing does not exist [UndefinedMethod]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
mago 1.57.0mago 1.57.0 Enforced
generics_class_string_property_new.php:39: Method `missing` does not exist on type `Conformance\Tests\GenericsClassStringPropertyNew\User`. [non-existent-method]
generics_class_string_property_new.php:39: The first value for `echo` is too general. [mixed-argument]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
mir 0.87.0mir 0.87.0 Enforced
generics_class_string_property_new.php:39: UndefinedMethod: Method Conformance\Tests\GenericsClassStringPropertyNew\User::missing() does not exist [MIR0004]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
phpantom 0.11.1phpantom_lsp 0.11.1 Enforced
generics_class_string_property_new.php:37: Cannot verify method 'name' — type of '$user' could not be resolved [unresolved_member_access]
generics_class_string_property_new.php:39: Cannot verify method 'missing' — type of '$made' could not be resolved [unresolved_member_access]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
Expectation diff
Line 37: Expected valid value to be accepted, got ["Cannot verify method 'name' — type of '$user' could not be resolved [unresolved_member_access]"]
intelephense 1.18.5intelephense 1.18.5 Enforced
generics_class_string_property_new.php:39: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006]
generics_class_string_property_new.php:39: Undefined method 'missing'. [P1013]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Enforced
generics_class_string_property_new.php:39: Method "missing" does not exist on class "Conformance\Tests\GenericsClassStringPropertyNew\User" [missing_member]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
phpy 1.0.19409phpy 1.0.19409 Enforced
generics_class_string_property_new.php:39: Call to unknown method: Conformance\Tests\GenericsClassStringPropertyNew\User::missing()
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
qodana 262.10968.76PhpStorm 262.10968.76 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
Expectation diff
Line 39: Expected 1 error(s)
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
Expectation diff
Line 39: Expected 1 error(s)
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
Expectation diff
Line 39: Expected 1 error(s)
phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Enforced
generics_class_string_property_new.php:39: Call to an undefined method Conformance\Tests\GenericsClassStringPropertyNew\User::missing().
generics_class_string_property_new.php:39: Parameter #1 (mixed) of echo cannot be converted to string.
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 Enforced
generics_class_string_property_new.php:36: Cannot safely instantiate class Conformance\Tests\GenericsClassStringPropertyNew\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation]
generics_class_string_property_new.php:38: Cannot safely instantiate class Conformance\Tests\GenericsClassStringPropertyNew\User with "new $class_name" as its constructor might change in child classes [UnsafeInstantiation]
generics_class_string_property_new.php:39: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
generics_class_string_property_new.php:39: Method Conformance\Tests\GenericsClassStringPropertyNew\User::missing does not exist [UndefinedMethod]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\GenericsClassStringPropertyNew;

/**
 * `new` through a `class-string<T>` property.
 *
 * Instantiating through a `class-string<T>` held in a property:
 * `new ($this->class)()` is a T. The class-string generic case is already
 * covered for parameters (phpdoc_advanced_generic_class_string); this asks
 * whether the property read and the `new (expr)` syntax carry it too.
 *
 * References:
 * - PHPStan / Psalm class-string<T>
 * - https://wiki.php.net/rfc/variable_syntax_tweaks (new with an expression)
 */

final class User
{
    public function name(): string
    {
        return 'user';
    }
}

final class Factory
{
    /** @var class-string<User> */
    public string $class = User::class; // T: class-string<User>
}

function instantiates(Factory $factory): void
{
    $user = new ($factory->class)(); // E?[noise]: UnsafeInstantiation
    echo $user->name(); // V
    $made = new ($factory->class)(); // E?[noise]: UnsafeInstantiation
    echo $made->missing(); // E: the instance is a User
}