Skip to content

Add property types and return types in constraints #18362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
2 changes: 1 addition & 1 deletion cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ with either :class:`Symfony\\Contracts\\Cache\\CacheInterface` or
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return function(ContainerConfigurator $container) {
return function(ContainerConfigurator $container): void {
$container->services()
// ...

Expand Down
14 changes: 7 additions & 7 deletions components/options_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Imagine you have a ``Mailer`` class which has four options: ``host``,

class Mailer
{
protected $options;
protected array $options;

public function __construct(array $options = [])
{
Expand All @@ -37,7 +37,7 @@ check which options are set::
class Mailer
{
// ...
public function sendMail($from, $to)
public function sendMail($from, $to): void
{
$mail = ...;

Expand Down Expand Up @@ -884,9 +884,9 @@ can change your code to do the configuration only once per class::
// ...
class Mailer
{
private static $resolversByClass = [];
private static array $resolversByClass = [];

protected $options;
protected array $options;

public function __construct(array $options = [])
{
Expand All @@ -902,7 +902,7 @@ can change your code to do the configuration only once per class::
$this->options = self::$resolversByClass[$class]->resolve($options);
}

public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
// ...
}
Expand All @@ -917,9 +917,9 @@ method ``clearOptionsConfig()`` and call it periodically::
// ...
class Mailer
{
private static $resolversByClass = [];
private static array $resolversByClass = [];

public static function clearOptionsConfig()
public static function clearOptionsConfig(): void
{
self::$resolversByClass = [];
}
Expand Down
10 changes: 5 additions & 5 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ exists in your project::
private int $age;
private string $name;
private bool $sportsperson;
private ?\DateTime $createdAt;
private ?\DateTimeInterface $createdAt;

// Getters
public function getAge(): int
Expand Down Expand Up @@ -113,7 +113,7 @@ exists in your project::
$this->sportsperson = $sportsperson;
}

public function setCreatedAt(\DateTime $createdAt = null): void
public function setCreatedAt(\DateTimeInterface $createdAt = null): void
{
$this->createdAt = $createdAt;
}
Expand Down Expand Up @@ -607,11 +607,11 @@ processes::
class Person
{
public function __construct(
private $firstName,
private string $firstName,
) {
}

public function getFirstName()
public function getFirstName(): string
{
return $this->firstName;
}
Expand Down Expand Up @@ -663,7 +663,7 @@ defines a ``Person`` entity with a ``firstName`` property:
{
public function __construct(
#[SerializedName('customer_name')]
private $firstName,
private string $firstName,
) {
}

Expand Down
6 changes: 3 additions & 3 deletions components/validator/resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ In this example, the validation metadata is retrieved executing the

class User
{
protected $name;
protected string $name;

public static function loadValidatorMetadata(ClassMetadata $metadata)
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
$metadata->addPropertyConstraint('name', new Assert\Length([
Expand Down Expand Up @@ -99,7 +99,7 @@ prefixed classes included in doc block comments (``/** ... */``). For example::
/**
* @Assert\NotBlank
*/
protected $name;
protected string $name;
}

To enable the annotation loader, call the
Expand Down
10 changes: 5 additions & 5 deletions components/var_dumper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ then its dump representation::

class PropertyExample
{
public $publicProperty = 'The `+` prefix denotes public properties,';
protected $protectedProperty = '`#` protected ones and `-` private ones.';
private $privateProperty = 'Hovering a property shows a reminder.';
public string $publicProperty = 'The `+` prefix denotes public properties,';
protected string $protectedProperty = '`#` protected ones and `-` private ones.';
private string $privateProperty = 'Hovering a property shows a reminder.';
}

$var = new PropertyExample();
Expand All @@ -391,7 +391,7 @@ then its dump representation::

class DynamicPropertyExample
{
public $declaredProperty = 'This property is declared in the class definition';
public string $declaredProperty = 'This property is declared in the class definition';
}

$var = new DynamicPropertyExample();
Expand All @@ -404,7 +404,7 @@ then its dump representation::

class ReferenceExample
{
public $info = "Circular and sibling references are displayed as `#number`.\nHovering them highlights all instances in the same dump.\n";
public string $info = "Circular and sibling references are displayed as `#number`.\nHovering them highlights all instances in the same dump.\n";
}
$var = new ReferenceExample();
$var->aCircularReference = $var;
Expand Down
6 changes: 3 additions & 3 deletions components/var_exporter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ following class hierarchy::

abstract class AbstractClass
{
protected $foo;
private $bar;
protected int $foo;
private int $bar;

protected function setBar($bar)
protected function setBar($bar): void
{
$this->bar = $bar;
}
Expand Down
5 changes: 1 addition & 4 deletions doctrine/resolve_target_entity.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ An Invoice entity::
#[ORM\Table(name: 'invoice')]
class Invoice
{
/**
* @var InvoiceSubjectInterface
*/
#[ORM\ManyToOne(targetEntity: InvoiceSubjectInterface::class)]
protected $subject;
protected InvoiceSubjectInterface $subject;
}

An InvoiceSubjectInterface::
Expand Down
2 changes: 1 addition & 1 deletion event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ notify Symfony that it is an event listener by using a special "tag":

use App\EventListener\ExceptionListener;

return function(ContainerConfigurator $container) {
return function(ContainerConfigurator $container): void {
$services = $container->services();

$services->set(ExceptionListener::class)
Expand Down
2 changes: 1 addition & 1 deletion form/bootstrap5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ configuration:
// config/packages/twig.php
use Symfony\Config\TwigConfig;

return static function(TwigConfig $twig) {
return static function(TwigConfig $twig): void {
$twig->formThemes(['bootstrap_5_layout.html.twig']);

// ...
Expand Down
8 changes: 4 additions & 4 deletions form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Let's start by creating a ``Task`` entity::

class Task
{
protected $description;
protected $tags;
protected string $description;
protected ArrayCollection $tags;

public function __construct()
{
Expand Down Expand Up @@ -53,7 +53,7 @@ objects::

class Tag
{
private $name;
private string $name;

public function getName(): string
{
Expand Down Expand Up @@ -466,7 +466,7 @@ you will learn about next!).
// ...

#[ORM\ManyToMany(targetEntity: Tag::class, cascade: ['persist'])]
protected $tags;
protected array $tags;

.. code-block:: yaml

Expand Down
27 changes: 14 additions & 13 deletions forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ following ``Task`` class::

class Task
{
protected $task;
protected $dueDate;
protected string $task;

protected ?\DateTimeInterface $dueDate;

public function getTask(): string
{
Expand All @@ -56,12 +57,12 @@ following ``Task`` class::
$this->task = $task;
}

public function getDueDate(): ?\DateTime
public function getDueDate(): ?\DateTimeInterface
{
return $this->dueDate;
}

public function setDueDate(?\DateTime $dueDate): void
public function setDueDate(?\DateTimeInterface $dueDate): void
{
$this->dueDate = $dueDate;
}
Expand Down Expand Up @@ -128,7 +129,7 @@ use the ``createFormBuilder()`` helper::
// creates a task object and initializes some data for this example
$task = new Task();
$task->setTask('Write a blog post');
$task->setDueDate(new \DateTime('tomorrow'));
$task->setDueDate(new \DateTimeImmutable('tomorrow'));

$form = $this->createFormBuilder($task)
->add('task', TextType::class)
Expand Down Expand Up @@ -209,7 +210,7 @@ use the ``createForm()`` helper (otherwise, use the ``create()`` method of the
// creates a task object and initializes some data for this example
$task = new Task();
$task->setTask('Write a blog post');
$task->setDueDate(new \DateTime('tomorrow'));
$task->setDueDate(new \DateTimeImmutable('tomorrow'));

$form = $this->createForm(TaskType::class, $task);

Expand Down Expand Up @@ -471,7 +472,7 @@ to a class. You can add them either to the entity class or to the form class.

To see the first approach - adding constraints to the entity - in action,
add the validation constraints, so that the ``task`` field cannot be empty,
and the ``dueDate`` field cannot be empty, and must be a valid ``DateTime``
and the ``dueDate`` field cannot be empty, and must be a valid ``DateTimeImmutable``
object.

.. configuration-block::
Expand All @@ -486,11 +487,11 @@ object.
class Task
{
#[Assert\NotBlank]
public $task;
public string $task;

#[Assert\NotBlank]
#[Assert\Type(\DateTime::class)]
protected $dueDate;
#[Assert\Type(\DateTimeInterface::class)]
protected \DateTimeInterface $dueDate;
}

.. code-block:: yaml
Expand All @@ -502,7 +503,7 @@ object.
- NotBlank: ~
dueDate:
- NotBlank: ~
- Type: \DateTime
- Type: \DateTimeInterface

.. code-block:: xml

Expand All @@ -519,7 +520,7 @@ object.
</property>
<property name="dueDate">
<constraint name="NotBlank"/>
<constraint name="Type">\DateTime</constraint>
<constraint name="Type">\DateTimeInterface</constraint>
</property>
</class>
</constraint-mapping>
Expand All @@ -544,7 +545,7 @@ object.
$metadata->addPropertyConstraint('dueDate', new NotBlank());
$metadata->addPropertyConstraint(
'dueDate',
new Type(\DateTime::class)
new Type(\DateTimeInterface::class)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/custom_version_strategy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ After creating the strategy PHP class, register it as a Symfony service.
use App\Asset\VersionStrategy\GulpBusterVersionStrategy;
use Symfony\Component\DependencyInjection\Definition;

return function(ContainerConfigurator $container) {
return function(ContainerConfigurator $container): void {
$services = $container->services();

$services->set(GulpBusterVersionStrategy::class)
Expand Down
2 changes: 1 addition & 1 deletion messenger/multiple_buses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ you can determine the message bus based on an implemented interface:
use App\MessageHandler\CommandHandlerInterface;
use App\MessageHandler\QueryHandlerInterface;

return function(ContainerConfigurator $container) {
return function(ContainerConfigurator $container): void {
$services = $container->services();

// ...
Expand Down
2 changes: 1 addition & 1 deletion profiler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ you'll need to configure the data collector explicitly:

use App\DataCollector\RequestCollector;

return function(ContainerConfigurator $container) {
return function(ContainerConfigurator $container): void {
$services = $container->services();

$services->set(RequestCollector::class)
Expand Down
4 changes: 2 additions & 2 deletions reference/constraints/All.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ entry in that array:
new Assert\NotBlank,
new Assert\Length(min: 5),
])]
protected $favoriteColors = [];
protected array $favoriteColors = [];
}

.. code-block:: yaml
Expand Down Expand Up @@ -77,7 +77,7 @@ entry in that array:

class User
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('favoriteColors', new Assert\All([
'constraints' => [
Expand Down
6 changes: 3 additions & 3 deletions reference/constraints/AtLeastOneOf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ The following constraints ensure that:
new Assert\Regex('/#/'),
new Assert\Length(min: 10),
])]
protected $plainPassword;
protected string $plainPassword;

#[Assert\AtLeastOneOf([
new Assert\Count(min: 3),
new Assert\All(
new Assert\GreaterThanOrEqual(5)
),
])]
protected $grades;
protected array $grades;
}

.. code-block:: yaml
Expand Down Expand Up @@ -113,7 +113,7 @@ The following constraints ensure that:

class Student
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf([
'constraints' => [
Expand Down
Loading