Skip to content

Commit cf884e7

Browse files
committed
Rename Container::getUnsafeValues() to getUntrustedValues()
Mirroring the upstream change from 3.1.10: nette/forms@fb2df3b 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()`.
1 parent 488d326 commit cf884e7

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

extension.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ services:
104104
- phpstan.broker.dynamicMethodReturnTypeExtension
105105

106106
-
107-
class: PHPStan\Type\Nette\FormContainerUnsafeValuesDynamicReturnTypeExtension
107+
class: PHPStan\Type\Nette\FormContainerUntrustedValuesDynamicReturnTypeExtension
108108
tags:
109109
- phpstan.broker.dynamicMethodReturnTypeExtension
110110

src/Type/Nette/FormContainerUnsafeValuesDynamicReturnTypeExtension.php renamed to src/Type/Nette/FormContainerUntrustedValuesDynamicReturnTypeExtension.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use PHPStan\Type\Type;
1414
use function count;
1515

16-
class FormContainerUnsafeValuesDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
16+
class FormContainerUntrustedValuesDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
1717
{
1818

1919
public function getClass(): string
@@ -23,13 +23,13 @@ public function getClass(): string
2323

2424
public function isMethodSupported(MethodReflection $methodReflection): bool
2525
{
26-
return $methodReflection->getName() === 'getUnsafeValues';
26+
return $methodReflection->getName() === 'getUntrustedValues' || $methodReflection->getName() === 'getUnsafeValues';
2727
}
2828

2929
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
3030
{
3131
if (count($methodCall->getArgs()) === 0) {
32-
return null;
32+
return new ObjectType('Nette\Utils\ArrayHash');
3333
}
3434

3535
$arg = $methodCall->getArgs()[0]->value;
@@ -38,6 +38,10 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3838
return new ObjectType('Nette\Utils\ArrayHash');
3939
}
4040

41+
if ($scopedType->isClassString()->yes()) {
42+
return $scopedType->getClassStringObjectType();
43+
}
44+
4145
if (count($scopedType->getConstantStrings()) === 1 && $scopedType->getConstantStrings()[0]->getValue() === 'array') {
4246
return new ArrayType(new StringType(), new MixedType());
4347
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Nette;
4+
5+
use Composer\InstalledVersions;
6+
use OutOfBoundsException;
7+
use PHPStan\Testing\TypeInferenceTestCase;
8+
use function class_exists;
9+
use function version_compare;
10+
11+
final class FormContainerUntrustedValuesDynamicReturnTypeExtensionTest extends TypeInferenceTestCase
12+
{
13+
14+
public static function dataFileAsserts(): iterable
15+
{
16+
try {
17+
$formsVersion = class_exists(InstalledVersions::class)
18+
? InstalledVersions::getVersion('nette/forms')
19+
: null;
20+
} catch (OutOfBoundsException $e) {
21+
$formsVersion = null;
22+
}
23+
24+
if ($formsVersion !== null || version_compare($formsVersion, '3.1.10', '<')) {
25+
return;
26+
}
27+
28+
yield from self::gatherAssertTypes(__DIR__ . '/data/FormContainerUntrustedValues.php');
29+
}
30+
31+
/**
32+
* @dataProvider dataFileAsserts
33+
* @param mixed ...$args
34+
*/
35+
public function testFileAsserts(
36+
string $assertType,
37+
string $file,
38+
...$args
39+
): void
40+
{
41+
$this->assertFileAsserts($assertType, $file, ...$args);
42+
}
43+
44+
public static function getAdditionalConfigFiles(): array
45+
{
46+
return [
47+
__DIR__ . '/phpstan.neon',
48+
];
49+
}
50+
51+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace PHPStan\Type\Nette\Data\FormContainerUntrustedValues;
4+
5+
use Nette\Forms\Form;
6+
use Nette\Utils\ArrayHash;
7+
use function PHPStan\Testing\assertType;
8+
9+
class Dto
10+
{
11+
public string $name;
12+
public string $value;
13+
14+
public function __construct(
15+
string $name,
16+
string $value
17+
)
18+
{
19+
$this->name = $name;
20+
$this->name = $value;
21+
}
22+
}
23+
24+
class FormContainerUntrustedValues
25+
{
26+
public function test()
27+
{
28+
$form = new Form();
29+
$form->addText('name');
30+
$form->addText('value');
31+
32+
$dto = $form->getUntrustedValues(Dto::class);
33+
$array = $form->getUntrustedValues('array');
34+
35+
assertType(Dto::class, $dto);
36+
assertType('array<string, mixed>', $array);
37+
38+
assertType(ArrayHash::class, $form->getUntrustedValues());
39+
assertType(ArrayHash::class, $form->getUntrustedValues(null));
40+
}
41+
}

0 commit comments

Comments
 (0)