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 |
||
8 | class Salesforce |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $objName; |
||
14 | |||
15 | public $baseUrl; |
||
16 | |||
17 | public $instanceUrl; |
||
18 | |||
19 | public $client; |
||
20 | |||
21 | /** |
||
22 | * Salesforce constructor. |
||
23 | * |
||
24 | * @param ClientInterface $client |
||
25 | * @param string $url |
||
26 | * @param string $instanceUrl |
||
27 | */ |
||
28 | public function __construct(ClientInterface $client, string $url, string $instanceUrl) |
||
34 | |||
35 | /** |
||
36 | * @param string $method |
||
37 | * @param array $args |
||
38 | * @return bool|mixed|string |
||
39 | */ |
||
40 | public function __call($method, $args) |
||
66 | |||
67 | /** |
||
68 | * Create object dynamically |
||
69 | * |
||
70 | * @param $method |
||
71 | * @param $args |
||
72 | * @return bool |
||
73 | */ |
||
74 | View Code Duplication | private function callCreateOnObject($method, $args) |
|
85 | |||
86 | private function callUpdateOnObject($method, $args) |
||
87 | { |
||
88 | $type = substr($method, 6); |
||
89 | $class = '\\Surge\\LaravelSalesforce\\Objects\\' . $type; |
||
90 | |||
91 | if (class_exists($class)) { |
||
92 | return (new $class($this))->update($args[0], $args[1]); |
||
93 | } |
||
94 | |||
95 | return (new BaseObject($this, $type))->update($type, $args[0]); |
||
96 | } |
||
97 | |||
98 | View Code Duplication | private function callDeleteOnObject($method, $args) |
|
109 | |||
110 | private function callGetOnObject($method, $args) |
||
121 | |||
122 | private function callExistsOnObject($method, $args) |
||
139 | |||
140 | /** |
||
141 | * Run query |
||
142 | * |
||
143 | * @param $query |
||
144 | * @return mixed |
||
145 | */ |
||
146 | public function runQuery($query) |
||
156 | |||
157 | /** |
||
158 | * Run report. |
||
159 | * |
||
160 | * @param string $id |
||
161 | * @param bool $includeDetails |
||
162 | * @return mixed |
||
163 | * |
||
164 | */ |
||
165 | public function getReport(string $id, bool $includeDetails = true) |
||
169 | } |
||
170 |
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.