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 |
||
10 | class HttpImposter extends Imposter |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $protocol = self::PROTOCOL_HTTP; |
||
16 | |||
17 | /** |
||
18 | * @var Stub\Stub[] |
||
19 | */ |
||
20 | private $stubs = []; |
||
21 | |||
22 | /** |
||
23 | * @return array |
||
24 | */ |
||
25 | 8 | public function jsonSerialize() |
|
26 | { |
||
27 | 8 | return \Meare\Juggler\array_filter_null([ |
|
28 | 8 | 'port' => $this->getPort(), |
|
29 | 8 | 'protocol' => $this->getProtocol(), |
|
30 | 8 | 'name' => $this->getName(), |
|
31 | 8 | 'requests' => $this->hasRequests() ? $this->serializeRequests() : null, |
|
32 | 8 | 'stubs' => $this->jsonSerializeStubs(), |
|
33 | 8 | ]); |
|
34 | } |
||
35 | |||
36 | /** |
||
37 | * @return array |
||
38 | */ |
||
39 | 3 | private function serializeRequests() |
|
40 | { |
||
41 | 3 | $requests = $this->requests; |
|
42 | 3 | foreach ($requests as $key => $request) { |
|
43 | 3 | View Code Duplication | if (isset($request['query'])) { |
|
|||
44 | 1 | $requests[$key]['query'] = \Meare\Juggler\json_object($request['query']); |
|
45 | 1 | } |
|
46 | 3 | View Code Duplication | if (isset($request['headers'])) { |
47 | 1 | $requests[$key]['headers'] = \Meare\Juggler\json_object($request['headers']); |
|
48 | 1 | } |
|
49 | 3 | } |
|
50 | |||
51 | 3 | return $requests; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return array |
||
56 | */ |
||
57 | 8 | private function jsonSerializeStubs() |
|
58 | { |
||
59 | 8 | $stubs = []; |
|
60 | 8 | foreach ($this->stubs as $stub) { |
|
61 | 1 | $stubs[] = $stub->jsonSerialize(); |
|
62 | 8 | } |
|
63 | |||
64 | 8 | return $stubs; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param IResponse[]|IResponse $responses |
||
69 | * @param IPredicate[]|IPredicate $predicates |
||
70 | * @return Stub\Stub |
||
71 | */ |
||
72 | 1 | public function createStub($responses = null, $predicates = null) |
|
79 | |||
80 | /** |
||
81 | * @param Stub\Stub $stub |
||
82 | */ |
||
83 | 7 | public function addStub(Stub\Stub $stub) |
|
87 | |||
88 | /** |
||
89 | * @return Stub\Stub[] |
||
90 | */ |
||
91 | 3 | public function getStubs() |
|
95 | |||
96 | /** |
||
97 | * @param Stub\Stub[] $stubs |
||
98 | */ |
||
99 | 2 | public function setStubs(array $stubs) |
|
104 | |||
105 | 2 | public function clearStubs() |
|
109 | |||
110 | /** |
||
111 | * @param Stub\Stub[] $stubs |
||
112 | */ |
||
113 | 2 | public function addStubs(array $stubs) |
|
119 | |||
120 | /** |
||
121 | * @param Stub\Stub $stub |
||
122 | * @throws NotFoundException |
||
123 | */ |
||
124 | 2 | public function removeStub(Stub\Stub $stub) |
|
135 | |||
136 | /** |
||
137 | * @param array $criteria |
||
138 | * @return Stub\Stub |
||
139 | * @throws NotFoundException |
||
140 | */ |
||
141 | 2 | public function findStubByPredicates($criteria) |
|
150 | } |
||
151 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.