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) |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Create a connection of the appropriate type |
||
| 29 | * |
||
| 30 | * @param array $databaseConfig |
||
| 31 | * @param string $error Error message passed by value |
||
| 32 | * @return mixed|null Either the connection object, or null if error |
||
| 33 | */ |
||
| 34 | protected function createConnection($databaseConfig, &$error) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Helper function to quote a string value |
||
| 88 | * |
||
| 89 | * @param mixed $conn Connection object/resource |
||
| 90 | * @param string $value Value to quote |
||
| 91 | * @return string Quoted strieng |
||
| 92 | */ |
||
| 93 | protected function quote($conn, $value) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Helper function to execute a query |
||
| 108 | * |
||
| 109 | * @param mixed $conn Connection object/resource |
||
| 110 | * @param string $sql SQL string to execute |
||
| 111 | * @return array List of first value from each resulting row |
||
| 112 | */ |
||
| 113 | protected function query($conn, $sql) |
||
| 133 | |||
| 134 | public function requireDatabaseFunctions($databaseConfig) |
||
| 139 | |||
| 140 | View Code Duplication | public function requireDatabaseServer($databaseConfig) |
|
| 150 | |||
| 151 | View Code Duplication | public function requireDatabaseConnection($databaseConfig) |
|
| 162 | |||
| 163 | public function getDatabaseVersion($databaseConfig) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Ensure that the SQL Server version is at least 10.00.2531 (SQL Server 2008 SP1). |
||
| 172 | * |
||
| 173 | * @see http://www.sqlteam.com/article/sql-server-versions |
||
| 174 | * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc |
||
| 175 | * @return array Result - e.g. array('success' => true, 'error' => 'details of error') |
||
| 176 | */ |
||
| 177 | public function requireDatabaseVersion($databaseConfig) |
||
| 197 | |||
| 198 | public function requireDatabaseOrCreatePermissions($databaseConfig) |
||
| 227 | |||
| 228 | public function requireDatabaseAlterPermissions($databaseConfig) |
||
| 229 | { |
||
| 230 | $success = false; |
||
| 231 | $conn = $this->createConnection($databaseConfig, $error); |
||
| 232 | if (!empty($conn)) { |
||
| 233 | if (!$this->isAzure($databaseConfig)) { |
||
| 234 | // Make sure to select the current database when checking permission against this database |
||
| 235 | $this->query($conn, "USE \"{$databaseConfig['database']}\""); |
||
| 236 | } |
||
| 237 | $permissions = $this->query($conn, "select COUNT(*) from sys.fn_my_permissions(NULL,'DATABASE') WHERE permission_name like 'create table';"); |
||
| 247 |