Passed
Pull Request — master (#12)
by romain
09:45
created

FieldPatchMessageSpec   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 41.51 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 22
loc 53
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace spec\Yproximite\Api\Message\Field;
4
5
use PhpSpec\ObjectBehavior;
6
7
use Yproximite\Api\Model\Field\Field;
8
use Yproximite\Api\Model\Field\FieldTranslation;
9
use Yproximite\Api\Message\Field\FieldPatchMessage;
10
use Yproximite\Api\Message\Field\FieldTranslationMessage;
11
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