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 |
||
| 17 | abstract class AbstractDbRelateTest extends PHPunitTestCase |
||
| 18 | { |
||
| 19 | use TestServiceContainerAwareTrait; |
||
| 20 | |||
| 21 | |||
| 22 | /** |
||
| 23 | * Db connection, default |
||
| 24 | * |
||
| 25 | * @var Adodb |
||
| 26 | */ |
||
| 27 | protected static $db = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Db connection, mysql |
||
| 31 | * |
||
| 32 | * @var Adodb |
||
| 33 | */ |
||
| 34 | protected static $dbMysql = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Service name of db profiles |
||
| 38 | * |
||
| 39 | * @var string[] Index by db profile name |
||
| 40 | */ |
||
| 41 | protected static $dbServiceNames = [ |
||
| 42 | 'db' => 'Db', |
||
| 43 | 'dbMysql' => 'MysqlDb', |
||
| 44 | 'dbSybase' => 'SybaseDb', |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Db connection, Sybase |
||
| 49 | * |
||
| 50 | * @var Adodb |
||
| 51 | */ |
||
| 52 | protected static $dbSybase = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Using db profile |
||
| 56 | * |
||
| 57 | * Available value: default, mysql, sybase |
||
| 58 | * |
||
| 59 | * Multiple value can join with comma. |
||
| 60 | * |
||
| 61 | * Extend and change this value in child class to select which db to use. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected static $dbUsing = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Test table: group |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected static $tableGroup = 'test_group'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Test table: user |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected static $tableUser = 'test_user'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Test table: user_group |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected static $tableUserGroup = 'test_user_group'; |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * Connect to db and assign to static property $dbXxx |
||
| 91 | * |
||
| 92 | * @see $dbUsing |
||
| 93 | * @param string $profile Using db profile, multi splitted by ',' |
||
| 94 | */ |
||
| 95 | protected static function connectDb($profile) |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * @param Adodb $db |
||
| 134 | */ |
||
| 135 | protected static function createTable($db) |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * @param Adodb $db |
||
| 201 | */ |
||
| 202 | protected static function dropTable($db) |
||
| 226 | |||
| 227 | |||
| 228 | View Code Duplication | public static function setUpBeforeClass() |
|
| 245 | |||
| 246 | |||
| 247 | View Code Duplication | public static function tearDownAfterClass() |
|
| 261 | } |
||
| 262 |
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.