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:
Complex classes like setup_cmd_database often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use setup_cmd_database, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class setup_cmd_database extends setup_cmd |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Allow to run this command via setup-cli |
||
| 22 | */ |
||
| 23 | const SETUP_CLI_CALLABLE = true; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Maximum length of database name (at least for MySQL this is the limit) |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | const MAX_DB_NAME_LEN = 16; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Instance of Api\Db to connect or create the db |
||
| 34 | * |
||
| 35 | * @var Api\Db |
||
| 36 | */ |
||
| 37 | private $test_db; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor |
||
| 41 | * |
||
| 42 | * @param string/array $domain domain-name to customize the defaults or array with all parameters |
||
| 43 | * @param string $db_type db-type (mysql, pgsql, ...) |
||
| 44 | * @param string $db_host =null |
||
| 45 | * @param string $db_port =null |
||
| 46 | * @param string $db_name =null |
||
| 47 | * @param string $db_user =null |
||
| 48 | * @param string $db_pass =null |
||
| 49 | * @param string $db_root =null |
||
| 50 | * @param string $db_root_pw =null |
||
| 51 | * @param string $sub_command ='create_db' 'create_db', 'test_db', 'test_db_root' |
||
| 52 | * @param string $db_grant_host ='localhost' host/ip of webserver for grant |
||
| 53 | * @param boolean $make_db_name_unique =false true: if create fails because db exists, |
||
| 54 | * try creating a unique name by shortening the name and adding a number to it |
||
| 55 | */ |
||
| 56 | function __construct($domain,$db_type=null,$db_host=null,$db_port=null,$db_name=null,$db_user=null,$db_pass=null, |
||
| 57 | $db_root=null,$db_root_pw=null,$sub_command='create_db',$db_grant_host='localhost',$make_db_name_unique=false) |
||
| 58 | { |
||
| 59 | if (!is_array($domain)) |
||
| 60 | { |
||
| 61 | $data = array( |
||
| 62 | 'domain' => $domain, |
||
| 63 | 'db_type' => $db_type, |
||
| 64 | 'db_host' => $db_host, |
||
| 65 | 'db_port' => $db_port, |
||
| 66 | 'db_name' => $db_name, |
||
| 67 | 'db_user' => $db_user, |
||
| 68 | 'db_pass' => $db_pass, |
||
| 69 | 'db_root' => $db_root, |
||
| 70 | 'db_root_pw' => $db_root_pw, |
||
| 71 | 'sub_command' => $sub_command, |
||
| 72 | 'db_grant_host' => $db_grant_host, |
||
| 73 | 'make_db_name_unique' => $make_db_name_unique, |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | else |
||
| 77 | { |
||
| 78 | $data = $domain; |
||
| 79 | } |
||
| 80 | // need to incorporate correct defaults for given database type |
||
| 81 | admin_cmd::__construct(array_merge(self::defaults($data['db_type']), array_diff($data, array(null)))); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * run the command: test or create database |
||
| 86 | * |
||
| 87 | * @param boolean $check_only =false only run the checks (and throw the exceptions), but not the command itself |
||
| 88 | * @return string success message |
||
| 89 | * @throws Exception(lang('Wrong credentials to access the header.inc.php file!'),2); |
||
| 90 | * @throws Exception('header.inc.php not found!'); |
||
| 91 | */ |
||
| 92 | protected function exec($check_only=false) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Connect to database |
||
| 135 | * |
||
| 136 | * @param string $user =null default $this->db_user |
||
| 137 | * @param string $pass =null default $this->db_pass |
||
| 138 | * @param string $name =null default $this->db_name |
||
| 139 | * @throws Api\Exception\WrongUserinput Can not connect to database ... |
||
| 140 | */ |
||
| 141 | private function connect($user=null,$pass=null,$name=null) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Check and if does not yet exist create the new database and user |
||
| 167 | * |
||
| 168 | * The check will fail if the database exists, but already contains tables |
||
| 169 | * |
||
| 170 | * if $this->make_db_name_unique is set, a decrementing nummeric prefix gets |
||
| 171 | * added to $this->db_name AND $this->db_user, if db already exists. |
||
| 172 | * |
||
| 173 | * @return string with success message |
||
| 174 | * @throws Api\Exception\WrongUserinput |
||
| 175 | */ |
||
| 176 | private function create() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Drop database and user |
||
| 250 | * |
||
| 251 | * @return string with success message |
||
| 252 | * @throws Api\Exception\WrongUserinput |
||
| 253 | * @throws Api\Db\Exception if database not exist |
||
| 254 | */ |
||
| 255 | private function drop() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Return default database settings for a given domain |
||
| 274 | * |
||
| 275 | * @param string $db_type ='mysqli' |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | static function defaults($db_type='mysqli') |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Merges the default into the current properties, if they are empty or contain placeholders |
||
| 307 | */ |
||
| 308 | private function _merge_defaults() |
||
| 326 | } |
||
| 327 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.