@@ -48,6 +48,7 @@ private function loadUsers(ObjectManager $manager): void
4848 $ user ->setRoles ($ roles );
4949
5050 $ manager ->persist ($ user );
51+
5152 $ this ->addReference ($ username , $ user );
5253 }
5354
@@ -60,6 +61,7 @@ private function loadTags(ObjectManager $manager): void
6061 $ tag = new Tag ($ name );
6162
6263 $ manager ->persist ($ tag );
64+
6365 $ this ->addReference ('tag- ' .$ name , $ tag );
6466 }
6567
@@ -79,11 +81,8 @@ private function loadPosts(ObjectManager $manager): void
7981 $ post ->addTag (...$ tags );
8082
8183 foreach (range (1 , 5 ) as $ i ) {
82- /** @var User $commentAuthor */
83- $ commentAuthor = $ this ->getReference ('john_user ' );
84-
8584 $ comment = new Comment ();
86- $ comment ->setAuthor ($ commentAuthor );
85+ $ comment ->setAuthor ($ this -> getReference ( ' john_user ' , User::class) );
8786 $ comment ->setContent ($ this ->getRandomText (random_int (255 , 512 )));
8887 $ comment ->setPublishedAt (new \DateTimeImmutable ('now + ' .$ i .'seconds ' ));
8988
@@ -138,18 +137,14 @@ private function getPostData(): array
138137
139138 foreach ($ this ->getPhrases () as $ i => $ title ) {
140139 // $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-
145140 $ posts [] = [
146141 $ title ,
147142 $ this ->slugger ->slug ($ title )->lower (),
148143 $ this ->getRandomText (),
149144 $ this ->getPostContent (),
150145 (new \DateTimeImmutable ('now - ' .$ i .'days ' ))->setTime (random_int (8 , 17 ), random_int (7 , 49 ), random_int (0 , 59 )),
151146 // 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) ,
153148 $ this ->getRandomTags (),
154149 ];
155150 }
@@ -260,11 +255,9 @@ private function getRandomTags(): array
260255 shuffle ($ tagNames );
261256 $ selectedTags = \array_slice ($ tagNames , 0 , random_int (2 , 4 ));
262257
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+ );
269262 }
270263}
0 commit comments