Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ services:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PHPStan\Type\Nette\FormContainerUnsafeValuesDynamicReturnTypeExtension
class: PHPStan\Type\Nette\FormContainerUntrustedValuesDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PHPStan\Type\Type;
use function count;

class FormContainerUnsafeValuesDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
class FormContainerUntrustedValuesDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
Expand All @@ -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;
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Nette;

use Composer\InstalledVersions;
use OutOfBoundsException;
use PHPStan\Testing\TypeInferenceTestCase;
use function class_exists;
use function version_compare;

final class FormContainerUntrustedValuesDynamicReturnTypeExtensionTest extends TypeInferenceTestCase
{

public static function dataFileAsserts(): iterable
{
try {
$formsVersion = class_exists(InstalledVersions::class)
? InstalledVersions::getVersion('nette/forms')
: null;
} catch (OutOfBoundsException $e) {
$formsVersion = null;
}

if ($formsVersion === null || version_compare($formsVersion, '3.1.10', '<')) {
return;
}

yield from self::gatherAssertTypes(__DIR__ . '/data/FormContainerUntrustedValues.php');
}

/**
* @dataProvider dataFileAsserts
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/phpstan.neon',
];
}

}
41 changes: 41 additions & 0 deletions tests/Type/Nette/data/FormContainerUntrustedValues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace PHPStan\Type\Nette\Data\FormContainerUntrustedValues;

use Nette\Forms\Form;
use Nette\Utils\ArrayHash;
use function PHPStan\Testing\assertType;

class Dto
{
public string $name;
public string $value;

public function __construct(
string $name,
string $value
)
{
$this->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<string, mixed>', $array);

assertType(ArrayHash::class, $form->getUntrustedValues());
assertType(ArrayHash::class, $form->getUntrustedValues(null));
}
}