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 |
||
15 | class SiteServiceSpec extends ObjectBehavior |
||
16 | { |
||
17 | function it_is_initializable() |
||
18 | { |
||
19 | $this->shouldHaveType(SiteService::class); |
||
20 | } |
||
21 | |||
22 | function let(Client $client, ModelFactory $factory) |
||
23 | { |
||
24 | $this->beConstructedWith($client, $factory); |
||
25 | } |
||
26 | |||
27 | function it_should_get_sites(Client $client, ModelFactory $factory) |
||
28 | { |
||
29 | $method = 'GET'; |
||
30 | $path = 'sites'; |
||
31 | |||
32 | $client->sendRequest($method, $path)->willReturn([]); |
||
33 | $client->sendRequest($method, $path)->shouldBeCalled(); |
||
34 | |||
35 | $factory->createMany(Site::class, [])->willReturn([]); |
||
36 | |||
37 | $this->getSites(); |
||
38 | } |
||
39 | |||
40 | function it_should_get_site(Client $client, ModelFactory $factory, Site $site) |
||
41 | { |
||
42 | $method = 'GET'; |
||
43 | $path = 'sites/1'; |
||
44 | |||
45 | $client->sendRequest($method, $path)->willReturn([]); |
||
46 | $client->sendRequest($method, $path)->shouldBeCalled(); |
||
47 | |||
48 | $factory->create(Site::class, [])->willReturn($site); |
||
49 | |||
50 | $this->getSite(1); |
||
51 | } |
||
52 | |||
53 | function it_should_post_site( |
||
54 | Client $client, |
||
55 | ModelFactory $factory, |
||
56 | SitePostMessage $message, |
||
57 | Site $site |
||
58 | ) { |
||
59 | $message->getCompanyId()->willReturn(1); |
||
60 | $message->build()->willReturn([]); |
||
61 | |||
62 | $method = 'POST'; |
||
63 | $path = 'sites'; |
||
64 | $data = ['api_site' => []]; |
||
65 | |||
66 | $client->sendRequest($method, $path, $data)->willReturn([]); |
||
67 | $client->sendRequest($method, $path, $data)->shouldBeCalled(); |
||
68 | |||
69 | $factory->create(Site::class, [])->willReturn($site); |
||
70 | |||
71 | $this->postSite($message); |
||
72 | } |
||
73 | |||
74 | function it_should_patch_site( |
||
75 | Client $client, |
||
76 | ModelFactory $factory, |
||
77 | SitePatchMessage $message, |
||
78 | Site $site |
||
79 | ) { |
||
80 | $message->getId()->willReturn(1); |
||
81 | $message->build()->willReturn([]); |
||
82 | |||
83 | $method = 'PATCH'; |
||
84 | $path = 'sites/1'; |
||
85 | $data = ['api_site' => []]; |
||
86 | |||
87 | $client->sendRequest($method, $path, $data)->willReturn([]); |
||
88 | $client->sendRequest($method, $path, $data)->shouldBeCalled(); |
||
89 | |||
90 | $factory->create(Site::class, [])->willReturn($site); |
||
91 | |||
92 | $this->patchSite($message); |
||
93 | } |
||
94 | |||
95 | function it_should_delete_site(Client $client) |
||
96 | { |
||
97 | $method = 'DELETE'; |
||
98 | $path = 'sites/1'; |
||
99 | |||
100 | $client->sendRequest($method, $path)->shouldBeCalled(); |
||
101 | |||
102 | $this->deleteSite(1); |
||
103 | } |
||
104 | |||
105 | function it_should_get_platform_children( |
||
106 | Client $client, |
||
107 | ModelFactory $factory, |
||
108 | PlatformChildrenListMessage $message |
||
109 | ) { |
||
110 | $message->getSiteId()->willReturn(1); |
||
111 | $message->build()->willReturn([]); |
||
112 | |||
113 | $method = 'GET'; |
||
114 | $path = 'platform/1/children'; |
||
115 | |||
116 | $client->sendRequest($method, $path)->willReturn([]); |
||
117 | $client->sendRequest($method, $path)->shouldBeCalled(); |
||
118 | |||
119 | $factory->createMany(Site::class, [])->willReturn([]); |
||
120 | |||
121 | $this->getPlatformChildren($message); |
||
122 | } |
||
123 | } |
||
124 |