Conditions | 14 |
Paths | 937 |
Total Lines | 87 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
40 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
41 | { |
||
42 | $io = new SymfonyStyle($input, $output); |
||
43 | |||
44 | $count = $input->getArgument('count'); |
||
45 | // get faker |
||
46 | if (!class_exists(Factory::class)) { |
||
47 | $io->error('Faker should be installed. Run `composer install --dev`'); |
||
48 | return ExitCode::UNSPECIFIED_ERROR; |
||
49 | } |
||
50 | $faker = Factory::create(); |
||
51 | |||
52 | // users |
||
53 | $users = []; |
||
54 | for ($i = 0; $i <= $count; ++$i) { |
||
55 | $user = new User(); |
||
56 | $user->setLogin($login = $faker->firstName . rand(0, 9999)); |
||
57 | $user->setPassword($login); |
||
58 | $users[] = $user; |
||
59 | } |
||
60 | // tags |
||
61 | /** @var TagRepository $tagRepository */ |
||
62 | $tagRepository = $this->orm->getRepository(Tag::class); |
||
63 | $tags = []; |
||
64 | $tagWords = []; |
||
65 | for ($i = 0, $fails = 0; $i <= $count; ++$i) { |
||
66 | $word = $faker->word(); |
||
67 | if (in_array($word, $tagWords, true)) { |
||
68 | --$i; |
||
69 | ++$fails; |
||
70 | if ($fails >= $count) { |
||
71 | break; |
||
72 | } |
||
73 | continue; |
||
74 | } |
||
75 | $tagWords[] = $word; |
||
76 | $tag = $tagRepository->getOrCreate($word); |
||
77 | $tags[] = $tag; |
||
78 | } |
||
79 | // posts |
||
80 | for ($i = 0; $i <= $count; ++$i) { |
||
81 | /** @var User $postUser */ |
||
82 | $postUser = $users[array_rand($users)]; |
||
83 | $post = new Post(); |
||
84 | $post->setUser($postUser); |
||
85 | $postUser->addPost($post); |
||
86 | $post->setTitle($faker->text(64)); |
||
87 | $post->setContent($faker->realText(4000)); |
||
88 | $public = rand(0, 2) > 0; |
||
89 | $post->setPublic($public); |
||
90 | if ($public) { |
||
91 | $post->setPublishedAt(new \DateTimeImmutable(date('r', rand(time(), strtotime('-2 years'))))); |
||
92 | } |
||
93 | // tags |
||
94 | $postTags = (array)array_rand($tags, rand(1, count($tags))); |
||
95 | foreach ($postTags as $tagId) { |
||
96 | $post->addTag($tags[$tagId]); |
||
97 | } |
||
98 | // comments |
||
99 | $commentsCount = rand(0, $count); |
||
100 | for ($j = 0; $j <= $commentsCount; ++$j) { |
||
101 | $comment = new Comment(); |
||
102 | $comment->setContent($faker->realText(500)); |
||
103 | $commentPublic = rand(0, 3) > 0; |
||
104 | $comment->setPublic($commentPublic); |
||
105 | if ($commentPublic) { |
||
106 | $comment->setPublishedAt(new \DateTimeImmutable(date('r', rand(time(), strtotime('-1 years'))))); |
||
107 | } |
||
108 | $commentUser = $users[array_rand($users)]; |
||
109 | $comment->setUser($commentUser); |
||
110 | $post->addComment($comment); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | try { |
||
115 | $transaction = new Transaction($this->orm); |
||
116 | foreach ($users as $user) { |
||
117 | $transaction->persist($user); |
||
118 | } |
||
119 | $transaction->run(); |
||
120 | $io->success('Done'); |
||
121 | } catch (\Throwable $t) { |
||
122 | $io->error($t->getMessage()); |
||
123 | return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR; |
||
124 | } |
||
125 | |||
126 | return ExitCode::OK; |
||
127 | } |
||
129 |