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 FieldPatchMessageSpec extends ObjectBehavior |
||
13 | { |
||
14 | function it_is_initializable() |
||
15 | { |
||
16 | $this->shouldHaveType(FieldPatchMessage::class); |
||
17 | } |
||
18 | |||
19 | function it_should_build() |
||
20 | { |
||
21 | $translation = new FieldTranslationMessage(); |
||
22 | $translation->setLocale('en'); |
||
23 | $translation->setValue('Secret'); |
||
24 | |||
25 | $this->setToken('some-field'); |
||
26 | $this->setDescription('Some description'); |
||
27 | $this->addTranslation($translation); |
||
28 | |||
29 | $transData = [ |
||
30 | 'value' => 'Secret', |
||
31 | ]; |
||
32 | |||
33 | $data = [ |
||
34 | 'token' => 'some-field', |
||
35 | 'description' => 'Some description', |
||
36 | 'translations' => ['en' => $transData], |
||
37 | ]; |
||
38 | |||
39 | $this->build()->shouldReturn($data); |
||
40 | } |
||
41 | |||
42 | function it_should_create_from_field(Field $field, FieldTranslation $translation) |
||
43 | { |
||
44 | $translation->getLocale()->willReturn('en'); |
||
45 | $translation->getValue()->willReturn('Some field'); |
||
46 | |||
47 | $field->getId()->willReturn(1); |
||
48 | $field->getToken()->willReturn('some-field'); |
||
49 | $field->getDescription()->willReturn('Top-secret'); |
||
50 | $field->getTranslations()->willReturn([$translation]); |
||
51 | |||
52 | $transMessage = new FieldTranslationMessage(); |
||
53 | $transMessage->setLocale('en'); |
||
54 | $transMessage->setValue('Some field'); |
||
55 | |||
56 | $message = new FieldPatchMessage(); |
||
57 | $message->setId(1); |
||
58 | $message->setToken('some-field'); |
||
59 | $message->setDescription('Top-secret'); |
||
60 | $message->addTranslation($transMessage); |
||
61 | |||
62 | $this::createFromField($field)->shouldBeLike($message); |
||
63 | } |
||
64 | } |
||
65 |