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 |
||
26 | class Client implements HttpClient, HttpAsyncClient |
||
27 | { |
||
28 | use HttpAsyncClientEmulator; |
||
29 | use VersionBridgeClient; |
||
30 | |||
31 | /** |
||
32 | * @var ResponseFactory|ResponseFactoryInterface |
||
33 | */ |
||
34 | private $responseFactory; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $conditionalResults = []; |
||
40 | |||
41 | /** |
||
42 | * @var RequestInterface[] |
||
43 | */ |
||
44 | private $requests = []; |
||
45 | |||
46 | /** |
||
47 | * @var ResponseInterface[] |
||
48 | */ |
||
49 | private $responses = []; |
||
50 | |||
51 | /** |
||
52 | * @var ResponseInterface|null |
||
53 | */ |
||
54 | private $defaultResponse; |
||
55 | |||
56 | /** |
||
57 | * @var Exception[] |
||
58 | */ |
||
59 | private $exceptions = []; |
||
60 | |||
61 | /** |
||
62 | * @var Exception|null |
||
63 | */ |
||
64 | private $defaultException; |
||
65 | |||
66 | /** |
||
67 | * @param ResponseFactory|ResponseFactoryInterface|null |
||
68 | */ |
||
69 | 15 | public function __construct($responseFactory = null) |
|
70 | { |
||
71 | 15 | if (!$responseFactory instanceof ResponseFactory && !$responseFactory instanceof ResponseFactoryInterface && null !== $responseFactory) { |
|
72 | throw new \TypeError( |
||
73 | sprintf('%s::__construct(): Argument #1 ($responseFactory) must be of type %s|%s|null, %s given', self::class, ResponseFactory::class, ResponseFactoryInterface::class, get_debug_type($responseFactory)) |
||
|
|||
74 | ); |
||
75 | } |
||
76 | |||
77 | $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function doSendRequest(RequestInterface $request) |
||
84 | { |
||
85 | $this->requests[] = $request; |
||
86 | |||
87 | foreach ($this->conditionalResults as $result) { |
||
88 | /** |
||
89 | * @var RequestMatcher |
||
90 | */ |
||
91 | $matcher = $result['matcher']; |
||
92 | |||
93 | /** |
||
94 | * @var callable |
||
95 | */ |
||
96 | $callable = $result['callable']; |
||
97 | |||
98 | if ($matcher->matches($request)) { |
||
99 | return $callable($request); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | if (count($this->exceptions) > 0) { |
||
104 | throw array_shift($this->exceptions); |
||
105 | } |
||
106 | |||
107 | if (count($this->responses) > 0) { |
||
108 | return array_shift($this->responses); |
||
109 | } |
||
110 | |||
111 | if ($this->defaultException) { |
||
112 | throw $this->defaultException; |
||
113 | } |
||
114 | |||
115 | if ($this->defaultResponse) { |
||
116 | return $this->defaultResponse; |
||
117 | } |
||
118 | |||
119 | // Return success response by default |
||
120 | return $this->responseFactory->createResponse(); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Adds an exception to be thrown or response to be returned if the request |
||
125 | * matcher matches. |
||
126 | * |
||
127 | * For more complex logic, pass a callable as $result. The method is given |
||
128 | * the request and MUST either return a ResponseInterface or throw an |
||
129 | * exception that implements the PSR-18 / HTTPlug exception interface. |
||
130 | * |
||
131 | * @param ResponseInterface|Exception|ClientExceptionInterface|callable $result |
||
132 | */ |
||
133 | public function on(RequestMatcher $requestMatcher, $result) |
||
134 | { |
||
135 | if (!$result instanceof ResponseInterface && !$result instanceof Exception && !$result instanceof ClientExceptionInterface && !is_callable($result)) { |
||
136 | throw new \TypeError( |
||
137 | sprintf('%s::on(): Argument #2 ($result) must be of type %s|%s|%s|callable, %s given', self::class, ResponseInterface::class, Exception::class, ClientExceptionInterface::class, get_debug_type($result)) |
||
138 | ); |
||
139 | } |
||
140 | |||
141 | $callable = self::makeCallable($result); |
||
142 | |||
143 | $this->conditionalResults[] = [ |
||
144 | 'matcher' => $requestMatcher, |
||
145 | 'callable' => $callable, |
||
146 | ]; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param ResponseInterface|Exception|ClientExceptionInterface|callable $result |
||
151 | * |
||
152 | * @return callable |
||
153 | */ |
||
154 | private static function makeCallable($result) |
||
155 | { |
||
156 | if (is_callable($result)) { |
||
157 | return $result; |
||
158 | } |
||
159 | |||
160 | if ($result instanceof ResponseInterface) { |
||
161 | return function () use ($result) { |
||
162 | return $result; |
||
163 | }; |
||
164 | } |
||
165 | |||
166 | return function () use ($result) { |
||
167 | throw $result; |
||
168 | }; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Adds an exception that will be thrown. |
||
173 | */ |
||
174 | public function addException(\Exception $exception) |
||
181 | |||
182 | /** |
||
183 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
184 | * |
||
185 | * If both a default exception and a default response are set, the exception will be thrown. |
||
186 | */ |
||
187 | public function setDefaultException(\Exception $defaultException = null) |
||
194 | |||
195 | /** |
||
196 | * Adds a response that will be returned in first in first out order. |
||
197 | */ |
||
198 | public function addResponse(ResponseInterface $response) |
||
202 | |||
203 | /** |
||
204 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
205 | */ |
||
206 | public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
||
210 | |||
211 | /** |
||
212 | * Returns requests that were sent. |
||
213 | * |
||
214 | * @return RequestInterface[] |
||
215 | */ |
||
216 | public function getRequests() |
||
220 | |||
221 | public function getLastRequest() |
||
225 | |||
226 | public function reset() |
||
235 | } |
||
236 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.