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 |
||
24 | class Client implements HttpClient, HttpAsyncClient |
||
25 | { |
||
26 | use HttpAsyncClientEmulator; |
||
27 | use VersionBridgeClient; |
||
28 | |||
29 | /** |
||
30 | * @var ResponseFactory |
||
31 | */ |
||
32 | private $responseFactory; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $conditionalResults = []; |
||
38 | |||
39 | /** |
||
40 | * @var RequestInterface[] |
||
41 | */ |
||
42 | private $requests = []; |
||
43 | |||
44 | /** |
||
45 | * @var ResponseInterface[] |
||
46 | */ |
||
47 | private $responses = []; |
||
48 | |||
49 | /** |
||
50 | * @var ResponseInterface|null |
||
51 | */ |
||
52 | private $defaultResponse; |
||
53 | |||
54 | /** |
||
55 | * @var Exception[] |
||
56 | */ |
||
57 | private $exceptions = []; |
||
58 | |||
59 | /** |
||
60 | * @var Exception|null |
||
61 | */ |
||
62 | private $defaultException; |
||
63 | |||
64 | 15 | public function __construct(ResponseFactory $responseFactory = null) |
|
65 | { |
||
66 | 15 | $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
|
67 | 15 | } |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 11 | public function doSendRequest(RequestInterface $request) |
|
73 | { |
||
74 | 11 | $this->requests[] = $request; |
|
75 | |||
76 | 11 | foreach ($this->conditionalResults as $result) { |
|
77 | 4 | $matcher = $result['matcher']; |
|
78 | 4 | $callable = $result['callable']; |
|
79 | |||
80 | /** |
||
81 | * @var $matcher RequestMatcher |
||
82 | * @var ResponseInterface|Exception $result |
||
83 | */ |
||
84 | 4 | if ($matcher->matches($request)) { |
|
85 | 4 | return $callable($request); |
|
86 | } |
||
87 | } |
||
88 | |||
89 | 8 | if (count($this->exceptions) > 0) { |
|
90 | 1 | throw array_shift($this->exceptions); |
|
91 | } |
||
92 | |||
93 | 7 | if (count($this->responses) > 0) { |
|
94 | 3 | return array_shift($this->responses); |
|
95 | } |
||
96 | |||
97 | 4 | if ($this->defaultException) { |
|
98 | 1 | throw $this->defaultException; |
|
99 | } |
||
100 | |||
101 | 3 | if ($this->defaultResponse) { |
|
102 | 1 | return $this->defaultResponse; |
|
103 | } |
||
104 | |||
105 | // Return success response by default |
||
106 | 2 | return $this->responseFactory->createResponse(); |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param RequestMatcher $requestMatcher |
||
111 | * @param ResponseInterface|\Exception|callable $result |
||
112 | */ |
||
113 | 4 | public function on(RequestMatcher $requestMatcher, $result) |
|
114 | { |
||
115 | 4 | $callable = null; |
|
|
|||
116 | |||
117 | switch (true) { |
||
118 | 4 | case is_callable($result): |
|
119 | 1 | $callable = $result; |
|
120 | |||
121 | 1 | break; |
|
122 | 3 | case $result instanceof ResponseInterface: |
|
123 | $callable = function () use ($result) { |
||
124 | 1 | return $result; |
|
125 | 2 | }; |
|
126 | |||
127 | 2 | break; |
|
128 | 1 | case $result instanceof \Exception: |
|
129 | 1 | $callable = function () use ($result) { |
|
130 | 1 | throw $result; |
|
131 | 1 | }; |
|
132 | |||
133 | 1 | break; |
|
134 | default: |
||
135 | throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable'); |
||
136 | } |
||
137 | 4 | $this->conditionalResults[] = [ |
|
138 | 4 | 'matcher' => $requestMatcher, |
|
139 | 4 | 'callable' => $callable, |
|
140 | ]; |
||
141 | 4 | } |
|
142 | |||
143 | /** |
||
144 | * Adds an exception that will be thrown. |
||
145 | */ |
||
146 | 2 | View Code Duplication | public function addException(\Exception $exception) |
147 | { |
||
148 | 2 | if (!$exception instanceof Exception) { |
|
149 | @trigger_error('Clients may only throw exceptions of type '.Exception::class.'. Setting an exception of class '.get_class($exception).' will not be possible anymore in the future', E_USER_DEPRECATED); |
||
150 | } |
||
151 | $this->exceptions[] = $exception; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
156 | * |
||
157 | * If both a default exception and a default response are set, the exception will be thrown. |
||
158 | */ |
||
159 | View Code Duplication | public function setDefaultException(\Exception $defaultException = null) |
|
166 | |||
167 | /** |
||
168 | * Adds a response that will be returned in first in first out order. |
||
169 | */ |
||
170 | public function addResponse(ResponseInterface $response) |
||
174 | |||
175 | /** |
||
176 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
177 | */ |
||
178 | public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
||
182 | |||
183 | /** |
||
184 | * Returns requests that were sent. |
||
185 | * |
||
186 | * @return RequestInterface[] |
||
187 | */ |
||
188 | public function getRequests() |
||
192 | |||
193 | public function getLastRequest() |
||
197 | |||
198 | public function reset() |
||
206 | } |
||
207 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.