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 |
||
| 14 | class CallTrackingServiceSpec extends ObjectBehavior |
||
| 15 | { |
||
| 16 | function it_is_initializable() |
||
| 17 | { |
||
| 18 | $this->shouldHaveType(CallTrackingService::class); |
||
| 19 | } |
||
| 20 | |||
| 21 | function let(Client $client, ModelFactory $factory) |
||
| 22 | { |
||
| 23 | $this->beConstructedWith($client, $factory); |
||
| 24 | } |
||
| 25 | |||
| 26 | function it_should_post_call_tracking( |
||
| 27 | Client $client, |
||
| 28 | ModelFactory $factory, |
||
| 29 | CallTrackingPostMessage $message, |
||
| 30 | CallTracking $callTracking |
||
| 31 | ) { |
||
| 32 | $message->getSiteId()->willReturn(1); |
||
| 33 | $message->build()->willReturn([]); |
||
| 34 | |||
| 35 | $method = 'POST'; |
||
| 36 | $path = 'sites/1/call_trackings'; |
||
| 37 | $data = ['api_call_tracking' => []]; |
||
| 38 | |||
| 39 | $client->sendRequest($method, $path, $data)->willReturn([]); |
||
| 40 | $client->sendRequest($method, $path, $data)->shouldBeCalled(); |
||
| 41 | |||
| 42 | $factory->create(CallTracking::class, [])->willReturn($callTracking); |
||
| 43 | |||
| 44 | $this->postCallTracking($message); |
||
| 45 | } |
||
| 46 | |||
| 47 | function it_should_patch_call_tracking( |
||
| 48 | Client $client, |
||
| 49 | ModelFactory $factory, |
||
| 50 | CallTrackingPatchMessage $message, |
||
| 51 | CallTracking $callTracking |
||
| 52 | ) { |
||
| 53 | $message->getSiteId()->willReturn(1); |
||
| 54 | $message->build()->willReturn([]); |
||
| 55 | |||
| 56 | $method = 'PATCH'; |
||
| 57 | $path = 'sites/1/call_trackings/update'; |
||
| 58 | $data = ['api_call_tracking_edit' => []]; |
||
| 59 | |||
| 60 | $client->sendRequest($method, $path, $data)->willReturn([]); |
||
| 61 | $client->sendRequest($method, $path, $data)->shouldBeCalled(); |
||
| 62 | |||
| 63 | $factory->create(CallTracking::class, [])->willReturn($callTracking); |
||
| 64 | |||
| 65 | $this->patchCallTracking($message); |
||
| 66 | } |
||
| 67 | } |
||
| 68 |