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 |
||
| 11 | class Result { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * The storage query that produced this result. |
||
| 15 | * |
||
| 16 | * @var Query |
||
| 17 | */ |
||
| 18 | protected $query; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * An associative array of the result data. |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $data; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The error that occurred when executing the query, if any. |
||
| 29 | * |
||
| 30 | * @var Error|null |
||
| 31 | */ |
||
| 32 | protected $error; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The number of rows in the result data. |
||
| 36 | * |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | protected $count; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The set of fields available for each row in the result. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $fields; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Auto incremented primary key of an inserted row. |
||
| 50 | * |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | protected $insertId; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Error captured from the query that produced this result. |
||
| 57 | * |
||
| 58 | * @var Error |
||
| 59 | */ |
||
| 60 | protected $error; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Instantiate a new storage query result. |
||
| 64 | * |
||
| 65 | * @param Query $query |
||
| 66 | * @param array $data [optional] |
||
| 67 | * @param array $info [optional] |
||
| 68 | * @param Error $error [optional] |
||
| 69 | */ |
||
| 70 | public function __construct(Query $query, array $data = array(), array $info = array(), Error $error = null) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Set the result info. |
||
| 78 | * |
||
| 79 | * Accepts the keys 'affected', 'count', 'insert_id' and 'fields'. |
||
| 80 | * |
||
| 81 | * @param array $info |
||
| 82 | */ |
||
| 83 | View Code Duplication | protected function setInfo(array $info) { |
|
| 96 | |||
| 97 | } |
||
| 98 |
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.