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 |
||
| 22 | class InstallContext implements Context |
||
| 23 | { |
||
| 24 | use CommonContextTrait; |
||
| 25 | |||
| 26 | private static $yawikGlobalConfig; |
||
| 27 | |||
| 28 | private static $yawikBackupConfig; |
||
| 29 | |||
| 30 | public function __construct() |
||
| 31 | { |
||
| 32 | static::$yawikGlobalConfig = getcwd() . '/config/autoload/yawik.config.global.php'; |
||
| 33 | static::$yawikBackupConfig = str_replace('yawik.config.global.php', 'yawik.backup', static::$yawikGlobalConfig); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @Given I have install module activated |
||
| 38 | */ |
||
| 39 | public function iHaveInstallModuleActivated() |
||
| 40 | { |
||
| 41 | // backup existing file |
||
| 42 | $yawikBackupConfig = static::$yawikBackupConfig; |
||
| 43 | $yawikGlobalConfig = static::$yawikGlobalConfig; |
||
| 44 | |||
| 45 | if (is_file($yawikGlobalConfig)) { |
||
| 46 | rename($yawikGlobalConfig, $yawikBackupConfig); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @Given I go to the install page |
||
| 52 | */ |
||
| 53 | public function iGoToInstallPage() |
||
| 54 | { |
||
| 55 | $url = $this->minkContext->locatePath('/'); |
||
| 56 | $this->visit($url); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @AfterSuite |
||
| 61 | */ |
||
| 62 | public static function tearDown() |
||
| 63 | { |
||
| 64 | static::restoreConfig(); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @AfterScenario |
||
| 69 | */ |
||
| 70 | public function afterScenario() |
||
| 71 | { |
||
| 72 | static::restoreConfig(); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @BeforeSuite |
||
| 77 | */ |
||
| 78 | public static function setUp() |
||
| 79 | { |
||
| 80 | static::restoreConfig(); |
||
| 81 | } |
||
| 82 | |||
| 83 | public static function restoreConfig() |
||
| 84 | { |
||
| 85 | // restore backup |
||
| 86 | $yawikBackupConfig = static::$yawikBackupConfig; |
||
| 87 | $yawikGlobalConfig = static::$yawikGlobalConfig; |
||
| 88 | |||
| 89 | if (is_file($yawikBackupConfig)) { |
||
| 90 | rename($yawikBackupConfig, $yawikGlobalConfig); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @Given I fill database connection with an active connection |
||
| 96 | */ |
||
| 97 | public function iFillActiveConnectionString() |
||
| 98 | { |
||
| 99 | $config = $this->getService('config'); |
||
| 100 | $connection = $config['doctrine']['connection']['odm_default']['connectionString']; |
||
| 101 | $this->minkContext->fillField('db_conn', $connection); |
||
| 102 | } |
||
| 103 | } |
||
| 104 |