Skip to content

Commit 2339f19

Browse files
Add property types and return types in constraints
1 parent ca2ac32 commit 2339f19

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+452
-291
lines changed

components/options_resolver.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Imagine you have a ``Mailer`` class which has four options: ``host``,
2323

2424
class Mailer
2525
{
26-
protected $options;
26+
protected array $options;
2727

2828
public function __construct(array $options = [])
2929
{
@@ -37,7 +37,7 @@ check which options are set::
3737
class Mailer
3838
{
3939
// ...
40-
public function sendMail($from, $to)
40+
public function sendMail($from, $to): void
4141
{
4242
$mail = ...;
4343

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

889-
protected $options;
889+
protected array $options;
890890

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

905-
public function configureOptions(OptionsResolver $resolver)
905+
public function configureOptions(OptionsResolver $resolver): void
906906
{
907907
// ...
908908
}
@@ -917,9 +917,9 @@ method ``clearOptionsConfig()`` and call it periodically::
917917
// ...
918918
class Mailer
919919
{
920-
private static $resolversByClass = [];
920+
private static array $resolversByClass = [];
921921

922-
public static function clearOptionsConfig()
922+
public static function clearOptionsConfig(): void
923923
{
924924
self::$resolversByClass = [];
925925
}

components/serializer.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ exists in your project::
7373
private int $age;
7474
private string $name;
7575
private bool $sportsperson;
76-
private ?\DateTime $createdAt;
76+
private ?\DateTimeInterface $createdAt;
7777

7878
// Getters
7979
public function getAge(): int
@@ -113,7 +113,7 @@ exists in your project::
113113
$this->sportsperson = $sportsperson;
114114
}
115115

116-
public function setCreatedAt(\DateTime $createdAt = null): void
116+
public function setCreatedAt(\DateTimeInterface $createdAt = null): void
117117
{
118118
$this->createdAt = $createdAt;
119119
}
@@ -607,11 +607,11 @@ processes::
607607
class Person
608608
{
609609
public function __construct(
610-
private $firstName,
610+
private string $firstName,
611611
) {
612612
}
613613

614-
public function getFirstName()
614+
public function getFirstName(): string
615615
{
616616
return $this->firstName;
617617
}
@@ -663,7 +663,7 @@ defines a ``Person`` entity with a ``firstName`` property:
663663
{
664664
public function __construct(
665665
#[SerializedName('customer_name')]
666-
private $firstName,
666+
private string $firstName,
667667
) {
668668
}
669669

components/validator/resources.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ In this example, the validation metadata is retrieved executing the
3737

3838
class User
3939
{
40-
protected $name;
40+
protected string $name;
4141

42-
public static function loadValidatorMetadata(ClassMetadata $metadata)
42+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
4343
{
4444
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
4545
$metadata->addPropertyConstraint('name', new Assert\Length([
@@ -99,7 +99,7 @@ prefixed classes included in doc block comments (``/** ... */``). For example::
9999
/**
100100
* @Assert\NotBlank
101101
*/
102-
protected $name;
102+
protected string $name;
103103
}
104104

105105
To enable the annotation loader, call the

components/var_dumper.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,9 @@ then its dump representation::
372372
373373
class PropertyExample
374374
{
375-
public $publicProperty = 'The `+` prefix denotes public properties,';
376-
protected $protectedProperty = '`#` protected ones and `-` private ones.';
377-
private $privateProperty = 'Hovering a property shows a reminder.';
375+
public string $publicProperty = 'The `+` prefix denotes public properties,';
376+
protected string $protectedProperty = '`#` protected ones and `-` private ones.';
377+
private string $privateProperty = 'Hovering a property shows a reminder.';
378378
}
379379
380380
$var = new PropertyExample();
@@ -391,7 +391,7 @@ then its dump representation::
391391
392392
class DynamicPropertyExample
393393
{
394-
public $declaredProperty = 'This property is declared in the class definition';
394+
public string $declaredProperty = 'This property is declared in the class definition';
395395
}
396396
397397
$var = new DynamicPropertyExample();
@@ -404,7 +404,7 @@ then its dump representation::
404404
405405
class ReferenceExample
406406
{
407-
public $info = "Circular and sibling references are displayed as `#number`.\nHovering them highlights all instances in the same dump.\n";
407+
public string $info = "Circular and sibling references are displayed as `#number`.\nHovering them highlights all instances in the same dump.\n";
408408
}
409409
$var = new ReferenceExample();
410410
$var->aCircularReference = $var;

components/var_exporter.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ following class hierarchy::
5050

5151
abstract class AbstractClass
5252
{
53-
protected $foo;
54-
private $bar;
53+
protected int $foo;
54+
private int $bar;
5555

56-
protected function setBar($bar)
56+
protected function setBar($bar): void
5757
{
5858
$this->bar = $bar;
5959
}

doctrine/resolve_target_entity.rst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,8 @@ An Invoice entity::
6565
#[ORM\Table(name: 'invoice')]
6666
class Invoice
6767
{
68-
/**
69-
* @var InvoiceSubjectInterface
70-
*/
7168
#[ORM\ManyToOne(targetEntity: InvoiceSubjectInterface::class)]
72-
protected $subject;
69+
protected InvoiceSubjectInterface $subject;
7370
}
7471

7572
An InvoiceSubjectInterface::

form/form_collections.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Let's start by creating a ``Task`` entity::
1616

1717
class Task
1818
{
19-
protected $description;
20-
protected $tags;
19+
protected string $description;
20+
protected ArrayCollection $tags;
2121

2222
public function __construct()
2323
{
@@ -53,7 +53,7 @@ objects::
5353

5454
class Tag
5555
{
56-
private $name;
56+
private string $name;
5757

5858
public function getName(): string
5959
{
@@ -466,7 +466,7 @@ you will learn about next!).
466466
// ...
467467
468468
#[ORM\ManyToMany(targetEntity: Tag::class, cascade: ['persist'])]
469-
protected $tags;
469+
protected array $tags;
470470
471471
.. code-block:: yaml
472472

forms.rst

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ following ``Task`` class::
4343

4444
class Task
4545
{
46-
protected $task;
47-
protected $dueDate;
46+
protected string $task;
47+
48+
protected ?\DateTimeInterface $dueDate;
4849

4950
public function getTask(): string
5051
{
@@ -56,12 +57,12 @@ following ``Task`` class::
5657
$this->task = $task;
5758
}
5859

59-
public function getDueDate(): ?\DateTime
60+
public function getDueDate(): ?\DateTimeInterface
6061
{
6162
return $this->dueDate;
6263
}
6364

64-
public function setDueDate(?\DateTime $dueDate): void
65+
public function setDueDate(?\DateTimeInterface $dueDate): void
6566
{
6667
$this->dueDate = $dueDate;
6768
}
@@ -128,7 +129,7 @@ use the ``createFormBuilder()`` helper::
128129
// creates a task object and initializes some data for this example
129130
$task = new Task();
130131
$task->setTask('Write a blog post');
131-
$task->setDueDate(new \DateTime('tomorrow'));
132+
$task->setDueDate(new \DateTimeImmutable('tomorrow'));
132133

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

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

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

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

477478
.. configuration-block::
@@ -486,11 +487,11 @@ object.
486487
class Task
487488
{
488489
#[Assert\NotBlank]
489-
public $task;
490+
public string $task;
490491
491492
#[Assert\NotBlank]
492-
#[Assert\Type(\DateTime::class)]
493-
protected $dueDate;
493+
#[Assert\Type(\DateTimeInterface::class)]
494+
protected \DateTimeInterface $dueDate;
494495
}
495496
496497
.. code-block:: yaml
@@ -502,7 +503,7 @@ object.
502503
- NotBlank: ~
503504
dueDate:
504505
- NotBlank: ~
505-
- Type: \DateTime
506+
- Type: \DateTimeInterface
506507
507508
.. code-block:: xml
508509
@@ -519,7 +520,7 @@ object.
519520
</property>
520521
<property name="dueDate">
521522
<constraint name="NotBlank"/>
522-
<constraint name="Type">\DateTime</constraint>
523+
<constraint name="Type">\DateTimeInterface</constraint>
523524
</property>
524525
</class>
525526
</constraint-mapping>
@@ -544,7 +545,7 @@ object.
544545
$metadata->addPropertyConstraint('dueDate', new NotBlank());
545546
$metadata->addPropertyConstraint(
546547
'dueDate',
547-
new Type(\DateTime::class)
548+
new Type(\DateTimeInterface::class)
548549
);
549550
}
550551
}

reference/constraints/All.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ entry in that array:
3131
new Assert\NotBlank,
3232
new Assert\Length(min: 5),
3333
])]
34-
protected $favoriteColors = [];
34+
protected array $favoriteColors = [];
3535
}
3636
3737
.. code-block:: yaml
@@ -77,7 +77,7 @@ entry in that array:
7777
7878
class User
7979
{
80-
public static function loadValidatorMetadata(ClassMetadata $metadata)
80+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
8181
{
8282
$metadata->addPropertyConstraint('favoriteColors', new Assert\All([
8383
'constraints' => [

reference/constraints/AtLeastOneOf.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ The following constraints ensure that:
3535
new Assert\Regex('/#/'),
3636
new Assert\Length(min: 10),
3737
])]
38-
protected $plainPassword;
38+
protected string $plainPassword;
3939
4040
#[Assert\AtLeastOneOf([
4141
new Assert\Count(min: 3),
4242
new Assert\All(
4343
new Assert\GreaterThanOrEqual(5)
4444
),
4545
])]
46-
protected $grades;
46+
protected array $grades;
4747
}
4848
4949
.. code-block:: yaml
@@ -113,7 +113,7 @@ The following constraints ensure that:
113113
114114
class Student
115115
{
116-
public static function loadValidatorMetadata(ClassMetadata $metadata)
116+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
117117
{
118118
$metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf([
119119
'constraints' => [

reference/constraints/Bic.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ will contain a Business Identifier Code (BIC).
3030
class Transaction
3131
{
3232
#[Assert\Bic]
33-
protected $businessIdentifierCode;
33+
protected string $businessIdentifierCode;
3434
}
3535
3636
.. code-block:: yaml
@@ -66,7 +66,7 @@ will contain a Business Identifier Code (BIC).
6666
6767
class Transaction
6868
{
69-
public static function loadValidatorMetadata(ClassMetadata $metadata)
69+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
7070
{
7171
$metadata->addPropertyConstraint('businessIdentifierCode', new Assert\Bic());
7272
}

reference/constraints/Blank.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ of an ``Author`` class were blank, you could do the following:
3737
class Author
3838
{
3939
#[Assert\Blank]
40-
protected $firstName;
40+
protected string $firstName;
4141
}
4242
4343
.. code-block:: yaml
@@ -73,7 +73,7 @@ of an ``Author`` class were blank, you could do the following:
7373
7474
class Author
7575
{
76-
public static function loadValidatorMetadata(ClassMetadata $metadata)
76+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
7777
{
7878
$metadata->addPropertyConstraint('firstName', new Assert\Blank());
7979
}

reference/constraints/CardScheme.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ on an object that will contain a credit card number.
3232
schemes: [Assert\CardScheme::VISA],
3333
message: 'Your credit card number is invalid.',
3434
)]
35-
protected $cardNumber;
35+
protected string $cardNumber;
3636
}
3737
3838
.. code-block:: yaml
@@ -75,7 +75,7 @@ on an object that will contain a credit card number.
7575
7676
class Transaction
7777
{
78-
public static function loadValidatorMetadata(ClassMetadata $metadata)
78+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
7979
{
8080
$metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme([
8181
'schemes' => [

0 commit comments

Comments
 (0)