AnnotationToAttributeRector: ensure bool param in attribute (#6254)

This commit is contained in:
Maxim Tugaev 2021-04-27 14:41:29 +03:00 committed by GitHub
parent 94cf4314e6
commit a5a99317b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 0 deletions

View File

@ -10,7 +10,9 @@ use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
@ -104,6 +106,14 @@ final class PhpAttributeGroupFactory
return $value->getValue();
}
if ($value instanceof ConstExprTrueNode) {
return true;
}
if ($value instanceof ConstExprFalseNode) {
return false;
}
if ($value instanceof Node) {
return (string) $value;
}

View File

@ -0,0 +1,37 @@
<?php
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;
use Symfony\Component\Validator\Constraints as Assert;
final class EntityColumnAndAssertNotBlankWithBoolean
{
/**
* @Assert\NotBlank(allowNull=true)
*/
public $name;
/**
* @Assert\NotBlank(allowNull=false)
*/
public $surname;
}
?>
-----
<?php
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;
use Symfony\Component\Validator\Constraints as Assert;
final class EntityColumnAndAssertNotBlankWithBoolean
{
#[\Symfony\Component\Validator\Constraints\NotBlank(allowNull: true)]
public $name;
#[\Symfony\Component\Validator\Constraints\NotBlank(allowNull: false)]
public $surname;
}
?>

View File

@ -33,6 +33,10 @@ return static function (ContainerConfigurator $containerConfigurator): void {
'Symfony\Component\Validator\Constraints\Range',
'Symfony\Component\Validator\Constraints\Range'
),
new AnnotationToAttribute(
'Symfony\Component\Validator\Constraints\NotBlank',
'Symfony\Component\Validator\Constraints\NotBlank'
),
]),
]]);
};