phpdoc_advanced_fallback_object_shape.
This file asks two extras: whether bar?: string may be omitted, and whether
intersecting the shape with \stdClass still accepts a matching object.
References:
- PHPStan object shapes
- Psalm object properties| Analyzer | Version | Result | Diagnostics |
|---|---|---|---|
| phan | 6.0.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 | Not enforced | No diagnostics reported.
|
| phpstan | 2.2.11PHPStan - PHP Static Analysis Tool 2.2.11 | Enforced reported Lv.5+ | phpdoc_advanced_object_shape_variants.php:55: Parameter #1 $shape of function Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesOptionalObjectShape expects object{foo: int, bar?: string}, Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WrongFooType given. [reported-from-level=5]
phpdoc_advanced_object_shape_variants.php:56: Parameter #1 $shape of function Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesWritableObjectShape expects object{foo: int}&stdClass, object{foo: string}&stdClass given. [reported-from-level=5]With strict-rulesphpdoc_advanced_object_shape_variants.php:55: Parameter #1 $shape of function Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesOptionalObjectShape expects object{foo: int, bar?: string}, Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WrongFooType given.
phpdoc_advanced_object_shape_variants.php:56: Parameter #1 $shape of function Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesWritableObjectShape expects object{foo: int}&stdClass, object{foo: string}&stdClass given.
|
| psalm | 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac next: 7.0.0-beta19 |
Incidental (2/2) (pzoom≠) | phpdoc_advanced_object_shape_variants.php:51: Argument 1 of Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesOptionalObjectShape expects object{foo:int, bar?:string}, but Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WithoutOptionalProperty provided [InvalidArgument]
phpdoc_advanced_object_shape_variants.php:55: Argument 1 of Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesOptionalObjectShape expects object{foo:int, bar?:string}, but Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WrongFooType provided [InvalidArgument]
phpdoc_advanced_object_shape_variants.php:56: Argument 1 of Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesWritableObjectShape expects object{foo:int}&stdClass, but object{foo:'wrong'} provided [InvalidArgument]pzoom (Psalm port)phpdoc_advanced_object_shape_variants.php:51: Argument 1 of takesOptionalObjectShape expects object{bar?: string, foo: int}, but Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WithoutOptionalProperty provided [InvalidArgument]
phpdoc_advanced_object_shape_variants.php:52: Argument 1 of takesOptionalObjectShape expects object{bar?: string, foo: int}, but object{foo: 1} provided [InvalidArgument]
phpdoc_advanced_object_shape_variants.php:53: Argument 1 of takesWritableObjectShape expects object{foo: int}&stdClass, but object{foo: 1} provided [InvalidArgument]
phpdoc_advanced_object_shape_variants.php:55: Argument 1 of takesOptionalObjectShape expects object{bar?: string, foo: int}, but Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WrongFooType provided [InvalidArgument]
phpdoc_advanced_object_shape_variants.php:56: Argument 1 of takesWritableObjectShape expects object{foo: int}&stdClass, but object{foo: 'wrong'} provided [InvalidArgument]
Expectation diffLine 51: Expected valid value to be accepted, got ["Argument 1 of Conformance\\Tests\\PhpdocAdvancedObjectShapeVariants\\takesOptionalObjectShape expects object{foo:int, bar?:string}, but Conformance\\Tests\\PhpdocAdvancedObjectShapeVariants\\WithoutOptionalProperty provided [InvalidArgument]"] |
| mago | 1.47.4mago 1.47.4 | Unrecognized | phpdoc_advanced_object_shape_variants.php:44: Could not resolve the type for the @param tag. [invalid-param-tag]
phpdoc_advanced_object_shape_variants.php:55: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesOptionalObjectShape`: expected `object{bar?: string, foo: int}`, but found `Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WrongFooType`. [invalid-argument]
|
| mir | 0.72.1mir 0.72.1 | Incidental (1/2) | phpdoc_advanced_object_shape_variants.php:53: InvalidArgument: Argument $shape of takesWritableObjectShape() expects 'object&stdClass', got 'object' [MIR0201] phpdoc_advanced_object_shape_variants.php:56: InvalidArgument: Argument $shape of takesWritableObjectShape() expects 'object&stdClass', got 'object' [MIR0201]
Expectation diffLine 53: Expected valid value to be accepted, got ["InvalidArgument: Argument $shape of takesWritableObjectShape() expects 'object&stdClass', got 'object' [MIR0201]"] |
| phpantom | 0.10.0phpantom_lsp 0.10.0 | Incidental (2/2) | phpdoc_advanced_object_shape_variants.php:53: Argument 1 ($shape) expects object{foo: int}&stdClass, got object{foo: int} [type_mismatch_argument]
phpdoc_advanced_object_shape_variants.php:55: Argument 1 ($shape) expects object{foo: int, bar?: string}, got Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WrongFooType [type_mismatch_argument]
phpdoc_advanced_object_shape_variants.php:56: Argument 1 ($shape) expects object{foo: int}&stdClass, got object{foo: string} [type_mismatch_argument]
Expectation diffLine 53: Expected valid value to be accepted, got ["Argument 1 ($shape) expects object{foo: int}&stdClass, got object{foo: int} [type_mismatch_argument]"] |
| intelephense | 1.18.5intelephense 1.18.5 | Not enforced | No diagnostics reported.
|
| phpactor | 2026.07.22.0Phpactor 2026.07.22.0 | Not enforced | No diagnostics reported.
|
| phpy | 0.2.0phpy 0.2.0 | Not enforced | No diagnostics reported.
|
| qodana | 262.9437.196Qodana 262.9437.196 | Not enforced | No diagnostics reported.
|
| noverify | 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 | Unrecognized | phpdoc_advanced_object_shape_variants.php:39: param $shape miss matched with phpdoc type <<object{foo: int, bar?: string}>> [funcParamTypeMissMatch]
phpdoc_advanced_object_shape_variants.php:46: param $shape miss matched with phpdoc type <<object{foo: int}&\stdClass>> [funcParamTypeMissMatch]
|
| steins | 0.1.6steins 0.1.6 (2026-08-25 revision c13b4c7) | Not enforced | No diagnostics reported.
|
| phpstan-strict | 2.2.11PHPStan - PHP Static Analysis Tool 2.2.11 | Enforced | phpdoc_advanced_object_shape_variants.php:55: Parameter #1 $shape of function Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesOptionalObjectShape expects object{foo: int, bar?: string}, Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WrongFooType given.
phpdoc_advanced_object_shape_variants.php:56: Parameter #1 $shape of function Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesWritableObjectShape expects object{foo: int}&stdClass, object{foo: string}&stdClass given.
|
| psalm-next | 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 | Incidental (2/2) | phpdoc_advanced_object_shape_variants.php:51: Argument 1 of Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesOptionalObjectShape expects object{foo:int, bar?:string}, but Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WithoutOptionalProperty provided [InvalidArgument]
phpdoc_advanced_object_shape_variants.php:55: Argument 1 of Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesOptionalObjectShape expects object{foo:int, bar?:string}, but Conformance\Tests\PhpdocAdvancedObjectShapeVariants\WrongFooType provided [InvalidArgument]
phpdoc_advanced_object_shape_variants.php:56: Argument 1 of Conformance\Tests\PhpdocAdvancedObjectShapeVariants\takesWritableObjectShape expects object{foo:int}&stdClass, but object{foo:'wrong'} provided [InvalidArgument]
Expectation diffLine 51: Expected valid value to be accepted, got ["Argument 1 of Conformance\\Tests\\PhpdocAdvancedObjectShapeVariants\\takesOptionalObjectShape expects object{foo:int, bar?:string}, but Conformance\\Tests\\PhpdocAdvancedObjectShapeVariants\\WithoutOptionalProperty provided [InvalidArgument]"] |
<?php
declare(strict_types=1);
namespace Conformance\Tests\PhpdocAdvancedObjectShapeVariants;
/**
* Optional properties and writable object shapes.
*
* The sealed required-property case is `phpdoc_advanced_fallback_object_shape`.
* This file asks two extras: whether `bar?: string` may be omitted, and whether
* intersecting the shape with `\stdClass` still accepts a matching object.
*
* References:
* - PHPStan object shapes
* - Psalm object properties
*/
final class WithOptionalProperty
{
public int $foo = 1;
public string $bar = 'optional';
}
final class WithoutOptionalProperty
{
public int $foo = 1;
}
final class WrongFooType
{
public string $foo = 'wrong';
}
/**
* @param object{foo: int, bar?: string} $shape
*/
function takesOptionalObjectShape(object $shape): void // T: object{foo: int, bar?: string}
{
}
/**
* @param object{foo: int}&\stdClass $shape
*/
function takesWritableObjectShape(object $shape): void // T: object{foo: int}&\stdClass
{
}
takesOptionalObjectShape(new WithOptionalProperty()); // V
takesOptionalObjectShape(new WithoutOptionalProperty()); // V
takesOptionalObjectShape((object) ['foo' => 1]); // V
takesWritableObjectShape((object) ['foo' => 1]); // V
takesOptionalObjectShape(new WrongFooType()); // E?: foo must have type int
takesWritableObjectShape((object) ['foo' => 'wrong']); // E?: foo must have type int