@@ -48,6 +48,7 @@ private function loadUsers(ObjectManager $manager): void
48
48
$ user ->setRoles ($ roles );
49
49
50
50
$ manager ->persist ($ user );
51
+
51
52
$ this ->addReference ($ username , $ user );
52
53
}
53
54
@@ -60,6 +61,7 @@ private function loadTags(ObjectManager $manager): void
60
61
$ tag = new Tag ($ name );
61
62
62
63
$ manager ->persist ($ tag );
64
+
63
65
$ this ->addReference ('tag- ' .$ name , $ tag );
64
66
}
65
67
@@ -79,11 +81,8 @@ private function loadPosts(ObjectManager $manager): void
79
81
$ post ->addTag (...$ tags );
80
82
81
83
foreach (range (1 , 5 ) as $ i ) {
82
- /** @var User $commentAuthor */
83
- $ commentAuthor = $ this ->getReference ('john_user ' );
84
-
85
84
$ comment = new Comment ();
86
- $ comment ->setAuthor ($ commentAuthor );
85
+ $ comment ->setAuthor ($ this -> getReference ( ' john_user ' , User::class) );
87
86
$ comment ->setContent ($ this ->getRandomText (random_int (255 , 512 )));
88
87
$ comment ->setPublishedAt (new \DateTimeImmutable ('now + ' .$ i .'seconds ' ));
89
88
@@ -138,18 +137,14 @@ private function getPostData(): array
138
137
139
138
foreach ($ this ->getPhrases () as $ i => $ title ) {
140
139
// $postData = [$title, $slug, $summary, $content, $publishedAt, $author, $tags, $comments];
141
-
142
- /** @var User $user */
143
- $ user = $ this ->getReference (['jane_admin ' , 'tom_admin ' ][0 === $ i ? 0 : random_int (0 , 1 )]);
144
-
145
140
$ posts [] = [
146
141
$ title ,
147
142
$ this ->slugger ->slug ($ title )->lower (),
148
143
$ this ->getRandomText (),
149
144
$ this ->getPostContent (),
150
145
(new \DateTimeImmutable ('now - ' .$ i .'days ' ))->setTime (random_int (8 , 17 ), random_int (7 , 49 ), random_int (0 , 59 )),
151
146
// Ensure that the first post is written by Jane Doe to simplify tests
152
- $ user ,
147
+ $ this -> getReference ([ ' jane_admin ' , ' tom_admin ' ][ 0 === $ i ? 0 : random_int ( 0 , 1 )], User::class) ,
153
148
$ this ->getRandomTags (),
154
149
];
155
150
}
@@ -260,11 +255,9 @@ private function getRandomTags(): array
260
255
shuffle ($ tagNames );
261
256
$ selectedTags = \array_slice ($ tagNames , 0 , random_int (2 , 4 ));
262
257
263
- return array_map (function ($ tagName ) {
264
- /** @var Tag $tag */
265
- $ tag = $ this ->getReference ('tag- ' .$ tagName );
266
-
267
- return $ tag ;
268
- }, $ selectedTags );
258
+ return array_map (
259
+ fn ($ tagName ) => $ this ->getReference ('tag- ' .$ tagName , Tag::class),
260
+ $ selectedTags
261
+ );
269
262
}
270
263
}
0 commit comments