Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 12 | class ArticlePostMessageSpec extends ObjectBehavior |
||
| 13 | { |
||
| 14 | function it_is_initializable() |
||
| 15 | { |
||
| 16 | $this->shouldHaveType(ArticlePostMessage::class); |
||
| 17 | } |
||
| 18 | |||
| 19 | function it_should_build() |
||
| 20 | { |
||
| 21 | $translation = new ArticleTranslationMessage(); |
||
| 22 | $translation->setLocale('en'); |
||
| 23 | $translation->setTitle('English title'); |
||
| 24 | $translation->setBody('English content'); |
||
| 25 | |||
| 26 | $media = new ArticleMediaMessage(); |
||
| 27 | $media->setMediaId(1); |
||
| 28 | $media->setDisplayOrder(5); |
||
| 29 | |||
| 30 | $this->setStatus(Article::STATUS_PUBLISHED); |
||
| 31 | $this->setCategoryIds([1, 2]); |
||
| 32 | $this->setMediaLimit(5); |
||
| 33 | $this->setShareOnFacebook(true); |
||
| 34 | $this->addTranslation($translation); |
||
| 35 | $this->addMedia($media); |
||
| 36 | |||
| 37 | $translationData = [ |
||
| 38 | 'title' => 'English title', |
||
| 39 | 'body' => 'English content', |
||
| 40 | ]; |
||
| 41 | |||
| 42 | $mediaData = [ |
||
| 43 | 'media' => 1, |
||
| 44 | 'displayOrder' => 5, |
||
| 45 | ]; |
||
| 46 | |||
| 47 | $data = [ |
||
| 48 | 'translations' => ['en' => $translationData], |
||
| 49 | 'status' => 'published', |
||
| 50 | 'categories' => [1, 2], |
||
| 51 | 'articleMedias' => [$mediaData], |
||
| 52 | 'mediaLimit' => 5, |
||
| 53 | 'shareOnFacebook' => true, |
||
| 54 | ]; |
||
| 55 | |||
| 56 | $this->build()->shouldReturn($data); |
||
| 57 | } |
||
| 58 | } |
||
| 59 |