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 declare(strict_types=1); |
||
14 | trait OperatorTrait |
||
15 | { |
||
16 | /** @var ClientInterface */ |
||
17 | protected $client; |
||
18 | |||
19 | /** @var ApiInterface */ |
||
20 | protected $api; |
||
21 | |||
22 | /** |
||
23 | * {@inheritDoc} |
||
24 | */ |
||
25 | public function __construct(ClientInterface $client, ApiInterface $api) |
||
30 | |||
31 | /** |
||
32 | * Magic method for dictating how objects are rendered when var_dump is called. |
||
33 | * For the benefit of users, extremely verbose and heavy properties (such as HTTP clients) are |
||
34 | * removed to provide easier access to normal state, such as resource attributes. |
||
35 | * |
||
36 | * @codeCoverageIgnore |
||
37 | * @return array |
||
38 | */ |
||
39 | public function __debugInfo() |
||
53 | |||
54 | /** |
||
55 | * Magic method which intercepts async calls, finds the sequential version, and wraps it in a |
||
56 | * {@see Promise} object. In order for this to happen, the called methods need to be in the |
||
57 | * following format: `createAsync`, where `create` is the sequential method being wrapped. |
||
58 | * |
||
59 | * @param $methodName The name of the method being invoked. |
||
60 | * @param $args The arguments to be passed to the sequential method. |
||
61 | * |
||
62 | * @throws \RuntimeException If method does not exist |
||
63 | * |
||
64 | * @return Promise |
||
65 | */ |
||
66 | public function __call($methodName, $args) |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function getOperation(array $definition): Operation |
||
98 | |||
99 | /** |
||
100 | * @param Operation $operation |
||
101 | * @param array $userValues |
||
102 | * @param bool $async |
||
103 | * |
||
104 | * @return mixed |
||
105 | * @throws \Exception |
||
106 | */ |
||
107 | protected function sendRequest(Operation $operation, array $userValues = [], bool $async = false) |
||
117 | |||
118 | /** |
||
119 | * {@inheritDoc} |
||
120 | */ |
||
121 | public function execute(array $definition, array $userValues = []): ResponseInterface |
||
125 | |||
126 | /** |
||
127 | * {@inheritDoc} |
||
128 | */ |
||
129 | public function executeAsync(array $definition, array $userValues = []): PromiseInterface |
||
133 | |||
134 | /** |
||
135 | * {@inheritDoc} |
||
136 | */ |
||
137 | View Code Duplication | public function model(string $class, $data = null): ResourceInterface |
|
152 | } |
||
153 |
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.