From 90469a564e1180c9e5654033acb6cb61d7af9a4f Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 14 Mar 2024 00:23:32 +0100 Subject: [PATCH 1/2] Add param out for propertyAccessor --- composer.json | 2 +- extension.neon | 1 + .../PropertyAccessorInterface.stub | 21 +++++++++++++++++++ tests/Type/Symfony/ExtensionTest.php | 1 + tests/Type/Symfony/data/property_accessor.php | 13 ++++++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 stubs/Symfony/Component/PropertyAccess/PropertyAccessorInterface.stub create mode 100644 tests/Type/Symfony/data/property_accessor.php diff --git a/composer.json b/composer.json index b8f33962..7da1f489 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "require": { "php": "^7.2 || ^8.0", "ext-simplexml": "*", - "phpstan/phpstan": "^1.10.36" + "phpstan/phpstan": "^1.10.62" }, "conflict": { "symfony/framework-bundle": "<3.0" diff --git a/extension.neon b/extension.neon index 531eb7df..43b966ad 100644 --- a/extension.neon +++ b/extension.neon @@ -66,6 +66,7 @@ parameters: - stubs/Symfony/Component/OptionsResolver/Exception/InvalidOptionsException.stub - stubs/Symfony/Component/OptionsResolver/Options.stub - stubs/Symfony/Component/Process/Process.stub + - stubs/Symfony/Component/PropertyAccess/PropertyAccessorInterface.stub - stubs/Symfony/Component/PropertyAccess/PropertyPathInterface.stub - stubs/Symfony/Component/Security/Acl/Model/AclInterface.stub - stubs/Symfony/Component/Security/Acl/Model/EntryInterface.stub diff --git a/stubs/Symfony/Component/PropertyAccess/PropertyAccessorInterface.stub b/stubs/Symfony/Component/PropertyAccess/PropertyAccessorInterface.stub new file mode 100644 index 00000000..48547b12 --- /dev/null +++ b/stubs/Symfony/Component/PropertyAccess/PropertyAccessorInterface.stub @@ -0,0 +1,21 @@ + + * @phpstan-param T &$objectOrArray + * @phpstan-param-out ($objectOrArray is object ? T : array) $objectOrArray + * + * @return void + * + * @throws Exception\InvalidArgumentException If the property path is invalid + * @throws Exception\AccessException If a property/index does not exist or is not public + * @throws Exception\UnexpectedTypeException If a value within the path is neither object nor array + */ + public function setValue(object|array &$objectOrArray, string|PropertyPathInterface $propertyPath, mixed $value); + +} diff --git a/tests/Type/Symfony/ExtensionTest.php b/tests/Type/Symfony/ExtensionTest.php index 73eb49b7..a076caac 100644 --- a/tests/Type/Symfony/ExtensionTest.php +++ b/tests/Type/Symfony/ExtensionTest.php @@ -28,6 +28,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/ExampleOptionCommand.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/ExampleOptionLazyCommand.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/kernel_interface.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/property_accessor.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/request_get_content.php'); $ref = new ReflectionMethod(Request::class, 'getSession'); diff --git a/tests/Type/Symfony/data/property_accessor.php b/tests/Type/Symfony/data/property_accessor.php new file mode 100644 index 00000000..0e445684 --- /dev/null +++ b/tests/Type/Symfony/data/property_accessor.php @@ -0,0 +1,13 @@ + 'ea']; +$propertyAccessor->setValue($array, 'foo', 'bar'); +assertType('array', $array); + +$object = new \stdClass(); +$propertyAccessor->setValue($object, 'foo', 'bar'); +assertType('stdClass', $object); From 2485968011f0220492e691dcceb8985bfc19465a Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 14 Mar 2024 01:37:48 +0100 Subject: [PATCH 2/2] Fix stub --- extension.neon | 5 +++++ .../PropertyAccess/Exception/AccessException.stub | 7 +++++++ .../PropertyAccess/Exception/ExceptionInterface.stub | 7 +++++++ .../PropertyAccess/Exception/InvalidArgumentException.stub | 7 +++++++ .../PropertyAccess/Exception/RuntimeException.stub | 7 +++++++ .../PropertyAccess/Exception/UnexpectedTypeException.stub | 7 +++++++ .../PropertyAccess/PropertyAccessorInterface.stub | 4 +++- 7 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 stubs/Symfony/Component/PropertyAccess/Exception/AccessException.stub create mode 100644 stubs/Symfony/Component/PropertyAccess/Exception/ExceptionInterface.stub create mode 100644 stubs/Symfony/Component/PropertyAccess/Exception/InvalidArgumentException.stub create mode 100644 stubs/Symfony/Component/PropertyAccess/Exception/RuntimeException.stub create mode 100644 stubs/Symfony/Component/PropertyAccess/Exception/UnexpectedTypeException.stub diff --git a/extension.neon b/extension.neon index 43b966ad..09837d28 100644 --- a/extension.neon +++ b/extension.neon @@ -66,6 +66,11 @@ parameters: - stubs/Symfony/Component/OptionsResolver/Exception/InvalidOptionsException.stub - stubs/Symfony/Component/OptionsResolver/Options.stub - stubs/Symfony/Component/Process/Process.stub + - stubs/Symfony/Component/PropertyAccess/Exception/AccessException.stub + - stubs/Symfony/Component/PropertyAccess/Exception/ExceptionInterface.stub + - stubs/Symfony/Component/PropertyAccess/Exception/InvalidArgumentException.stub + - stubs/Symfony/Component/PropertyAccess/Exception/RuntimeException.stub + - stubs/Symfony/Component/PropertyAccess/Exception/UnexpectedTypeException.stub - stubs/Symfony/Component/PropertyAccess/PropertyAccessorInterface.stub - stubs/Symfony/Component/PropertyAccess/PropertyPathInterface.stub - stubs/Symfony/Component/Security/Acl/Model/AclInterface.stub diff --git a/stubs/Symfony/Component/PropertyAccess/Exception/AccessException.stub b/stubs/Symfony/Component/PropertyAccess/Exception/AccessException.stub new file mode 100644 index 00000000..a763b784 --- /dev/null +++ b/stubs/Symfony/Component/PropertyAccess/Exception/AccessException.stub @@ -0,0 +1,7 @@ + * @phpstan-param T &$objectOrArray * @phpstan-param-out ($objectOrArray is object ? T : array) $objectOrArray + * @phpstan-param string|PropertyPathInterface $propertyPath + * @phpstan-param mixed $value * * @return void * @@ -16,6 +18,6 @@ interface PropertyAccessorInterface * @throws Exception\AccessException If a property/index does not exist or is not public * @throws Exception\UnexpectedTypeException If a value within the path is neither object nor array */ - public function setValue(object|array &$objectOrArray, string|PropertyPathInterface $propertyPath, mixed $value); + public function setValue(&$objectOrArray, $propertyPath, $value); }