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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"phpunit/phpunit": "^9.5",
"symfony/config": "^4.2 || ^5.0",
"symfony/console": "^4.0 || ^5.0",
"symfony/form": "^4.0 || ^5.0",
"symfony/framework-bundle": "^4.4 || ^5.0",
"symfony/http-foundation": "^4.0 || ^5.0",
"symfony/messenger": "^4.2 || ^5.0",
Expand Down
5 changes: 5 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,8 @@ services:
class: PHPStan\Symfony\InputBagStubFilesExtension
tags:
- phpstan.stubFilesExtension

# FormInterface::getErrors() return type
-
factory: PHPStan\Type\Symfony\Form\FormInterfaceDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
64 changes: 64 additions & 0 deletions src/Type/Symfony/Form/FormInterfaceDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Symfony\Form;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormErrorIterator;
use Symfony\Component\Form\FormInterface;

final class FormInterfaceDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return FormInterface::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'getErrors';
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
if (!isset($methodCall->getArgs()[1])) {
return new GenericObjectType(FormErrorIterator::class, [new ObjectType(FormError::class)]);
}

$firstArgType = $scope->getType($methodCall->getArgs()[0]->value);
$secondArgType = $scope->getType($methodCall->getArgs()[1]->value);

$firstIsTrueType = (new ConstantBooleanType(true))->isSuperTypeOf($firstArgType);
$firstIsFalseType = (new ConstantBooleanType(false))->isSuperTypeOf($firstArgType);
$secondIsTrueType = (new ConstantBooleanType(true))->isSuperTypeOf($secondArgType);
$secondIsFalseType = (new ConstantBooleanType(false))->isSuperTypeOf($secondArgType);

$firstCompareType = $firstIsTrueType->compareTo($firstIsFalseType);
$secondCompareType = $secondIsTrueType->compareTo($secondIsFalseType);

if ($firstCompareType === $firstIsTrueType && $secondCompareType === $secondIsFalseType) {
return new GenericObjectType(FormErrorIterator::class, [
new UnionType([
new ObjectType(FormError::class),
new ObjectType(FormErrorIterator::class),
]),
]);
}

return new GenericObjectType(FormErrorIterator::class, [new ObjectType(FormError::class)]);
}

}
2 changes: 2 additions & 0 deletions tests/Type/Symfony/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function dataFileAsserts(): iterable
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/denormalizer.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/FormInterface_getErrors.php');
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Type/Symfony/data/FormInterface_getErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormErrorIterator;
use Symfony\Component\Form\FormInterface;
use function PHPStan\Testing\assertType;

/** @var FormInterface $form */
$form = new stdClass();

assertType(FormErrorIterator::class . '<'. FormError::class .'>', $form->getErrors());
assertType(FormErrorIterator::class . '<'. FormError::class .'>', $form->getErrors(false));
assertType(FormErrorIterator::class . '<'. FormError::class .'>', $form->getErrors(false, true));

assertType(FormErrorIterator::class . '<'. FormError::class .'>', $form->getErrors(true));
assertType(FormErrorIterator::class . '<'. FormError::class .'>', $form->getErrors(true, true));

assertType(FormErrorIterator::class . '<'. FormError::class .'>', $form->getErrors(false, false));

assertType(FormErrorIterator::class . '<'. FormError::class .'|'. FormErrorIterator::class . '>', $form->getErrors(true, false));