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
14 changes: 11 additions & 3 deletions src/Type/Nette/FormContainerValuesDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
}

// nette/forms 3.2.9+ and methods overriding it can describe a more precise return type in PHPDoc,
// older versions state just object|array
return $returnTag->getType()->isSuperTypeOf(
// older versions state just object|array (3.0 - 3.2.8) or ArrayHash|array (2.x)
$returnType = $returnTag->getType();
if ($returnType->isSuperTypeOf(
TypeCombinator::union(new ArrayType(new MixedType(), new MixedType()), new ObjectWithoutClassType()),
)->yes();
)->yes()) {
return true;
}

$arrayHashOrArray = TypeCombinator::union(new ArrayType(new MixedType(), new MixedType()), new ObjectType('Nette\Utils\ArrayHash'));

return $returnType->isSuperTypeOf($arrayHashOrArray)->yes()
&& $arrayHashOrArray->isSuperTypeOf($returnType)->yes();
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class FormContainerValuesDynamicReturnTypeExtensionTest extends TypeInfere
public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/FormContainerOverriddenValues.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/FormContainerArrayHashOrArrayValues.php');

try {
$formsVersion = class_exists(InstalledVersions::class)
Expand Down
53 changes: 53 additions & 0 deletions tests/Type/Nette/data/FormContainerArrayHashOrArrayValues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace PHPStan\Type\Nette\Data\FormContainerArrayHashOrArrayValues;

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

class CustomValues extends ArrayHash
{

}

/**
* nette/forms 2.x describes the return type of getValues() as ArrayHash|array
*/
class FormWithArrayHashOrArrayValues extends Form
{

/**
* @return ArrayHash|array
*/
public function getValues($returnType = null, ?array $controls = null)
{
return parent::getValues($returnType, $controls);
}

}

class FormWithNarrowerValues extends Form
{

/**
* @return CustomValues|array<string, string>
*/
public function getValues($returnType = null, ?array $controls = null)
{
return parent::getValues($returnType, $controls);
}

}

function (FormWithArrayHashOrArrayValues $form): void {
assertType('array<string, mixed>', $form->getValues(true));
assertType('array<string, mixed>', $form->getValues('array'));
assertType(ArrayHash::class, $form->getValues());
assertType(ArrayHash::class, $form->getValues(null));
};

function (FormWithNarrowerValues $form): void {
assertType('array<string, string>|' . CustomValues::class, $form->getValues(true));
assertType('array<string, string>|' . CustomValues::class, $form->getValues());
};
Loading