← All results

Optional properties and writable object shapes

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

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

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

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 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-rules
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.
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
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]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported — incidental, since values the type admits were also rejected on line(s) 51
Expectation diff
Line 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.57.0mago 1.57.0 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]
  • Recognition: spelling not resolved — reported on declaration line(s) 44
  • Enforcement: 1/2 of the expected violations reported — incidental, since the spelling was not resolved
mir 0.87.0mir 0.87.0 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]
  • Recognition: spelling resolved
  • Enforcement: 1/2 of the expected violations reported — incidental, since values the type admits were also rejected on line(s) 53
Expectation diff
Line 53: Expected valid value to be accepted, got ["InvalidArgument: Argument $shape of takesWritableObjectShape() expects 'object&stdClass', got 'object' [MIR0201]"]
phpantom 0.11.1phpantom_lsp 0.11.1 Enforced
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}&stdClass [type_mismatch_argument]
  • Recognition: spelling resolved
  • Enforcement: 2/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.10.07.0Phpactor 2026.10.07.0 Not enforced

No diagnostics reported.

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

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
qodana 262.10968.76PhpStorm 262.10968.76 Widened to object / stdClass

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported

Notes: Each spelling is checked as its class part alone: `object{foo: int, bar?: string}` as `object`, `object{foo: int}&\stdClass` as `stdClass` (its messages expect 'object&stdClass'). No property is compared, so a `foo` of the wrong type passes.

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]
  • Recognition: spelling not resolved — reported on declaration line(s) 39, 46
  • Enforcement: 0/2 of the expected violations reported — incidental, since the spelling was not resolved
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
phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 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.
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 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]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported — incidental, since values the type admits were also rejected on line(s) 51
Expectation diff
Line 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]"]

Source

<?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