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 |
||
16 | class ArticlePatchMessageSpec extends ObjectBehavior |
||
17 | { |
||
18 | function it_is_initializable() |
||
19 | { |
||
20 | $this->shouldHaveType(ArticlePatchMessage::class); |
||
21 | } |
||
22 | |||
23 | function it_should_build() |
||
24 | { |
||
25 | $translation = new ArticleTranslationMessage(); |
||
26 | $translation->setLocale('en'); |
||
27 | $translation->setTitle('English title'); |
||
28 | $translation->setBody('English content'); |
||
29 | |||
30 | $media = new ArticleMediaMessage(); |
||
31 | $media->setMediaId(1); |
||
32 | $media->setDisplayOrder(5); |
||
33 | |||
34 | $this->setStatus(Article::STATUS_PUBLISHED); |
||
35 | $this->setCategoryIds([1, 2]); |
||
36 | $this->setMediaLimit(5); |
||
37 | $this->setShareOnFacebook(true); |
||
38 | $this->addTranslation($translation); |
||
39 | $this->addMedia($media); |
||
40 | |||
41 | $translationData = [ |
||
42 | 'title' => 'English title', |
||
43 | 'body' => 'English content', |
||
44 | ]; |
||
45 | |||
46 | $mediaData = [ |
||
47 | 'media' => 1, |
||
48 | 'displayOrder' => 5, |
||
49 | ]; |
||
50 | |||
51 | $data = [ |
||
52 | 'translations' => ['en' => $translationData], |
||
53 | 'status' => 'published', |
||
54 | 'categories' => [1, 2], |
||
55 | 'articleMedias' => [$mediaData], |
||
56 | 'mediaLimit' => 5, |
||
57 | 'shareOnFacebook' => true, |
||
58 | ]; |
||
59 | |||
60 | $this->build()->shouldReturn($data); |
||
61 | } |
||
62 | |||
63 | function it_should_create_from_article( |
||
64 | Article $article, |
||
65 | Category $category, |
||
66 | ArticleTranslation $translation, |
||
67 | Media $media, |
||
68 | ArticleMedia $articleMedia |
||
69 | ) { |
||
70 | $category->getId()->willReturn(1); |
||
71 | |||
72 | $translation->getLocale()->willReturn('en'); |
||
73 | $translation->getTitle()->willReturn('English title'); |
||
74 | $translation->getBody()->willReturn('English content'); |
||
75 | |||
76 | $media->getId()->willReturn(1); |
||
77 | |||
78 | $articleMedia->getMedia()->willReturn($media); |
||
79 | $articleMedia->getDisplayOrder()->willReturn(10); |
||
80 | |||
81 | $article->getId()->willReturn(3); |
||
82 | $article->getCategories()->willReturn([$category]); |
||
83 | $article->getTranslations()->willReturn([$translation]); |
||
84 | $article->getMedias()->willReturn([$articleMedia]); |
||
85 | $article->getStatus()->willReturn(Article::STATUS_PUBLISHED); |
||
86 | $article->getMediaLimit()->willReturn(5); |
||
87 | $article->isShareOnFacebook()->willReturn(true); |
||
88 | |||
89 | $transMessage = new ArticleTranslationMessage(); |
||
90 | $transMessage->setLocale('en'); |
||
91 | $transMessage->setTitle('English title'); |
||
92 | $transMessage->setBody('English content'); |
||
93 | |||
94 | $mediaMessage = new ArticleMediaMessage(); |
||
95 | $mediaMessage->setMediaId(1); |
||
96 | $mediaMessage->setDisplayOrder(10); |
||
97 | |||
98 | $message = new ArticlePatchMessage(); |
||
99 | $message->setId(3); |
||
100 | $message->setStatus(Article::STATUS_PUBLISHED); |
||
101 | $message->setMediaLimit(5); |
||
102 | $message->setShareOnFacebook(true); |
||
103 | $message->setCategoryIds([1]); |
||
104 | $message->addTranslation($transMessage); |
||
105 | $message->addMedia($mediaMessage); |
||
106 | |||
107 | $this::createFromArticle($article)->shouldBeLike($message); |
||
108 | } |
||
109 | } |
||
110 |