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