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 FieldServiceSpec extends ObjectBehavior |
||
| 17 | { |
||
| 18 | function it_is_initializable() |
||
| 19 | { |
||
| 20 | $this->shouldHaveType(FieldService::class); |
||
| 21 | } |
||
| 22 | |||
| 23 | function let(Client $client, ModelFactory $factory) |
||
| 24 | { |
||
| 25 | $this->beConstructedWith($client, $factory); |
||
| 26 | } |
||
| 27 | |||
| 28 | function it_should_get_fields( |
||
| 29 | Client $client, |
||
| 30 | ModelFactory $factory, |
||
| 31 | FieldListMessage $message |
||
| 32 | ) { |
||
| 33 | $message->getSiteId()->willReturn(1); |
||
| 34 | $message->build()->willReturn([]); |
||
| 35 | |||
| 36 | $method = 'GET'; |
||
| 37 | $path = 'sites/1/fields'; |
||
| 38 | |||
| 39 | $client->sendRequest($method, $path)->willReturn([]); |
||
| 40 | $client->sendRequest($method, $path)->shouldBeCalled(); |
||
| 41 | |||
| 42 | $factory->createMany(Field::class, [])->willReturn([]); |
||
| 43 | |||
| 44 | $this->getFields($message); |
||
| 45 | } |
||
| 46 | |||
| 47 | function it_should_post_field( |
||
| 48 | Client $client, |
||
| 49 | ModelFactory $factory, |
||
| 50 | FieldPostMessage $message, |
||
| 51 | Field $field |
||
| 52 | ) { |
||
| 53 | $message->getSiteId()->willReturn(1); |
||
| 54 | $message->build()->willReturn([]); |
||
| 55 | |||
| 56 | $method = 'POST'; |
||
| 57 | $path = 'sites/1/fields'; |
||
| 58 | $data = ['api_field' => []]; |
||
| 59 | |||
| 60 | $client->sendRequest($method, $path, $data)->willReturn([]); |
||
| 61 | $client->sendRequest($method, $path, $data)->shouldBeCalled(); |
||
| 62 | |||
| 63 | $factory->create(Field::class, [])->willReturn($field); |
||
| 64 | |||
| 65 | $this->postField($message); |
||
| 66 | } |
||
| 67 | |||
| 68 | function it_should_patch_field( |
||
| 69 | Client $client, |
||
| 70 | ModelFactory $factory, |
||
| 71 | FieldPatchMessage $message, |
||
| 72 | Field $field |
||
| 73 | ) { |
||
| 74 | $message->getId()->willReturn(2); |
||
| 75 | $message->getSiteId()->willReturn(1); |
||
| 76 | $message->build()->willReturn([]); |
||
| 77 | |||
| 78 | $method = 'PATCH'; |
||
| 79 | $path = 'sites/1/fields/2'; |
||
| 80 | $data = ['api_field' => []]; |
||
| 81 | |||
| 82 | $client->sendRequest($method, $path, $data)->willReturn([]); |
||
| 83 | $client->sendRequest($method, $path, $data)->shouldBeCalled(); |
||
| 84 | |||
| 85 | $factory->create(Field::class, [])->willReturn($field); |
||
| 86 | |||
| 87 | $this->patchField($message); |
||
| 88 | } |
||
| 89 | |||
| 90 | function it_should_override_field( |
||
| 91 | Client $client, |
||
| 92 | ModelFactory $factory, |
||
| 93 | FieldOverrideMessage $message, |
||
| 94 | Field $field |
||
| 95 | ) { |
||
| 96 | $message->getId()->willReturn(5); |
||
| 97 | $message->getSiteId()->willReturn(1); |
||
| 98 | $message->build()->willReturn([]); |
||
| 99 | |||
| 100 | $method = 'GET'; |
||
| 101 | $path = 'sites/1/fields/5/override'; |
||
| 102 | |||
| 103 | $client->sendRequest($method, $path)->willReturn([]); |
||
| 104 | $client->sendRequest($method, $path)->shouldBeCalled(); |
||
| 105 | |||
| 106 | $factory->create(Field::class, [])->willReturn($field); |
||
| 107 | |||
| 108 | $this->overrideField($message); |
||
| 109 | } |
||
| 110 | } |
||
| 111 |