Skip to content

Commit f66c5ba

Browse files
committed
Add property types and constructor promotion.
1 parent e6f7bc5 commit f66c5ba

22 files changed

+101
-185
lines changed

config/services.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ services:
1515
autowire: true # Automatically injects dependencies in your services.
1616
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
1717
bind: # defines the scalar arguments once and apply them to any service defined/created in this file
18-
$locales: '%app_locales%'
19-
$defaultLocale: '%locale%'
20-
$emailSender: '%app.notifications.email_sender%'
18+
string $locales: '%app_locales%'
19+
string $defaultLocale: '%locale%'
20+
string $emailSender: '%app.notifications.email_sender%'
2121

2222
# makes classes in src/ available to be used as services
2323
# this creates a service per class whose id is the fully-qualified class name

src/Command/AddUserCommand.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,15 @@ class AddUserCommand extends Command
5252
// so it will be instantiated only when the command is actually called.
5353
protected static $defaultName = 'app:add-user';
5454

55-
/**
56-
* @var SymfonyStyle
57-
*/
58-
private $io;
59-
60-
private $entityManager;
61-
private $passwordEncoder;
62-
private $validator;
63-
private $users;
64-
65-
public function __construct(EntityManagerInterface $em, UserPasswordEncoderInterface $encoder, Validator $validator, UserRepository $users)
66-
{
55+
private SymfonyStyle $io;
56+
57+
public function __construct(
58+
private EntityManagerInterface $entityManager,
59+
private UserPasswordEncoderInterface $passwordEncoder,
60+
private Validator $validator,
61+
private UserRepository $users
62+
) {
6763
parent::__construct();
68-
69-
$this->entityManager = $em;
70-
$this->passwordEncoder = $encoder;
71-
$this->validator = $validator;
72-
$this->users = $users;
7364
}
7465

7566
/**

src/Command/DeleteUserCommand.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,14 @@ class DeleteUserCommand extends Command
4141
{
4242
protected static $defaultName = 'app:delete-user';
4343

44-
/** @var SymfonyStyle */
45-
private $io;
46-
private $entityManager;
47-
private $validator;
48-
private $users;
44+
private SymfonyStyle $io;
4945

50-
public function __construct(EntityManagerInterface $em, Validator $validator, UserRepository $users)
51-
{
46+
public function __construct(
47+
private EntityManagerInterface $entityManager,
48+
private Validator $validator,
49+
private UserRepository $users
50+
) {
5251
parent::__construct();
53-
54-
$this->entityManager = $em;
55-
$this->validator = $validator;
56-
$this->users = $users;
5752
}
5853

5954
/**

src/Command/ListUsersCommand.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,12 @@ class ListUsersCommand extends Command
4242
// a good practice is to use the 'app:' prefix to group all your custom application commands
4343
protected static $defaultName = 'app:list-users';
4444

45-
private $mailer;
46-
private $emailSender;
47-
private $users;
48-
49-
public function __construct(MailerInterface $mailer, $emailSender, UserRepository $users)
50-
{
45+
public function __construct(
46+
private MailerInterface $mailer,
47+
private string $emailSender,
48+
private UserRepository $users
49+
) {
5150
parent::__construct();
52-
53-
$this->mailer = $mailer;
54-
$this->emailSender = $emailSender;
55-
$this->users = $users;
5651
}
5752

5853
/**

src/DataFixtures/AppFixtures.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@
2323

2424
class AppFixtures extends Fixture
2525
{
26-
private $passwordEncoder;
27-
private $slugger;
28-
29-
public function __construct(UserPasswordEncoderInterface $passwordEncoder, SluggerInterface $slugger)
30-
{
31-
$this->passwordEncoder = $passwordEncoder;
32-
$this->slugger = $slugger;
26+
public function __construct(
27+
private UserPasswordEncoderInterface $passwordEncoder,
28+
private SluggerInterface $slugger
29+
) {
3330
}
3431

3532
public function load(ObjectManager $manager): void

src/Entity/Comment.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,19 @@
3131
class Comment
3232
{
3333
/**
34-
* @var int
35-
*
3634
* @ORM\Id
3735
* @ORM\GeneratedValue
3836
* @ORM\Column(type="integer")
3937
*/
40-
private $id;
38+
private ?int $id = null;
4139

4240
/**
43-
* @var Post
44-
*
4541
* @ORM\ManyToOne(targetEntity="Post", inversedBy="comments")
4642
* @ORM\JoinColumn(nullable=false)
4743
*/
48-
private $post;
44+
private ?Post $post = null;
4945

5046
/**
51-
* @var string
52-
*
5347
* @ORM\Column(type="text")
5448
*/
5549
#[
@@ -61,22 +55,18 @@ class Comment
6155
maxMessage: 'comment.too_long',
6256
)
6357
]
64-
private $content;
58+
private ?string $content = null;
6559

6660
/**
67-
* @var \DateTime
68-
*
6961
* @ORM\Column(type="datetime")
7062
*/
71-
private $publishedAt;
63+
private \DateTime $publishedAt;
7264

7365
/**
74-
* @var User
75-
*
7666
* @ORM\ManyToOne(targetEntity="App\Entity\User")
7767
* @ORM\JoinColumn(nullable=false)
7868
*/
79-
private $author;
69+
private ?User $author = null;
8070

8171
public function __construct()
8272
{

src/Entity/Post.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,31 @@
3636
class Post
3737
{
3838
/**
39-
* @var int
40-
*
4139
* @ORM\Id
4240
* @ORM\GeneratedValue
4341
* @ORM\Column(type="integer")
4442
*/
45-
private $id;
43+
private ?int $id = null;
4644

4745
/**
48-
* @var string
49-
*
5046
* @ORM\Column(type="string")
5147
*/
5248
#[Assert\NotBlank]
53-
private $title;
49+
private ?string $title = null;
5450

5551
/**
56-
* @var string
57-
*
5852
* @ORM\Column(type="string")
5953
*/
60-
private $slug;
54+
private ?string $slug = null;
6155

6256
/**
63-
* @var string
64-
*
6557
* @ORM\Column(type="string")
6658
*/
6759
#[
6860
Assert\NotBlank(message: 'post.blank_summary'),
69-
Assert\Length(max: 255),
61+
Assert\Length(max: 255)
7062
]
71-
private $summary;
63+
private ?string $summary = null;
7264

7365
/**
7466
* @var string
@@ -79,25 +71,21 @@ class Post
7971
Assert\NotBlank(message: 'post.blank_content'),
8072
Assert\Length(min: 10, minMessage: 'post.too_short_content')
8173
]
82-
private $content;
74+
private ?string $content = null;
8375

8476
/**
85-
* @var \DateTime
86-
*
8777
* @ORM\Column(type="datetime")
8878
*/
89-
private $publishedAt;
79+
private \DateTime $publishedAt;
9080

9181
/**
92-
* @var User
93-
*
9482
* @ORM\ManyToOne(targetEntity="App\Entity\User")
9583
* @ORM\JoinColumn(nullable=false)
9684
*/
97-
private $author;
85+
private ?User $author = null;
9886

9987
/**
100-
* @var Comment[]|ArrayCollection
88+
* @var Comment[]|Collection
10189
*
10290
* @ORM\OneToMany(
10391
* targetEntity="Comment",
@@ -107,17 +95,17 @@ class Post
10795
* )
10896
* @ORM\OrderBy({"publishedAt": "DESC"})
10997
*/
110-
private $comments;
98+
private Collection $comments;
11199

112100
/**
113-
* @var Tag[]|ArrayCollection
101+
* @var Tag[]|Collection
114102
*
115103
* @ORM\ManyToMany(targetEntity="App\Entity\Tag", cascade={"persist"})
116104
* @ORM\JoinTable(name="symfony_demo_post_tag")
117105
* @ORM\OrderBy({"name": "ASC"})
118106
*/
119107
#[Assert\Count(max: 4, maxMessage: 'post.too_many_tags')]
120-
private $tags;
108+
private Collection $tags;
121109

122110
public function __construct()
123111
{

src/Entity/Tag.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,16 @@
2626
class Tag implements \JsonSerializable
2727
{
2828
/**
29-
* @var int
30-
*
3129
* @ORM\Id
3230
* @ORM\GeneratedValue
3331
* @ORM\Column(type="integer")
3432
*/
35-
private $id;
33+
private ?int $id = null;
3634

3735
/**
38-
* @var string
39-
*
4036
* @ORM\Column(type="string", unique=true)
4137
*/
42-
private $name;
38+
private ?string $name = null;
4339

4440
public function getId(): ?int
4541
{

src/Entity/User.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,54 +31,42 @@
3131
class User implements UserInterface, \Serializable
3232
{
3333
/**
34-
* @var int
35-
*
3634
* @ORM\Id
3735
* @ORM\GeneratedValue
3836
* @ORM\Column(type="integer")
3937
*/
40-
private $id;
38+
private ?int $id = null;
4139

4240
/**
43-
* @var string
44-
*
4541
* @ORM\Column(type="string")
4642
*/
4743
#[Assert\NotBlank]
48-
private $fullName;
44+
private ?string $fullName = null;
4945

5046
/**
51-
* @var string
52-
*
5347
* @ORM\Column(type="string", unique=true)
5448
*/
5549
#[
5650
Assert\NotBlank,
5751
Assert\Length(min: 2, max: 50)
5852
]
59-
private $username;
53+
private ?string $username = null;
6054

6155
/**
62-
* @var string
63-
*
6456
* @ORM\Column(type="string", unique=true)
6557
*/
6658
#[Assert\Email]
67-
private $email;
59+
private ?string $email = null;
6860

6961
/**
70-
* @var string
71-
*
7262
* @ORM\Column(type="string")
7363
*/
74-
private $password;
64+
private ?string $password = null;
7565

7666
/**
77-
* @var array
78-
*
7967
* @ORM\Column(type="json")
8068
*/
81-
private $roles = [];
69+
private array $roles = [];
8270

8371
public function getId(): ?int
8472
{

src/Event/CommentCreatedEvent.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
class CommentCreatedEvent extends Event
1818
{
19-
protected $comment;
20-
21-
public function __construct(Comment $comment)
22-
{
23-
$this->comment = $comment;
19+
public function __construct(
20+
protected Comment $comment
21+
) {
2422
}
2523

2624
public function getComment(): Comment

src/EventSubscriber/CheckRequirementsSubscriber.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@
3030
*/
3131
class CheckRequirementsSubscriber implements EventSubscriberInterface
3232
{
33-
private $entityManager;
34-
35-
public function __construct(EntityManagerInterface $entityManager)
36-
{
37-
$this->entityManager = $entityManager;
33+
public function __construct(
34+
private EntityManagerInterface $entityManager
35+
) {
3836
}
3937

4038
// Event Subscribers must define this method to declare the events they

0 commit comments

Comments
 (0)