From 1f3e26317a0c03251c846cc6bc8e31b9c816fa5d Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Mon, 15 Dec 2025 00:07:50 +0100 Subject: [PATCH] Rename `Container::getUnsafeValues()` to `getUntrustedValues()` Mirroring the upstream change from 3.1.10: https://github.com/nette/forms/commit/fb2df3bfabf5f7ec11f622f8940d1e6e4d39582e The new method also makes the `returnType` argument optional, defaulting to `ArrayHash`. Also add handling of `returnType` taking `class-string`. Finally, add tests based on `getValues()`. --- extension.neon | 2 +- ...ustedValuesDynamicReturnTypeExtension.php} | 10 ++-- ...edValuesDynamicReturnTypeExtensionTest.php | 51 +++++++++++++++++++ .../data/FormContainerUntrustedValues.php | 41 +++++++++++++++ 4 files changed, 100 insertions(+), 4 deletions(-) rename src/Type/Nette/{FormContainerUnsafeValuesDynamicReturnTypeExtension.php => FormContainerUntrustedValuesDynamicReturnTypeExtension.php} (74%) create mode 100644 tests/Type/Nette/FormContainerUntrustedValuesDynamicReturnTypeExtensionTest.php create mode 100644 tests/Type/Nette/data/FormContainerUntrustedValues.php diff --git a/extension.neon b/extension.neon index 31eb67c..49f2125 100644 --- a/extension.neon +++ b/extension.neon @@ -104,7 +104,7 @@ services: - phpstan.broker.dynamicMethodReturnTypeExtension - - class: PHPStan\Type\Nette\FormContainerUnsafeValuesDynamicReturnTypeExtension + class: PHPStan\Type\Nette\FormContainerUntrustedValuesDynamicReturnTypeExtension tags: - phpstan.broker.dynamicMethodReturnTypeExtension diff --git a/src/Type/Nette/FormContainerUnsafeValuesDynamicReturnTypeExtension.php b/src/Type/Nette/FormContainerUntrustedValuesDynamicReturnTypeExtension.php similarity index 74% rename from src/Type/Nette/FormContainerUnsafeValuesDynamicReturnTypeExtension.php rename to src/Type/Nette/FormContainerUntrustedValuesDynamicReturnTypeExtension.php index d4d3f2b..0619fac 100644 --- a/src/Type/Nette/FormContainerUnsafeValuesDynamicReturnTypeExtension.php +++ b/src/Type/Nette/FormContainerUntrustedValuesDynamicReturnTypeExtension.php @@ -13,7 +13,7 @@ use PHPStan\Type\Type; use function count; -class FormContainerUnsafeValuesDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension +class FormContainerUntrustedValuesDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension { public function getClass(): string @@ -23,13 +23,13 @@ public function getClass(): string public function isMethodSupported(MethodReflection $methodReflection): bool { - return $methodReflection->getName() === 'getUnsafeValues'; + return $methodReflection->getName() === 'getUntrustedValues' || $methodReflection->getName() === 'getUnsafeValues'; } public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type { if (count($methodCall->getArgs()) === 0) { - return null; + return new ObjectType('Nette\Utils\ArrayHash'); } $arg = $methodCall->getArgs()[0]->value; @@ -38,6 +38,10 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method return new ObjectType('Nette\Utils\ArrayHash'); } + if ($scopedType->isClassString()->yes()) { + return $scopedType->getClassStringObjectType(); + } + if (count($scopedType->getConstantStrings()) === 1 && $scopedType->getConstantStrings()[0]->getValue() === 'array') { return new ArrayType(new StringType(), new MixedType()); } diff --git a/tests/Type/Nette/FormContainerUntrustedValuesDynamicReturnTypeExtensionTest.php b/tests/Type/Nette/FormContainerUntrustedValuesDynamicReturnTypeExtensionTest.php new file mode 100644 index 0000000..41215e6 --- /dev/null +++ b/tests/Type/Nette/FormContainerUntrustedValuesDynamicReturnTypeExtensionTest.php @@ -0,0 +1,51 @@ +assertFileAsserts($assertType, $file, ...$args); + } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/phpstan.neon', + ]; + } + +} diff --git a/tests/Type/Nette/data/FormContainerUntrustedValues.php b/tests/Type/Nette/data/FormContainerUntrustedValues.php new file mode 100644 index 0000000..53fcca5 --- /dev/null +++ b/tests/Type/Nette/data/FormContainerUntrustedValues.php @@ -0,0 +1,41 @@ +name = $name; + $this->name = $value; + } +} + +class FormContainerUntrustedValues +{ + public function test() + { + $form = new Form(); + $form->addText('name'); + $form->addText('value'); + + $dto = $form->getUntrustedValues(Dto::class); + $array = $form->getUntrustedValues('array'); + + assertType(Dto::class, $dto); + assertType('array', $array); + + assertType(ArrayHash::class, $form->getUntrustedValues()); + assertType(ArrayHash::class, $form->getUntrustedValues(null)); + } +}