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 | * This is a helper class for the SS installer. | ||
| 12 | * | ||
| 13 | * It does all the specific checking for MSSQLDatabase | ||
| 14 | * to ensure that the configuration is setup correctly. | ||
| 15 | * | ||
| 16 | * @package mssql | ||
| 17 | */ | ||
| 18 | class MSSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper | ||
| 19 | { | ||
| 20 | |||
| 21 | protected function isAzure($databaseConfig) | ||
| 25 | |||
| 26 | /** | ||
| 27 | * Create a connection of the appropriate type | ||
| 28 | * | ||
| 29 | * @param array $databaseConfig | ||
| 30 | * @param string $error Error message passed by value | ||
| 31 | * @return mixed|null Either the connection object, or null if error | ||
| 32 | */ | ||
| 33 | protected function createConnection($databaseConfig, &$error) | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Helper function to quote a string value | ||
| 86 | * | ||
| 87 | * @param mixed $conn Connection object/resource | ||
| 88 | * @param string $value Value to quote | ||
| 89 | * @return string Quoted strieng | ||
| 90 | */ | ||
| 91 | protected function quote($conn, $value) | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Helper function to execute a query | ||
| 106 | * | ||
| 107 | * @param mixed $conn Connection object/resource | ||
| 108 | * @param string $sql SQL string to execute | ||
| 109 | * @return array List of first value from each resulting row | ||
| 110 | */ | ||
| 111 | protected function query($conn, $sql) | ||
| 131 | |||
| 132 | public function requireDatabaseFunctions($databaseConfig) | ||
| 137 | |||
| 138 | View Code Duplication | public function requireDatabaseServer($databaseConfig) | |
| 148 | |||
| 149 | View Code Duplication | public function requireDatabaseConnection($databaseConfig) | |
| 160 | |||
| 161 | public function getDatabaseVersion($databaseConfig) | ||
| 167 | |||
| 168 | /** | ||
| 169 | * Ensure that the SQL Server version is at least 10.00.2531 (SQL Server 2008 SP1). | ||
| 170 | * | ||
| 171 | * @see http://www.sqlteam.com/article/sql-server-versions | ||
| 172 | * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc | ||
| 173 |      * @return array Result - e.g. array('success' => true, 'error' => 'details of error') | ||
| 174 | */ | ||
| 175 | public function requireDatabaseVersion($databaseConfig) | ||
| 195 | |||
| 196 | public function requireDatabaseOrCreatePermissions($databaseConfig) | ||
| 224 | |||
| 225 | public function requireDatabaseAlterPermissions($databaseConfig) | ||
| 226 |     { | ||
| 227 | $success = false; | ||
| 228 | $conn = $this->createConnection($databaseConfig, $error); | ||
| 229 |         if (!empty($conn)) { | ||
| 230 |             if (!$this->isAzure($databaseConfig)) { | ||
| 231 | // Make sure to select the current database when checking permission against this database | ||
| 232 |                 $this->query($conn, "USE \"{$databaseConfig['database']}\""); | ||
| 233 | } | ||
| 234 | $permissions = $this->query($conn, "select COUNT(*) from sys.fn_my_permissions(NULL,'DATABASE') WHERE permission_name like 'create table';"); | ||
| 235 | $success = $permissions[0] > 0; | ||
| 236 | } | ||
| 237 | |||
| 244 |