Skip to content
Merged
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
30 changes: 22 additions & 8 deletions src/Type/Nette/FormContainerValuesDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,36 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return $methodReflection->getName() === 'getValues';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
if (count($methodCall->getArgs()) === 0) {
$args = $methodCall->getArgs();

if (count($args) === 0) {
return new ObjectType('Nette\Utils\ArrayHash');
}

$arg = $methodCall->getArgs()[0]->value;
$arg = $args[0]->value;
$scopedType = $scope->getType($arg);
if ($scopedType->isTrue()->yes()) {
return new ArrayType(new StringType(), new MixedType());
}
if ($scopedType->isFalse()->yes()) {

$constantStrings = $scopedType->getConstantStrings();

if (count($constantStrings) === 0) {
return new ObjectType('Nette\Utils\ArrayHash');
}

return null;
$constantString = $constantStrings[0];

$value = $constantString->getValue();

if ($scopedType->isClassString()->yes()) {
return $scopedType->getClassStringObjectType();
}

if ($value === 'array') {
return new ArrayType(new StringType(), new MixedType());
}

return new ObjectType('Nette\Utils\ArrayHash');
}

}
109 changes: 20 additions & 89 deletions tests/Type/Nette/FormContainerValuesDynamicReturnTypeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,106 +2,37 @@

namespace PHPStan\Type\Nette;

use Nette\Utils\ArrayHash;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use PHPUnit\Framework\TestCase;
use PHPStan\Testing\TypeInferenceTestCase;

final class FormContainerValuesDynamicReturnTypeExtensionTest extends TestCase
final class FormContainerValuesDynamicReturnTypeExtensionTest extends TypeInferenceTestCase
{

private FormContainerValuesDynamicReturnTypeExtension $extension;

protected function setUp(): void
/**
* @return iterable<string, mixed[]>
*/
public static function dataFileAsserts(): iterable
{
$this->extension = new FormContainerValuesDynamicReturnTypeExtension();
yield from self::gatherAssertTypes(__DIR__ . '/data/FormContainerModel.php');
}

public function testParameterAsArray(): void
/**
* @dataProvider dataFileAsserts
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args
): void
{
$methodReflection = $this->createMock(MethodReflection::class);
$methodReflection
->method('getVariants')
->willReturn([new FunctionVariant(
TemplateTypeMap::createEmpty(),
TemplateTypeMap::createEmpty(),
[],
true,
new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(ArrayHash::class))]),
)]);

$scope = $this->createMock(Scope::class);
$scope->method('getType')->willReturn(new ConstantBooleanType(true));

$methodCall = $this->createMock(MethodCall::class);
$arg = $this->createMock(Arg::class);
$value = $this->createMock(Expr::class);
$arg->value = $value;
$methodCall->args = [
0 => $arg,
];
$methodCall->method('getArgs')->willReturn($methodCall->args);

$resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope);

self::assertInstanceOf(ArrayType::class, $resultType);
$this->assertFileAsserts($assertType, $file, ...$args);
}

public function testParameterAsArrayHash(): void
public static function getAdditionalConfigFiles(): array
{
$methodReflection = $this->createMock(MethodReflection::class);
$methodReflection
->method('getVariants')
->willReturn([new FunctionVariant(TemplateTypeMap::createEmpty(), TemplateTypeMap::createEmpty(), [], true, new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(ArrayHash::class))]))]);

$scope = $this->createMock(Scope::class);
$scope->method('getType')->willReturn(new ConstantBooleanType(false));

$methodCall = $this->createMock(MethodCall::class);
$arg = $this->createMock(Arg::class);
$value = $this->createMock(Expr::class);
$arg->value = $value;
$methodCall->args = [
0 => $arg,
return [
__DIR__ . '/phpstan.neon',
];
$methodCall->method('getArgs')->willReturn($methodCall->args);

$resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope);

self::assertInstanceOf(ObjectType::class, $resultType);
self::assertSame(ArrayHash::class, $resultType->describe(VerbosityLevel::value()));
}

public function testDefaultParameterIsArrayHash(): void
{
$methodReflection = $this->createMock(MethodReflection::class);
$methodReflection
->method('getVariants')
->willReturn([new FunctionVariant(TemplateTypeMap::createEmpty(), TemplateTypeMap::createEmpty(), [], true, new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(ArrayHash::class))]))]);

$scope = $this->createMock(Scope::class);
$scope->method('getType')->willReturn(new ConstantBooleanType(false));

$methodCall = $this->createMock(MethodCall::class);
$methodCall->args = [];
$methodCall->method('getArgs')->willReturn($methodCall->args);

$resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope);

self::assertInstanceOf(ObjectType::class, $resultType);
self::assertSame(ArrayHash::class, $resultType->describe(VerbosityLevel::value()));
}

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

namespace PHPStan\Type\Nette\Data\FormContainerModel;

use Nette\Forms\Form;
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 FormContainerModel
{
public function test()
{
$form = new Form();
$form->addText('name');
$form->addText('value');

$dto = $form->getValues(Dto::class);
$array = $form->getValues('array');

assertType(Dto::class, $dto);
assertType('array<string, mixed>', $array);
}
}