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 TeamWorkerServiceSpec extends ObjectBehavior |
||
15 | { |
||
16 | function it_is_initializable() |
||
17 | { |
||
18 | $this->shouldHaveType(TeamWorkerService::class); |
||
19 | } |
||
20 | |||
21 | function let(Client $client, ModelFactory $factory) |
||
22 | { |
||
23 | $this->beConstructedWith($client, $factory); |
||
24 | } |
||
25 | |||
26 | function it_should_post_team_worker( |
||
27 | Client $client, |
||
28 | ModelFactory $factory, |
||
29 | TeamWorkerPostMessage $message, |
||
30 | TeamWorker $teamWorker |
||
31 | ) { |
||
32 | $message->getSiteId()->willReturn(1); |
||
33 | $message->build()->willReturn([]); |
||
34 | |||
35 | $method = 'POST'; |
||
36 | $path = 'sites/1/teams/workers'; |
||
37 | $data = ['api_team_worker' => []]; |
||
38 | |||
39 | $client->sendRequest($method, $path, $data)->willReturn([]); |
||
40 | $client->sendRequest($method, $path, $data)->shouldBeCalled(); |
||
41 | |||
42 | $factory->create(TeamWorker::class, [])->willReturn($teamWorker); |
||
43 | |||
44 | $this->postTeamWorker($message); |
||
45 | } |
||
46 | |||
47 | function it_should_patch_team_worker( |
||
48 | Client $client, |
||
49 | ModelFactory $factory, |
||
50 | TeamWorkerPatchMessage $message, |
||
51 | TeamWorker $teamWorker |
||
52 | ) { |
||
53 | $message->getId()->willReturn(2); |
||
54 | $message->getSiteId()->willReturn(1); |
||
55 | $message->build()->willReturn([]); |
||
56 | |||
57 | $method = 'PATCH'; |
||
58 | $path = 'sites/1/teams/2/worker'; |
||
59 | $data = ['api_team_worker' => []]; |
||
60 | |||
61 | $client->sendRequest($method, $path, $data)->willReturn([]); |
||
62 | $client->sendRequest($method, $path, $data)->shouldBeCalled(); |
||
63 | |||
64 | $factory->create(TeamWorker::class, [])->willReturn($teamWorker); |
||
65 | |||
66 | $this->patchTeamWorker($message); |
||
67 | } |
||
68 | } |
||
69 |