Skip to content

Commit b6c7f68

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

Some content is hidden

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

76 files changed

+437
-274
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/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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ An Invoice entity::
6969
* @var InvoiceSubjectInterface
7070
*/
7171
#[ORM\ManyToOne(targetEntity: InvoiceSubjectInterface::class)]
72-
protected $subject;
72+
protected InvoiceSubjectInterface $subject;
7373
}
7474

7575
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ following ``Task`` class::
4343

4444
class Task
4545
{
46-
protected $task;
47-
protected $dueDate;
46+
protected string $task;
47+
protected ?\DateTime $dueDate;
4848

4949
public function getTask(): string
5050
{
@@ -486,11 +486,11 @@ object.
486486
class Task
487487
{
488488
#[Assert\NotBlank]
489-
public $task;
489+
public string $task;
490490
491491
#[Assert\NotBlank]
492492
#[Assert\Type(\DateTime::class)]
493-
protected $dueDate;
493+
protected \DateTime $dueDate;
494494
}
495495
496496
.. code-block:: yaml

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' => [

reference/constraints/Cascade.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ constraints that are set in the child classes ``BookMetadata`` and
3535
class BookCollection
3636
{
3737
#[Assert\NotBlank]
38-
protected $name = '';
38+
protected string $name = '';
3939
4040
public BookMetadata $metadata;
4141
@@ -76,7 +76,7 @@ constraints that are set in the child classes ``BookMetadata`` and
7676
{
7777
// ...
7878
79-
public static function loadValidatorMetadata(ClassMetadata $metadata)
79+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
8080
{
8181
$metadata->addConstraint(new Assert\Cascade());
8282
}

reference/constraints/Choice.rst

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ If your valid choice list is simple, you can pass them in directly via the
3232
3333
class Author
3434
{
35-
const GENRES = ['fiction', 'non-fiction'];
35+
public const GENRES = ['fiction', 'non-fiction'];
3636
3737
#[Assert\Choice(['New York', 'Berlin', 'Tokyo'])]
38-
protected $city;
38+
protected string $city;
3939
4040
#[Assert\Choice(choices: Author::GENRES, message: 'Choose a valid genre.')]
41-
protected $genre;
41+
protected string $genre;
4242
}
4343
4444
.. code-block:: yaml
@@ -91,7 +91,9 @@ If your valid choice list is simple, you can pass them in directly via the
9191
9292
class Author
9393
{
94-
public static function loadValidatorMetadata(ClassMetadata $metadata)
94+
// ...
95+
96+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
9597
{
9698
$metadata->addPropertyConstraint(
9799
'city',
@@ -138,7 +140,7 @@ constraint.
138140
class Author
139141
{
140142
#[Assert\Choice(callback: 'getGenres')]
141-
protected $genre;
143+
protected string $genre;
142144
}
143145
144146
.. code-block:: yaml
@@ -176,9 +178,9 @@ constraint.
176178
177179
class Author
178180
{
179-
protected $genre;
181+
// ...
180182
181-
public static function loadValidatorMetadata(ClassMetadata $metadata)
183+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
182184
{
183185
$metadata->addPropertyConstraint('genre', new Assert\Choice([
184186
'callback' => 'getGenres',
@@ -202,7 +204,7 @@ you can pass the class name and the method as an array.
202204
class Author
203205
{
204206
#[Assert\Choice(callback: [Genre::class, 'getGenres'])]
205-
protected $genre;
207+
protected string $genre;
206208
}
207209
208210
.. code-block:: yaml
@@ -244,7 +246,9 @@ you can pass the class name and the method as an array.
244246
245247
class Author
246248
{
247-
public static function loadValidatorMetadata(ClassMetadata $metadata)
249+
// ...
250+
251+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
248252
{
249253
$metadata->addPropertyConstraint('genre', new Assert\Choice([
250254
'callback' => [Genre::class, 'getGenres'],

reference/constraints/Cidr.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Basic Usage
2727
class NetworkSettings
2828
{
2929
#[Assert\Cidr]
30-
protected $cidrNotation;
30+
protected string $cidrNotation;
3131
}
3232
3333
.. code-block:: yaml
@@ -63,7 +63,9 @@ Basic Usage
6363
6464
class NetworkSettings
6565
{
66-
public static function loadValidatorMetadata(ClassMetadata $metadata)
66+
// ...
67+
68+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
6769
{
6870
$metadata->addPropertyConstraint('cidrNotation', new Assert\Cidr());
6971
}

0 commit comments

Comments
 (0)