Skip to content

Commit 4a4ed4a

Browse files
committed
minor #18551 Add property types (alexandre-daubois)
This PR was merged into the 6.2 branch. Discussion ---------- Add property types I'll do one more additional pass for methods return types in a separate PR. Will be easier to review 😄 Commits ------- d3e3df0 Add property types
2 parents 14995a6 + d3e3df0 commit 4a4ed4a

30 files changed

+93
-110
lines changed

components/asset.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ every day::
203203

204204
class DateVersionStrategy implements VersionStrategyInterface
205205
{
206-
private $version;
206+
private \DateTimeInterface $version;
207207

208208
public function __construct()
209209
{

components/dependency_injection.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ you want to make available as a service::
3131

3232
class Mailer
3333
{
34-
private $transport;
34+
private string $transport;
3535

3636
public function __construct()
3737
{
@@ -55,7 +55,7 @@ so this is passed into the constructor::
5555
class Mailer
5656
{
5757
public function __construct(
58-
private $transport,
58+
private string $transport,
5959
) {
6060
}
6161

@@ -124,7 +124,7 @@ it was only optional then you could use setter injection instead::
124124

125125
class NewsletterManager
126126
{
127-
private $mailer;
127+
private \Mailer $mailer;
128128

129129
public function setMailer(\Mailer $mailer)
130130
{

components/property_access.rst

+7-7
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ it with ``get``. So the actual method becomes ``getFirstName()``::
118118
// ...
119119
class Person
120120
{
121-
private $firstName = 'Wouter';
121+
private string $firstName = 'Wouter';
122122

123123
public function getFirstName()
124124
{
@@ -140,8 +140,8 @@ getters, this means that you can do something like this::
140140
// ...
141141
class Person
142142
{
143-
private $author = true;
144-
private $children = [];
143+
private bool $author = true;
144+
private array $children = [];
145145

146146
public function isAuthor()
147147
{
@@ -233,7 +233,7 @@ The ``getValue()`` method can also use the magic ``__get()`` method::
233233
// ...
234234
class Person
235235
{
236-
private $children = [
236+
private array $children = [
237237
'Wouter' => [...],
238238
];
239239

@@ -263,7 +263,7 @@ enable this feature by using :class:`Symfony\\Component\\PropertyAccess\\Propert
263263
// ...
264264
class Person
265265
{
266-
private $children = [
266+
private array $children = [
267267
'wouter' => [...],
268268
];
269269

@@ -362,7 +362,7 @@ see `Enable other Features`_::
362362
// ...
363363
class Person
364364
{
365-
private $children = [];
365+
private array $children = [];
366366

367367
public function __call($name, $args)
368368
{
@@ -405,7 +405,7 @@ properties through *adder* and *remover* methods::
405405
/**
406406
* @var string[]
407407
*/
408-
private $children = [];
408+
private array $children = [];
409409

410410
public function getChildren(): array
411411
{

components/property_info.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ information from annotations of properties and methods, such as ``@var``,
431431
* @param string $bar
432432
*/
433433
public function __construct(
434-
private $bar,
434+
private string $bar,
435435
) {
436436
}
437437
}

components/runtime.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ always using this ``ReactPHPRunner``::
440440

441441
class ReactPHPRuntime extends GenericRuntime
442442
{
443-
private $port;
443+
private int $port;
444444

445445
public function __construct(array $options)
446446
{

components/serializer.rst

+24-24
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ Assume you have the following plain-old-PHP object::
244244

245245
class MyObj
246246
{
247-
public $foo;
247+
public string $foo;
248248

249-
private $bar;
249+
private string $bar;
250250

251251
public function getBar()
252252
{
@@ -303,10 +303,10 @@ Then, create your groups definition:
303303
class MyObj
304304
{
305305
#[Groups(['group1', 'group2'])]
306-
public $foo;
306+
public string $foo;
307307
308308
#[Groups(['group4'])]
309-
public $anotherProperty;
309+
public string $anotherProperty;
310310
311311
#[Groups(['group3'])]
312312
public function getBar() // is* methods are also supported
@@ -449,10 +449,10 @@ Option 1: Using ``@Ignore`` Annotation
449449
450450
class MyClass
451451
{
452-
public $foo;
452+
public string $foo;
453453
454454
#[Ignore]
455-
public $bar;
455+
public string $bar;
456456
}
457457
458458
.. code-block:: yaml
@@ -1229,8 +1229,8 @@ You can change this behavior by setting the ``AbstractObjectNormalizer::SKIP_NUL
12291229
to ``true``::
12301230

12311231
$dummy = new class {
1232-
public $foo;
1233-
public $bar = 'notNull';
1232+
public ?string $foo = null;
1233+
public string $bar = 'notNull';
12341234
};
12351235

12361236
$normalizer = new ObjectNormalizer();
@@ -1305,8 +1305,8 @@ Circular references are common when dealing with entity relations::
13051305

13061306
class Organization
13071307
{
1308-
private $name;
1309-
private $members;
1308+
private string $name;
1309+
private array $members;
13101310

13111311
public function setName($name)
13121312
{
@@ -1331,10 +1331,10 @@ Circular references are common when dealing with entity relations::
13311331

13321332
class Member
13331333
{
1334-
private $name;
1335-
private $organization;
1334+
private string $name;
1335+
private Organization $organization;
13361336

1337-
public function setName($name)
1337+
public function setName(string $name)
13381338
{
13391339
$this->name = $name;
13401340
}
@@ -1404,12 +1404,12 @@ structure::
14041404

14051405
class MyObj
14061406
{
1407-
public $foo;
1407+
public string $foo;
14081408

14091409
/**
14101410
* @var self
14111411
*/
1412-
public $child;
1412+
public MyObj $child;
14131413
}
14141414

14151415
$level1 = new MyObj();
@@ -1437,7 +1437,7 @@ Here, we set it to 2 for the ``$child`` property:
14371437
class MyObj
14381438
{
14391439
#[MaxDepth(2)]
1440-
public $child;
1440+
public MyObj $child;
14411441
14421442
// ...
14431443
}
@@ -1499,10 +1499,10 @@ having unique identifiers::
14991499

15001500
class Foo
15011501
{
1502-
public $id;
1502+
public int $id;
15031503

15041504
#[MaxDepth(1)]
1505-
public $child;
1505+
public MyObj $child;
15061506
}
15071507

15081508
$level1 = new Foo();
@@ -1598,8 +1598,8 @@ context option::
15981598
class MyObj
15991599
{
16001600
public function __construct(
1601-
private $foo,
1602-
private $bar,
1601+
private string $foo,
1602+
private string $bar,
16031603
) {
16041604
}
16051605
}
@@ -1638,8 +1638,8 @@ parameter of the ``ObjectNormalizer``::
16381638

16391639
class ObjectOuter
16401640
{
1641-
private $inner;
1642-
private $date;
1641+
private ObjectInner $inner;
1642+
private \DateTimeInterface $date;
16431643

16441644
public function getInner()
16451645
{
@@ -1664,8 +1664,8 @@ parameter of the ``ObjectNormalizer``::
16641664

16651665
class ObjectInner
16661666
{
1667-
public $foo;
1668-
public $bar;
1667+
public string $foo;
1668+
public string $bar;
16691669
}
16701670

16711671
$normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());

components/validator/metadata.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ the ``Author`` class has at least 3 characters::
1717

1818
class Author
1919
{
20-
private $firstName;
20+
private string $firstName;
2121

2222
public static function loadValidatorMetadata(ClassMetadata $metadata)
2323
{

configuration/using_parameters_in_dic.rst

+2-3
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,10 @@ be injected with this parameter via the extension as follows::
101101

102102
class Configuration implements ConfigurationInterface
103103
{
104-
private $debug;
104+
private bool $debug;
105105

106-
public function __construct($debug)
106+
public function __construct(private bool $debug)
107107
{
108-
$this->debug = (bool) $debug;
109108
}
110109

111110
public function getConfigTreeBuilder()

contributing/code/standards.rst

+1-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ short example containing most features described below::
4949
{
5050
public const SOME_CONST = 42;
5151

52-
/**
53-
* @var string
54-
*/
55-
private $fooBar;
52+
private string $fooBar;
5653

5754
/**
5855
* @param $dummy some argument description

controller/upload_file.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ add a PDF brochure for each product. To do so, add a new property called
2222
// ...
2323

2424
#[ORM\Column(type: 'string')]
25-
private $brochureFilename;
25+
private string $brochureFilename;
2626

2727
public function getBrochureFilename(): string
2828
{
@@ -238,7 +238,7 @@ logic to a separate service::
238238
class FileUploader
239239
{
240240
public function __construct(
241-
private $targetDirectory,
241+
private string $targetDirectory,
242242
private SluggerInterface $slugger,
243243
) {
244244
}

doctrine/associations.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ the ``Product`` entity (and getter & setter methods):
148148
// ...
149149
150150
#[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'products')]
151-
private $category;
151+
private Category $category;
152152
153153
public function getCategory(): ?Category
154154
{
@@ -220,7 +220,7 @@ class that will hold these objects:
220220
// ...
221221
222222
#[ORM\OneToMany(targetEntity: Product::class, mappedBy: 'category')]
223-
private $products;
223+
private array $products;
224224
225225
public function __construct()
226226
{
@@ -588,7 +588,7 @@ that behavior, use the `orphanRemoval`_ option inside ``Category``:
588588
// ...
589589
590590
#[ORM\OneToMany(targetEntity: Product::class, mappedBy: 'category', orphanRemoval: true)]
591-
private $products;
591+
private array $products;
592592
593593
594594
Thanks to this, if the ``Product`` is removed from the ``Category``, it will be

form/create_form_type_extension.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ the database::
107107
/**
108108
* @var string The path - typically stored in the database
109109
*/
110-
private $path;
110+
private string $path;
111111

112112
// ...
113113

form/inherit_data_option.rst

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ entities, a ``Company`` and a ``Customer``::
1010

1111
class Company
1212
{
13-
private $name;
14-
private $website;
13+
private string $name;
14+
private string $website;
1515

16-
private $address;
17-
private $zipcode;
18-
private $city;
19-
private $country;
16+
private string $address;
17+
private string $zipcode;
18+
private string $city;
19+
private string $country;
2020
}
2121

2222
.. code-block:: php
@@ -26,13 +26,13 @@ entities, a ``Company`` and a ``Customer``::
2626
2727
class Customer
2828
{
29-
private $firstName;
30-
private $lastName;
29+
private string $firstName;
30+
private string $lastName;
3131
32-
private $address;
33-
private $zipcode;
34-
private $city;
35-
private $country;
32+
private string $address;
33+
private string $zipcode;
34+
private string $city;
35+
private string $country;
3636
}
3737
3838
As you can see, each entity shares a few of the same fields: ``address``,

form/unit_testing.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ make sure the ``FormRegistry`` uses the created instance::
154154

155155
class TestedTypeTest extends TypeTestCase
156156
{
157-
private $objectManager;
157+
private MockObject|ObjectManager $objectManager;
158158

159159
protected function setUp(): void
160160
{

0 commit comments

Comments
 (0)