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 |
||
| 18 | class Explain extends Basic |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @const EXPLAIN_OK Explain status when the request has succeeded |
||
| 22 | */ |
||
| 23 | const EXPLAIN_OK = 'ok'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @const EXPLAIN_FAILED Explain status when the request has failed |
||
| 27 | */ |
||
| 28 | const EXPLAIN_FAILED = 'failed'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @const EXPLAIN_EMPTY Explain status when the request has returned nothing |
||
| 32 | */ |
||
| 33 | const EXPLAIN_EMPTY = 'empty'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \BfwSql\Sql The sql instanced used to run EXPLAIN request |
||
| 37 | */ |
||
| 38 | protected $sql; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \stdClass An object with explain status and informations returned |
||
| 42 | */ |
||
| 43 | protected $explain; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Getter accessor to property sql |
||
| 47 | * |
||
| 48 | * @return \BfwSql\Sql |
||
| 49 | */ |
||
| 50 | public function getSql() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Getter accessor to property explain |
||
| 57 | * |
||
| 58 | * @return \stdClass |
||
| 59 | */ |
||
| 60 | public function getExplain() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | * Limited to "system query" event. |
||
| 68 | */ |
||
| 69 | protected function analyzeUpdate() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | * Limited to \BfwSql\Actions\Select object |
||
| 79 | * We not run EXPLAIN automatically on other request type to not destroy db |
||
| 80 | * |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | protected function systemQuery() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Obtain a \BfwSql\Sql instance. |
||
| 110 | * |
||
| 111 | * @return \BfwSql\Sql |
||
| 112 | */ |
||
| 113 | protected function obtainSql() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Run the EXPLAIN request |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | protected function runExplain() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | * Add explain informations to monolog too. |
||
| 157 | */ |
||
| 158 | protected function addQueryToMonoLog($query, $error) |
||
| 168 | } |
||
| 169 |
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.