Complex classes like Generator 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 Generator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Generator extends \yii\gii\Generator { |
||
| 31 | |||
| 32 | public $db = 'db'; |
||
| 33 | public $guidAttribute = 'guid'; |
||
| 34 | public $idAttribute = 'id'; |
||
| 35 | public $idAttributeType = ''; |
||
| 36 | public $idAttributeTypes = [ |
||
| 37 | 0 => 'Random Number', |
||
| 38 | 1 => 'Random String', |
||
| 39 | 2 => 'Auto Increment', |
||
| 40 | ]; |
||
| 41 | public $passwordHashAttribute = 'pass_hash'; |
||
| 42 | public $createdAtAttribute = 'create_time'; |
||
| 43 | public $updatedAtAttribute = 'update_time'; |
||
| 44 | public $enableIP = true; |
||
| 45 | public $ipAttribute1 = 'ip_1'; |
||
| 46 | public $ipAttribute2 = 'ip_2'; |
||
| 47 | public $ipAttribute3 = 'ip_3'; |
||
| 48 | public $ipAttribute4 = 'ip_4'; |
||
| 49 | public $ipTypeAttribute = 'ip_type'; |
||
| 50 | public $authKeyAttribute = 'auth_key'; |
||
| 51 | public $accessTokenAttribute = 'access_token'; |
||
| 52 | public $passwordResetTokenAttribute = 'password_reset_token'; |
||
| 53 | public $statusAttribute = 'status'; |
||
| 54 | public $sourceAttribute = 'source'; |
||
| 55 | public $ns = 'app\models'; |
||
| 56 | public $tableName; |
||
| 57 | public $modelClass; |
||
| 58 | public $baseClass = 'vistart\Models\models\BaseUserModel'; |
||
| 59 | public $generatePresettingRules = false; |
||
| 60 | public $generatePresettingBehaviors = false; |
||
| 61 | public $generateRelations = true; |
||
| 62 | public $generateLabelsFromComments = false; |
||
| 63 | public $useTablePrefix = false; |
||
| 64 | public $useSchemaName = true; |
||
| 65 | public $generateQuery = false; |
||
| 66 | public $queryNs = 'app\models'; |
||
| 67 | public $queryClass; |
||
| 68 | public $queryBaseClass = 'yii\db\ActiveQuery'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritdoc |
||
| 72 | */ |
||
| 73 | public function getName() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @inheritdoc |
||
| 79 | */ |
||
| 80 | public function getDescription() { |
||
| 81 | return 'This generator generates an BaseUserModel class for the specified database table.'; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @inheritdoc |
||
| 86 | */ |
||
| 87 | public function rules() { |
||
| 88 | return array_merge(parent::rules(), [ |
||
| 89 | [['db', 'ns', 'tableName', 'guidAttribute', 'idAttribute', 'passwordHashAttribute', 'createdAtAttribute', 'updatedAtAttribute', 'ipAttribute1', 'ipAttribute2', 'ipAttribute3', 'ipAttribute4', 'ipTypeAttribute', 'authKeyAttribute', 'accessTokenAttribute', 'passwordResetTokenAttribute', 'statusAttribute', 'sourceAttribute', 'modelClass', 'baseClass', 'queryNs', 'queryClass', 'queryBaseClass'], 'filter', 'filter' => 'trim'], |
||
| 90 | [['ns', 'queryNs'], 'filter', 'filter' => function($value) { |
||
| 91 | return trim($value, '\\'); |
||
| 92 | }], |
||
| 93 | [['db', 'ns', 'tableName', 'baseClass', 'queryNs', 'queryBaseClass', 'guidAttribute', 'passwordHashAttribute', 'authKeyAttribute', 'accessTokenAttribute', 'passwordResetTokenAttribute'], 'required'], |
||
| 94 | [['db', 'modelClass', 'queryClass'], 'match', 'pattern' => '/^\w+$/', 'message' => 'Only word characters are allowed.'], |
||
| 95 | [['ns', 'baseClass', 'queryNs', 'queryBaseClass'], 'match', 'pattern' => '/^[\w\\\\]+$/', 'message' => 'Only word characters and backslashes are allowed.'], |
||
| 96 | [['tableName'], 'match', 'pattern' => '/^(\w+\.)?([\w\*]+)$/', 'message' => 'Only word characters, and optionally an asterisk and/or a dot are allowed.'], |
||
| 97 | [['db'], 'validateDb'], |
||
| 98 | [['ns', 'queryNs'], 'validateNamespace'], |
||
| 99 | [['tableName'], 'validateTableName'], |
||
| 100 | [['modelClass'], 'validateModelClass', 'skipOnEmpty' => false], |
||
| 101 | [['baseClass'], 'validateClass', 'params' => ['extends' => ActiveRecord::className()]], |
||
| 102 | [['queryBaseClass'], 'validateClass', 'params' => ['extends' => ActiveQuery::className()]], |
||
| 103 | [['enableIP', 'generatePresettingRules', 'generatePresettingBehaviors', 'generateRelations', 'generateLabelsFromComments', 'useTablePrefix', 'useSchemaName', 'generateQuery'], 'boolean'], |
||
| 104 | [['guidAttribute', 'idAttribute', 'passwordHashAttribute', 'createdAtAttribute', 'updatedAtAttribute', 'ipAttribute1', 'ipAttribute2', 'ipAttribute3', 'ipAttribute4', 'ipTypeAttribute', 'authKeyAttribute', 'accessTokenAttribute', 'passwordResetTokenAttribute', 'statusAttribute', 'sourceAttribute'], 'validateFieldsExist'], |
||
| 105 | [['enableIP', 'ipAttribute1', 'ipAttribute2', 'ipAttribute3', 'ipAttribute4', 'ipTypeAttribute'], 'validateIP'], |
||
| 106 | [['enableI18N'], 'boolean'], |
||
| 107 | [['messageCategory'], 'validateMessageCategory', 'skipOnEmpty' => false], |
||
| 108 | ]); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @inheritdoc |
||
| 113 | */ |
||
| 114 | public function attributeLabels() { |
||
| 115 | return array_merge(parent::attributeLabels(), [ |
||
| 116 | 'ns' => 'Namespace', |
||
| 117 | 'db' => 'Database Connection ID', |
||
| 118 | // 'guidAttribute' => 'guid', |
||
| 119 | // 'idAttribute' => 'id', |
||
| 120 | // 'passwordHashAttribute' => 'pass_hash', |
||
| 121 | // 'createdAtAttribute' => 'create_time', |
||
| 122 | // 'updatedAtAttribute' => 'update_time', |
||
| 123 | // 'enableIP' => 'enableIP', |
||
| 124 | // 'ipAttribute1' => 'ip_1', |
||
| 125 | // 'ipAttribute2' => 'ip_2', |
||
| 126 | // 'ipAttribute3' => 'ip_3', |
||
| 127 | // 'ipAttribute4' => 'ip_4', |
||
| 128 | // 'ipTypeAttribute' => 'ip_type', |
||
| 129 | // 'authKeyAttribute' => 'auth_key', |
||
| 130 | // 'accessTokenAttribute' => 'access_token', |
||
| 131 | // 'passwordResetTokenAttribute' => 'password_reset_token', |
||
| 132 | // 'statusAttribute' => 'status', |
||
| 133 | // 'sourceAttribute' => 'source', |
||
| 134 | 'tableName' => 'Table Name', |
||
| 135 | 'modelClass' => 'Model Class', |
||
| 136 | 'baseClass' => 'Base Class', |
||
| 137 | 'generatePresettingRules' => 'Generate Presetting Rules', |
||
| 138 | 'generatePresettingBehaviors' => 'Generate Presetting Behaviors', |
||
| 139 | 'generateRelations' => 'Generate Relations', |
||
| 140 | 'generateLabelsFromComments' => 'Generate Labels from DB Comments', |
||
| 141 | 'generateQuery' => 'Generate ActiveQuery', |
||
| 142 | 'queryNs' => 'ActiveQuery Namespace', |
||
| 143 | 'queryClass' => 'ActiveQuery Class', |
||
| 144 | 'queryBaseClass' => 'ActiveQuery Base Class', |
||
| 145 | ]); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritdoc |
||
| 150 | */ |
||
| 151 | public function hints() { |
||
| 152 | return array_merge(parent::hints(), [ |
||
| 153 | 'ns' => 'This is the namespace of the ActiveRecord class to be generated, e.g., <code>app\models</code>', |
||
| 154 | 'db' => 'This is the ID of the DB application component.', |
||
| 155 | 'tableName' => 'This is the name of the DB table that the new ActiveRecord class is associated with, e.g. <code>post</code>. |
||
| 156 | The table name may consist of the DB schema part if needed, e.g. <code>public.post</code>. |
||
| 157 | The table name may end with asterisk to match multiple table names, e.g. <code>tbl_*</code> |
||
| 158 | will match tables who name starts with <code>tbl_</code>. In this case, multiple ActiveRecord classes |
||
| 159 | will be generated, one for each matching table name; and the class names will be generated from |
||
| 160 | the matching characters. For example, table <code>tbl_post</code> will generate <code>Post</code> |
||
| 161 | class.', |
||
| 162 | 'modelClass' => 'This is the name of the ActiveRecord class to be generated. The class name should not contain |
||
| 163 | the namespace part as it is specified in "Namespace". You do not need to specify the class name |
||
| 164 | if "Table Name" ends with asterisk, in which case multiple ActiveRecord classes will be generated.', |
||
| 165 | 'baseClass' => 'This is the base class of the new ActiveRecord class. It should be a fully qualified namespaced class name.', |
||
| 166 | 'guidAttribute' => 'REQUIRED. GUID Attribute Name, as well as field name of user table.', |
||
| 167 | 'idAttribute' => 'OPTIONAL. ID Attribute Name, as well as field name of user table. Please left empty if no need for this feature.', |
||
| 168 | 'passwordHashAttribute' => 'REQUIRED. Password Hash Attribute Name, as well as field name of user table.', |
||
| 169 | 'createdAtAttribute' => 'OPTIONAL. Created At Attribute Name, as well as field name of user table. Please left empty if no need for this feature.', |
||
| 170 | 'updatedAtAttribute' => 'OPTIONAL. Updated At Attribute Name, as well as field name of user table. Please left empty if no need for this feature.', |
||
| 171 | 'enableIP' => 'Indicates whether enable the IP address feature. If true, the following five attributes are required.', |
||
| 172 | 'ipAttribute1' => 'IP Attribute 1 Name, as well as field name of user table.', |
||
| 173 | 'ipAttribute2' => 'IP Attribute 2 Name, as well as field name of user table.', |
||
| 174 | 'ipAttribute3' => 'IP Attribute 3 Name, as well as field name of user table.', |
||
| 175 | 'ipAttribute4' => 'IP Attribute 4 Name, as well as field name of user table.', |
||
| 176 | 'ipTypeAttribute' => 'IP Type Attribute Name, as well as field name of user table.', |
||
| 177 | 'authKeyAttribute' => 'REQUIRED. Auth Key Attribute Name, as well as field name of user table.', |
||
| 178 | 'accessTokenAttribute' => 'REQUIRED. Access Token Attribute Name, as well as field name of user table.', |
||
| 179 | 'passwordResetTokenAttribute' => 'OPTIONAL. Password Reset Token Attribute Name, as well as field name of user table. Please left empty if no need for this feature.', |
||
| 180 | 'sourceAttribute' => 'OPTIONAL. Source Attribute Name, as well as field name of user table. Please left empty if no need for this feature.', |
||
| 181 | 'statusAttribute' => 'OPTIONAL. Status Attribute Name, as well as field name of user table. Please left empty if no need for this feature.', |
||
| 182 | 'generatePresettingRules' => 'This indicates whether the generator should generate the presetting rules.', |
||
| 183 | 'generatePresettingBehaviors' => 'This indicates whether the generator should generate the presetting behaviors.', |
||
| 184 | 'generateRelations' => 'This indicates whether the generator should generate relations based on |
||
| 185 | foreign key constraints it detects in the database. Note that if your database contains too many tables, |
||
| 186 | you may want to uncheck this option to accelerate the code generation process.', |
||
| 187 | 'generateLabelsFromComments' => 'This indicates whether the generator should generate attribute labels |
||
| 188 | by using the comments of the corresponding DB columns.', |
||
| 189 | 'useTablePrefix' => 'This indicates whether the table name returned by the generated ActiveRecord class |
||
| 190 | should consider the <code>tablePrefix</code> setting of the DB connection. For example, if the |
||
| 191 | table name is <code>tbl_post</code> and <code>tablePrefix=tbl_</code>, the ActiveRecord class |
||
| 192 | will return the table name as <code>{{%post}}</code>.', |
||
| 193 | 'useSchemaName' => 'This indicates whether to include the schema name in the ActiveRecord class |
||
| 194 | when it\'s auto generated. Only non default schema would be used.', |
||
| 195 | 'generateQuery' => 'This indicates whether to generate ActiveQuery for the ActiveRecord class.', |
||
| 196 | 'queryNs' => 'This is the namespace of the ActiveQuery class to be generated, e.g., <code>app\models</code>', |
||
| 197 | 'queryClass' => 'This is the name of the ActiveQuery class to be generated. The class name should not contain |
||
| 198 | the namespace part as it is specified in "ActiveQuery Namespace". You do not need to specify the class name |
||
| 199 | if "Table Name" ends with asterisk, in which case multiple ActiveQuery classes will be generated.', |
||
| 200 | 'queryBaseClass' => 'This is the base class of the new ActiveQuery class. It should be a fully qualified namespaced class name.', |
||
| 201 | ]); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @inheritdoc |
||
| 206 | */ |
||
| 207 | public function autoCompleteData() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @inheritdoc |
||
| 222 | */ |
||
| 223 | public function requiredTemplates() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @inheritdoc |
||
| 230 | */ |
||
| 231 | public function stickyAttributes() { |
||
| 234 | |||
| 235 | public function generate() { |
||
| 236 | $files = []; |
||
| 237 | $relations = $this->generateRelations(); |
||
| 238 | $db = $this->getDbConnection(); |
||
| 239 | foreach ($this->getTableNames() as $tableName) { |
||
| 240 | // model : |
||
| 241 | $modelClassName = $this->generateClassName($tableName); |
||
| 242 | $queryClassName = ($this->generateQuery) ? $this->generateQueryClassName($modelClassName) : false; |
||
| 243 | $tableSchema = $db->getTableSchema($tableName); |
||
| 244 | $params = [ |
||
| 245 | 'tableName' => $tableName, |
||
| 246 | 'className' => $modelClassName, |
||
| 247 | 'queryClassName' => $queryClassName, |
||
| 248 | 'tableSchema' => $tableSchema, |
||
| 249 | 'labels' => $this->generateLabels($tableSchema), |
||
| 250 | 'rules' => $this->generatePresettingRules, |
||
| 251 | 'behaviors' => $this->generatePresettingBehaviors, |
||
| 252 | 'relations' => isset($relations[$tableName]) ? $relations[$tableName] : [], |
||
| 253 | ]; |
||
| 254 | $files[] = new CodeFile( |
||
| 255 | Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/' . $modelClassName . '.php', $this->render('model.php', $params) |
||
| 256 | ); |
||
| 257 | |||
| 258 | // query : |
||
| 259 | if ($queryClassName) { |
||
|
|
|||
| 260 | $params = [ |
||
| 261 | 'className' => $queryClassName, |
||
| 262 | 'modelClassName' => $modelClassName, |
||
| 263 | ]; |
||
| 264 | $files[] = new CodeFile( |
||
| 265 | Yii::getAlias('@' . str_replace('\\', '/', $this->queryNs)) . '/' . $queryClassName . '.php', $this->render('query.php', $params) |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | return $files; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Generates the attribute labels for the specified table. |
||
| 274 | * @param \yii\db\TableSchema $table the table schema |
||
| 275 | * @return array the generated attribute labels (name => label) |
||
| 276 | */ |
||
| 277 | public function generateLabels($table) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Generates relations using a junction table by adding an extra viaTable(). |
||
| 298 | * @param \yii\db\TableSchema the table being checked |
||
| 299 | * @param array $fks obtained from the checkPivotTable() method |
||
| 300 | * @param array $relations |
||
| 301 | * @return array modified $relations |
||
| 302 | */ |
||
| 303 | private function generateManyManyRelations($table, $fks, $relations) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return array the generated relation declarations |
||
| 337 | */ |
||
| 338 | protected function generateRelations() { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Generates the link parameter to be used in generating the relation declaration. |
||
| 417 | * @param array $refs reference constraint |
||
| 418 | * @return string the generated link parameter. |
||
| 419 | */ |
||
| 420 | protected function generateRelationLink($refs) { |
||
| 428 | |||
| 429 | protected function generateRules($table) { |
||
| 430 | |||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Checks if the given table is a junction table. |
||
| 435 | * For simplicity, this method only deals with the case where the pivot contains two PK columns, |
||
| 436 | * each referencing a column in a different table. |
||
| 437 | * @param \yii\db\TableSchema the table being checked |
||
| 438 | * @return array|boolean the relevant foreign key constraint information if the table is a junction table, |
||
| 439 | * or false if the table is not a junction table. |
||
| 440 | */ |
||
| 441 | protected function checkPivotTable($table) { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Generate a relation name for the specified table and a base name. |
||
| 465 | * @param array $relations the relations being generated currently. |
||
| 466 | * @param \yii\db\TableSchema $table the table schema |
||
| 467 | * @param string $key a base name that the relation name may be generated from |
||
| 468 | * @param boolean $multiple whether this is a has-many relation |
||
| 469 | * @return string the relation name |
||
| 470 | */ |
||
| 471 | protected function generateRelationName($relations, $table, $key, $multiple) { |
||
| 489 | |||
| 490 | public function validateFieldsExist() { |
||
| 491 | $db = $this->getDbConnection(); |
||
| 492 | foreach ($this->getTableNames() as $table) { |
||
| 493 | $tableSchema = $db->getTableSchema($table); |
||
| 494 | $fields = [ |
||
| 495 | 'guidAttribute', |
||
| 496 | 'idAttribute', |
||
| 497 | 'passwordHashAttribute', |
||
| 498 | 'createdAtAttribute', |
||
| 499 | 'updatedAtAttribute', |
||
| 500 | 'ipAttribute1', |
||
| 501 | 'ipAttribute2', |
||
| 502 | 'ipAttribute3', |
||
| 503 | 'ipAttribute4', |
||
| 504 | 'ipTypeAttribute', |
||
| 505 | 'authKeyAttribute', |
||
| 506 | 'accessTokenAttribute', |
||
| 507 | 'passwordResetTokenAttribute', |
||
| 508 | 'statusAttribute', |
||
| 509 | 'sourceAttribute', |
||
| 510 | ]; |
||
| 511 | foreach ($fields as $field) { |
||
| 512 | if (isset($this->$field) && $this->$field != '' && !isset($tableSchema->columns[$this->$field])) { |
||
| 513 | $this->addError($field, '`' . $this->$field . '` field does not exist in `' . $table . '` table.'); |
||
| 514 | continue; |
||
| 515 | } |
||
| 516 | } |
||
| 517 | } |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Validates the [[db]] attribute. |
||
| 522 | */ |
||
| 523 | public function validateDb() { |
||
| 530 | |||
| 531 | public function validateIP() { |
||
| 532 | if (!$this->enableIP) { |
||
| 533 | return; |
||
| 534 | } |
||
| 535 | $db = $this->getDbConnection(); |
||
| 536 | $ipAttribute = 'ipAttribute'; |
||
| 537 | for ($i = 1; $i <= 4; $i++) { |
||
| 538 | $ipAttributeName = $ipAttribute . $i; |
||
| 539 | if (empty($this->$ipAttributeName)) { |
||
| 540 | $this->addError($ipAttributeName, "`$ipAttributeName` should not be empty."); |
||
| 541 | continue; |
||
| 542 | } |
||
| 543 | } |
||
| 544 | if (empty($this->ipTypeAttribute)) { |
||
| 545 | $this->addError('ipTypeAttribute', "`ipTypeAttribute` should not be empty."); |
||
| 546 | return; |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Validates the [[ns]] attribute. |
||
| 552 | */ |
||
| 553 | public function validateNamespace() { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Validates the [[modelClass]] attribute. |
||
| 563 | */ |
||
| 564 | public function validateModelClass() { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Validates the [[tableName]] attribute. |
||
| 575 | */ |
||
| 576 | public function validateTableName() { |
||
| 595 | |||
| 596 | protected $tableNames; |
||
| 597 | protected $classNames; |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @return array the table names that match the pattern specified by [[tableName]]. |
||
| 601 | */ |
||
| 602 | protected function getTableNames() { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Generates the table name by considering table prefix. |
||
| 635 | * If [[useTablePrefix]] is false, the table name will be returned without change. |
||
| 636 | * @param string $tableName the table name (which may contain schema prefix) |
||
| 637 | * @return string the generated table name |
||
| 638 | */ |
||
| 639 | public function generateTableName($tableName) { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Generates a class name from the specified table name. |
||
| 655 | * @param string $tableName the table name (which may contain schema prefix) |
||
| 656 | * @param boolean $useSchemaName should schema name be included in the class name, if present |
||
| 657 | * @return string the generated class name |
||
| 658 | */ |
||
| 659 | protected function generateClassName($tableName, $useSchemaName = null) { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Generates a query class name from the specified model class name. |
||
| 697 | * @param string $modelClassName model class name |
||
| 698 | * @return string generated class name |
||
| 699 | */ |
||
| 700 | protected function generateQueryClassName($modelClassName) { |
||
| 707 | |||
| 708 | /** |
||
| 709 | * @return Connection the DB connection as specified by [[db]]. |
||
| 710 | */ |
||
| 711 | protected function getDbConnection() { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Checks if any of the specified columns is auto incremental. |
||
| 717 | * @param \yii\db\TableSchema $table the table schema |
||
| 718 | * @param array $columns columns to check for autoIncrement property |
||
| 719 | * @return boolean whether any of the specified columns is auto incremental. |
||
| 720 | */ |
||
| 721 | protected function isColumnAutoIncremental($table, $columns) { |
||
| 730 | |||
| 731 | } |
||
| 732 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: