← All results

new $class() when $class is a union of known class names

Group: Object compatibility · Category: language · File: objects_new_from_class_string_union.php

generics_class_string_property_new covers a class-string<T> property; objects_class_constant_on_instance covers a single class name taken from $object::class. This file asks whether a local union User::class|Admin::class instantiates as User|Admin rather than object. Admin extends User, so name() is on every member and missing() on none — the probe does not depend on how strictly a tool checks methods present on only one member of a union. Source: jorgsowa/mir v0.87.0. References: - https://github.com/jorgsowa/mir/releases/tag/v0.87.0

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.11Phan 6.0.11 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Pass
objects_new_from_class_string_union.php:47: Call to undeclared method \Conformance\Tests\ObjectsNewFromClassStringUnion\User::missing [PhanUndeclaredMethod]
phpstan 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 Pass reported Lv.2+
objects_new_from_class_string_union.php:47: Call to an undefined method Conformance\Tests\ObjectsNewFromClassStringUnion\User::missing(). [reported-from-level=2]
objects_new_from_class_string_union.php:47: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10]
With strict-rules
objects_new_from_class_string_union.php:47: Call to an undefined method Conformance\Tests\ObjectsNewFromClassStringUnion\User::missing().
objects_new_from_class_string_union.php:47: Parameter #1 (mixed) of echo cannot be converted to string.
psalm 6.20.9Psalm 6.20.9@f67170ac3c3b931f5088681e21d6c8babc601c08
next: 7.0.4
Pass
objects_new_from_class_string_union.php:47: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
objects_new_from_class_string_union.php:47: Method Conformance\Tests\ObjectsNewFromClassStringUnion\User::missing does not exist [UndefinedMethod]
mago 1.59.0mago 1.59.0 Pass
objects_new_from_class_string_union.php:43: Ambiguous instantiation: the expression used with `new` can resolve to multiple different classes. [ambiguous-instantiation-target]
objects_new_from_class_string_union.php:43: Unsafe `new $class_name`: constructor of `Conformance\Tests\ObjectsNewFromClassStringUnion\User` is not final and its signature might change in child classes, potentially leading to runtime errors. [unsafe-instantiation]
objects_new_from_class_string_union.php:46: Ambiguous instantiation: the expression used with `new` can resolve to multiple different classes. [ambiguous-instantiation-target]
objects_new_from_class_string_union.php:46: Unsafe `new $class_name`: constructor of `Conformance\Tests\ObjectsNewFromClassStringUnion\User` is not final and its signature might change in child classes, potentially leading to runtime errors. [unsafe-instantiation]
objects_new_from_class_string_union.php:47: Method `missing` does not exist on type `Conformance\Tests\ObjectsNewFromClassStringUnion\User`. [non-existent-method]
objects_new_from_class_string_union.php:47: Method `missing` does not exist on type `Conformance\Tests\ObjectsNewFromClassStringUnion\Admin`. [non-existent-method]
objects_new_from_class_string_union.php:47: The first value for `echo` is too general. [mixed-argument]
mir 0.91.0mir 0.91.0 Pass
objects_new_from_class_string_union.php:47: UndefinedMethod: Method Conformance\Tests\ObjectsNewFromClassStringUnion\User::missing() does not exist [MIR0004]
phpantom 0.11.1phpantom_lsp 0.11.1 Pass
objects_new_from_class_string_union.php:47: Method 'missing' not found on any of the 2 possible types (Conformance\Tests\ObjectsNewFromClassStringUnion\User, Conformance\Tests\ObjectsNewFromClassStringUnion\Admin) [unknown_member]
intelephense 1.18.5intelephense 1.18.5 Pass
objects_new_from_class_string_union.php:47: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006]
objects_new_from_class_string_union.php:47: Undefined method 'missing'. [P1013]
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail

No diagnostics reported.

Expectation diff
Line 47: Expected 1 error(s)
phpy 1.0.19490phpy 1.0.19490 Pass
objects_new_from_class_string_union.php:47: Call to unknown method: Conformance\Tests\ObjectsNewFromClassStringUnion\Admin|Conformance\Tests\ObjectsNewFromClassStringUnion\User::missing()
qodana 262.11879.104PhpStorm 262.11879.104 Fail

No diagnostics reported.

Expectation diff
Line 47: 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 Fail
objects_new_from_class_string_union.php:44: not null safety call in function Conformance\Tests\ObjectsNewFromClassStringUnion\takesUser signature of param value [notNullSafetyFunctionArgumentVariable]
Expectation diff
Line 47: Expected 1 error(s)
Line 44: Expected valid value to be accepted, got ["not null safety call in function Conformance\\Tests\\ObjectsNewFromClassStringUnion\\takesUser signature of param value [notNullSafetyFunctionArgumentVariable]"]
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

Expectation diff
Line 47: Expected 1 error(s)
phpstan-strict 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 Pass
objects_new_from_class_string_union.php:47: Call to an undefined method Conformance\Tests\ObjectsNewFromClassStringUnion\User::missing().
objects_new_from_class_string_union.php:47: Parameter #1 (mixed) of echo cannot be converted to string.
psalm-next 7.0.4Psalm 7.0.4@fa5b1f6e665cb743838ffd865b8fc96207f9409f Pass
objects_new_from_class_string_union.php:47: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
objects_new_from_class_string_union.php:47: Method Conformance\Tests\ObjectsNewFromClassStringUnion\User::missing does not exist [UndefinedMethod]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\ObjectsNewFromClassStringUnion;

/**
 * `new $class()` when `$class` is a union of known class names.
 *
 * `generics_class_string_property_new` covers a `class-string<T>` property;
 * `objects_class_constant_on_instance` covers a single class name taken from
 * `$object::class`. This file asks whether a local union
 * `User::class|Admin::class` instantiates as `User|Admin` rather than
 * `object`. Admin extends User, so `name()` is on every member and
 * `missing()` on none — the probe does not depend on how strictly a tool
 * checks methods present on only one member of a union.
 *
 * Source: jorgsowa/mir v0.87.0.
 *
 * References:
 * - https://github.com/jorgsowa/mir/releases/tag/v0.87.0
 */

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

final class Admin extends User
{
}

function takesUser(User $value): void
{
}

function fromUnionClassString(): void
{
    $class = \random_int(0, 1) === 1 ? User::class : Admin::class;
    $instance = new $class(); // E?[noise]: UnsafeInstantiation on the non-final User
    takesUser($instance); // V: User|Admin is a User
    echo $instance->name(); // V
    $other = new $class(); // E?[noise]: UnsafeInstantiation on the non-final User
    echo $other->missing(); // E: neither User nor Admin has missing()
}