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:
Complex classes like DatabaseHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DatabaseHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class DatabaseHelper extends AbstractHelper |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $dbSettings = null; |
||
18 | |||
19 | /** |
||
20 | * @var bool |
||
21 | */ |
||
22 | protected $isSocketConnect = false; |
||
23 | |||
24 | /** |
||
25 | * @var PDO |
||
26 | */ |
||
27 | protected $_connection = null; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $_tables; |
||
33 | |||
34 | /** |
||
35 | * @param OutputInterface $output |
||
36 | * |
||
37 | * @throws RuntimeException |
||
38 | * @return void |
||
39 | */ |
||
40 | public function detectDbSettings(OutputInterface $output) |
||
41 | { |
||
42 | if ($this->dbSettings !== null) { |
||
43 | return; |
||
44 | } |
||
45 | |||
46 | $magentoHelper = $this->getHelperSet()->getCommand()->getHelper('magento'); |
||
47 | $config = $magentoHelper->getBaseConfig(); // @TODO Use \Magento\Framework\App\DeploymentConfig ? |
||
48 | |||
49 | if (!isset($config['db'])) { |
||
50 | $output->writeln('<error>DB settings was not found in config.xml file</error>'); |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | if (!isset($config['db']['connection']['default'])) { |
||
55 | throw new RuntimeException('Cannot find default connection config in app/etc/config.php'); |
||
56 | } |
||
57 | |||
58 | $this->dbSettings = (array) $config['db']['connection']['default']; |
||
59 | |||
60 | $this->dbSettings['prefix'] = ''; |
||
61 | if (isset($config['db']['table_prefix'])) { |
||
62 | $this->dbSettings['prefix'] = (string) $config['db']['table_prefix']; |
||
63 | } |
||
64 | |||
65 | View Code Duplication | if (strpos($this->dbSettings['host'], ':') !== false) { |
|
|
|||
66 | list($this->dbSettings['host'], $this->dbSettings['port']) = explode(':', $this->dbSettings['host']); |
||
67 | } |
||
68 | |||
69 | if (isset($this->dbSettings['comment'])) { |
||
70 | unset($this->dbSettings['comment']); |
||
71 | } |
||
72 | |||
73 | if (isset($this->dbSettings['unix_socket'])) { |
||
74 | $this->isSocketConnect = true; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Connects to the database without initializing magento |
||
80 | * |
||
81 | * @param OutputInterface $output = null |
||
1 ignored issue
–
show
|
|||
82 | * |
||
83 | * @return PDO |
||
84 | * @throws RuntimeException pdo mysql extension is not installed |
||
85 | */ |
||
86 | public function getConnection(OutputInterface $output = null) |
||
137 | |||
138 | /** |
||
139 | * Creates a PDO DSN for the adapter from $this->_config settings. |
||
140 | * |
||
141 | * @see Zend_Db_Adapter_Pdo_Abstract |
||
142 | * @return string |
||
143 | */ |
||
144 | public function dsn() |
||
145 | { |
||
146 | $this->detectDbSettings(new NullOutput()); |
||
147 | |||
148 | // baseline of DSN parts |
||
149 | $dsn = $this->dbSettings; |
||
150 | |||
151 | // don't pass the username, password, charset, database, persistent and driver_options in the DSN |
||
152 | unset($dsn['username']); |
||
153 | unset($dsn['password']); |
||
154 | unset($dsn['options']); |
||
155 | unset($dsn['charset']); |
||
156 | unset($dsn['persistent']); |
||
157 | unset($dsn['driver_options']); |
||
158 | unset($dsn['dbname']); |
||
159 | |||
160 | // use all remaining parts in the DSN |
||
161 | $buildDsn = array(); |
||
162 | foreach ($dsn as $key => $val) { |
||
163 | if (is_array($val)) { |
||
164 | continue; |
||
165 | } |
||
166 | $buildDsn[$key] = "$key=$val"; |
||
167 | } |
||
168 | |||
169 | return 'mysql:' . implode(';', $buildDsn); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Check whether current mysql user has $privilege privilege |
||
174 | * |
||
175 | * @param string $privilege |
||
176 | * |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function mysqlUserHasPrivilege($privilege) |
||
180 | { |
||
181 | $statement = $this->getConnection()->query('SHOW GRANTS'); |
||
182 | |||
183 | $result = $statement->fetchAll(PDO::FETCH_COLUMN); |
||
184 | foreach ($result as $row) { |
||
185 | if (preg_match('/^GRANT(.*)' . strtoupper($privilege) . '/', $row) |
||
186 | || preg_match('/^GRANT(.*)ALL/', $row) |
||
187 | ) { |
||
188 | return true; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | return false; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getMysqlClientToolConnectionString() |
||
219 | |||
220 | /** |
||
221 | * Get mysql variable value |
||
222 | * |
||
223 | * @param string $variable |
||
224 | * |
||
225 | * @return bool|string |
||
226 | */ |
||
227 | public function getMysqlVariableValue($variable) |
||
237 | |||
238 | /** |
||
239 | * @param array $commandConfig |
||
240 | * |
||
241 | * @throws \Exception |
||
242 | * @internal param $config |
||
243 | * @return array $commandConfig |
||
244 | * @return array |
||
245 | */ |
||
246 | public function getTableDefinitions(array $commandConfig) |
||
271 | |||
272 | /** |
||
273 | * @param array $list |
||
274 | * @param array $definitions |
||
275 | * @param array $resolved Which definitions where already resolved -> prevent endless loops |
||
276 | * |
||
277 | * @return array |
||
278 | * @throws RuntimeException |
||
279 | */ |
||
280 | public function resolveTables(array $list, array $definitions = array(), array $resolved = array()) |
||
332 | |||
333 | /** |
||
334 | * Get list of db tables |
||
335 | * |
||
336 | * @param bool $withoutPrefix |
||
337 | * |
||
338 | * @return array |
||
339 | */ |
||
340 | public function getTables($withoutPrefix = false) |
||
366 | |||
367 | /** |
||
368 | * Get list of db tables status |
||
369 | * |
||
370 | * @param bool $withoutPrefix |
||
371 | * |
||
372 | * @return array |
||
373 | */ |
||
374 | public function getTablesStatus($withoutPrefix = false) |
||
401 | |||
402 | /** |
||
403 | * @return array |
||
404 | */ |
||
405 | public function getDbSettings() |
||
406 | { |
||
407 | return $this->dbSettings; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @return boolean |
||
412 | */ |
||
413 | public function getIsSocketConnect() |
||
414 | { |
||
415 | return $this->isSocketConnect; |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Returns the canonical name of this helper. |
||
420 | * |
||
421 | * @return string The canonical name |
||
422 | * |
||
423 | * @api |
||
424 | */ |
||
425 | public function getName() |
||
426 | { |
||
427 | return 'database'; |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * @param OutputInterface $output |
||
432 | */ |
||
433 | View Code Duplication | public function dropDatabase($output) |
|
440 | |||
441 | /** |
||
442 | * @param OutputInterface $output |
||
443 | */ |
||
444 | public function dropTables($output) |
||
457 | |||
458 | /** |
||
459 | * @param OutputInterface $output |
||
460 | */ |
||
461 | View Code Duplication | public function createDatabase($output) |
|
468 | |||
469 | /** |
||
470 | * @param string $command example: 'VARIABLES', 'STATUS' |
||
471 | * @param string $variable [optional] |
||
472 | * |
||
473 | * @return array |
||
474 | */ |
||
475 | private function runShowCommand($command, $variable = null) |
||
502 | |||
503 | /** |
||
504 | * @param string|null $variable [optional] |
||
505 | * |
||
506 | * @return array |
||
507 | */ |
||
508 | public function getGlobalVariables($variable = null) |
||
512 | |||
513 | /** |
||
514 | * @param string $variable [optional] |
||
515 | * |
||
516 | * @return array |
||
517 | */ |
||
518 | public function getGlobalStatus($variable = null) |
||
522 | } |
||
523 |
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.