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 CompanyServiceSpec extends ObjectBehavior |
||
| 15 | { |
||
| 16 | function it_is_initializable() |
||
| 17 | { |
||
| 18 | $this->shouldHaveType(CompanyService::class); |
||
| 19 | } |
||
| 20 | |||
| 21 | function let(Client $client, ModelFactory $factory) |
||
| 22 | { |
||
| 23 | $this->beConstructedWith($client, $factory); |
||
| 24 | } |
||
| 25 | |||
| 26 | function it_should_post_company( |
||
| 27 | Client $client, |
||
| 28 | ModelFactory $factory, |
||
| 29 | CompanyPostMessage $message, |
||
| 30 | Company $company |
||
| 31 | ) { |
||
| 32 | $message->build()->willReturn([]); |
||
| 33 | |||
| 34 | $method = 'POST'; |
||
| 35 | $path = 'companies'; |
||
| 36 | $data = ['api_company' => []]; |
||
| 37 | |||
| 38 | $client->sendRequest($method, $path, $data)->willReturn([]); |
||
| 39 | $client->sendRequest($method, $path, $data)->shouldBeCalled(); |
||
| 40 | |||
| 41 | $factory->create(Company::class, [])->willReturn($company); |
||
| 42 | |||
| 43 | $this->postCompany($message); |
||
| 44 | } |
||
| 45 | |||
| 46 | function it_should_patch_company( |
||
| 47 | Client $client, |
||
| 48 | ModelFactory $factory, |
||
| 49 | CompanyPatchMessage $message, |
||
| 50 | Company $company |
||
| 51 | ) { |
||
| 52 | $message->getId()->willReturn(2); |
||
| 53 | $message->build()->willReturn([]); |
||
| 54 | |||
| 55 | $method = 'PATCH'; |
||
| 56 | $path = 'companies/2'; |
||
| 57 | $data = ['api_company' => []]; |
||
| 58 | |||
| 59 | $client->sendRequest($method, $path, $data)->willReturn([]); |
||
| 60 | $client->sendRequest($method, $path, $data)->shouldBeCalled(); |
||
| 61 | |||
| 62 | $factory->create(Company::class, [])->willReturn($company); |
||
| 63 | |||
| 64 | $this->patchCompany($message); |
||
| 65 | } |
||
| 66 | } |
||
| 67 |