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)); + } +}