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 |
||
16 | class ServiceClient implements ServiceClientInterface |
||
17 | { |
||
18 | /** @var HttpClient HTTP client used to send requests */ |
||
19 | private $httpClient; |
||
20 | |||
21 | /** @var HandlerStack */ |
||
22 | private $handlerStack; |
||
23 | |||
24 | /** @var callable */ |
||
25 | private $commandToRequestTransformer; |
||
26 | |||
27 | /** @var callable */ |
||
28 | private $responseToResultTransformer; |
||
29 | |||
30 | /** |
||
31 | * Instantiates a Guzzle ServiceClient for making requests to a web service. |
||
32 | * |
||
33 | * @param HttpClient $httpClient A fully-configured Guzzle HTTP client that |
||
34 | * will be used to perform the underlying HTTP requests. |
||
35 | * @param callable $commandToRequestTransformer A callable that transforms |
||
36 | * a Command into a Request. The function should accept a |
||
37 | * `GuzzleHttp\Command\CommandInterface` object and return a |
||
38 | * `Psr\Http\Message\RequestInterface` object. |
||
39 | * @param callable $responseToResultTransformer A callable that transforms a |
||
40 | * Response into a Result. The function should accept a |
||
41 | * `Psr\Http\Message\ResponseInterface` object (and optionally a |
||
42 | * `Psr\Http\Message\RequestInterface` object) and return a |
||
43 | * `GuzzleHttp\Command\ResultInterface` object. |
||
44 | * @param HandlerStack $commandHandlerStack A Guzzle HandlerStack, which can |
||
45 | * be used to add command-level middleware to the service client. |
||
46 | */ |
||
47 | public function __construct( |
||
59 | |||
60 | public function getHttpClient() |
||
64 | |||
65 | public function getHandlerStack() |
||
69 | |||
70 | public function getCommand($name, array $params = []) |
||
74 | |||
75 | public function execute(CommandInterface $command) |
||
79 | |||
80 | public function executeAsync(CommandInterface $command) |
||
87 | |||
88 | public function executeAll($commands, array $options = []) |
||
113 | |||
114 | public function executeAllAsync($commands, array $options = []) |
||
136 | |||
137 | /** |
||
138 | * Creates and executes a command for an operation by name. |
||
139 | * |
||
140 | * @param string $name Name of the command to execute. |
||
141 | * @param array $args Arguments to pass to the getCommand method. |
||
142 | * |
||
143 | * @return ResultInterface|PromiseInterface |
||
144 | * @see \GuzzleHttp\Command\ServiceClientInterface::getCommand |
||
145 | */ |
||
146 | public function __call($name, array $args) |
||
156 | |||
157 | /** |
||
158 | * Defines the main handler for commands that uses the HTTP client. |
||
159 | * |
||
160 | * @return callable |
||
161 | */ |
||
162 | private function createCommandHandler() |
||
184 | |||
185 | /** |
||
186 | * Transforms a Command object into a Request object. |
||
187 | * |
||
188 | * @param CommandInterface $command |
||
189 | * @return RequestInterface |
||
190 | */ |
||
191 | private function transformCommandToRequest(CommandInterface $command) |
||
197 | |||
198 | |||
199 | /** |
||
200 | * Transforms a Response object, also using data from the Request object, |
||
201 | * into a Result object. |
||
202 | * |
||
203 | * @param ResponseInterface $response |
||
204 | * @param RequestInterface $request |
||
205 | * @param CommandInterface $command |
||
206 | * @return ResultInterface |
||
207 | */ |
||
208 | private function transformResponseToResult( |
||
217 | } |
||
218 |
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.