| Conditions | 6 |
| Paths | 10 |
| Total Lines | 56 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 89 | public function load(array $options): void |
||
| 90 | { |
||
| 91 | $options = $this->optionsResolver->resolve($options); |
||
| 92 | |||
| 93 | $channels = $this->channelRepository->findAll(); |
||
| 94 | |||
| 95 | $this->createArticleCategories(); |
||
| 96 | |||
| 97 | /** @var ChannelInterface $channel */ |
||
| 98 | foreach ($channels as $channel) { |
||
| 99 | $imageIndex = 1; |
||
|
|
|||
| 100 | for ($i=1; $i <= $options['articles_per_channel']; $i++) { |
||
| 101 | /** @var ArticleInterface $article */ |
||
| 102 | $article = $this->articleFactory->createNew(); |
||
| 103 | |||
| 104 | $article->addChannel($channel); |
||
| 105 | $article->setCode($this->faker->md5); |
||
| 106 | $article->setEnabled(rand(1, 100) > 30); |
||
| 107 | |||
| 108 | // Add categories |
||
| 109 | $categories = $this->faker->randomElements($this->categories, rand(1, count($this->categories))); |
||
| 110 | |||
| 111 | /** @var ArticleCategoryInterface $category */ |
||
| 112 | foreach ($categories as $category) { |
||
| 113 | $article->addCategory($category); |
||
| 114 | } |
||
| 115 | |||
| 116 | // Set translatable fields |
||
| 117 | foreach ($this->getLocales() as $localeCode) { |
||
| 118 | $article->setCurrentLocale($localeCode); |
||
| 119 | $article->setFallbackLocale($localeCode); |
||
| 120 | |||
| 121 | $article->getTranslation()->setTitle($this->faker->text(20)); |
||
| 122 | $article->getTranslation()->setContent($this->faker->text); |
||
| 123 | $article->getTranslation()->setSlug($this->faker->slug); |
||
| 124 | } |
||
| 125 | |||
| 126 | // Add images |
||
| 127 | $srcImage = new UploadedFile($this->faker->image(null, 1200, 350), $article->getTitle()); |
||
| 128 | /** @var ArticleImageInterface $image */ |
||
| 129 | $image = $this->articleImageFactory->createNew(); |
||
| 130 | $image->setFile($srcImage); |
||
| 131 | $article->addImage($image); |
||
| 132 | |||
| 133 | $this->imageUploader->uploadImages(new ResourceControllerEvent($article)); |
||
| 134 | |||
| 135 | // Add comments |
||
| 136 | if (rand(0, 100) > 50) { |
||
| 137 | $this->addArticleComments($article); |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->objectManager->persist($article); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | $this->objectManager->flush(); |
||
| 145 | } |
||
| 214 |