Skip to content

Commit 844af5d

Browse files
committed
minor #18362 Add property types and return types in constraints (alexandre-daubois)
This PR was merged into the 6.2 branch. Discussion ---------- Add property types and return types in constraints Forgot a "few" ones in constraints 😄 Commits ------- e33a140 Add property types and return types in constraints and few other places
2 parents b8453ea + e33a140 commit 844af5d

File tree

97 files changed

+510
-349
lines changed

Some content is hidden

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

97 files changed

+510
-349
lines changed

cache.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ with either :class:`Symfony\\Contracts\\Cache\\CacheInterface` or
363363
// config/services.php
364364
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
365365
366-
return function(ContainerConfigurator $container) {
366+
return function(ContainerConfigurator $container): void {
367367
$container->services()
368368
// ...
369369

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::

event_dispatcher.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ notify Symfony that it is an event listener by using a special "tag":
9191
9292
use App\EventListener\ExceptionListener;
9393
94-
return function(ContainerConfigurator $container) {
94+
return function(ContainerConfigurator $container): void {
9595
$services = $container->services();
9696
9797
$services->set(ExceptionListener::class)

form/bootstrap5.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ configuration:
5757
// config/packages/twig.php
5858
use Symfony\Config\TwigConfig;
5959
60-
return static function(TwigConfig $twig) {
60+
return static function(TwigConfig $twig): void {
6161
$twig->formThemes(['bootstrap_5_layout.html.twig']);
6262
6363
// ...

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

0 commit comments

Comments
 (0)