| Conditions | 28 |
| Paths | 8160 |
| Total Lines | 88 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 63 | public function generateRules($table) |
||
| 64 | { |
||
| 65 | $types = []; |
||
| 66 | $lengths = []; |
||
| 67 | foreach ($table->columns as $column) { |
||
| 68 | if ($column->autoIncrement) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | if (!$column->allowNull && $column->defaultValue === null) { |
||
| 72 | $types['required'][] = $column->name; |
||
| 73 | } |
||
| 74 | switch ($column->type) { |
||
| 75 | case Schema::TYPE_SMALLINT: |
||
| 76 | case Schema::TYPE_INTEGER: |
||
| 77 | case Schema::TYPE_BIGINT: |
||
| 78 | $types['integer'][] = $column->name; |
||
| 79 | break; |
||
| 80 | case Schema::TYPE_BOOLEAN: |
||
| 81 | $types['boolean'][] = $column->name; |
||
| 82 | break; |
||
| 83 | case Schema::TYPE_FLOAT: |
||
| 84 | case 'double': // Schema::TYPE_DOUBLE, which is available since Yii 2.0.3 |
||
| 85 | case Schema::TYPE_DECIMAL: |
||
| 86 | case Schema::TYPE_MONEY: |
||
| 87 | $types['number'][] = $column->name; |
||
| 88 | break; |
||
| 89 | case Schema::TYPE_DATE: |
||
| 90 | case Schema::TYPE_TIME: |
||
| 91 | case Schema::TYPE_DATETIME: |
||
| 92 | case Schema::TYPE_TIMESTAMP: |
||
| 93 | $types['safe'][] = $column->name; |
||
| 94 | break; |
||
| 95 | default: // strings |
||
| 96 | if ($column->size > 0) { |
||
| 97 | $lengths[$column->size][] = $column->name; |
||
| 98 | } else { |
||
| 99 | $types['string'][] = $column->name; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | $rules = []; |
||
| 104 | foreach ($types as $type => $columns) { |
||
| 105 | $rules[] = "[['" . implode("', '", $columns) . "'], '$type']"; |
||
| 106 | } |
||
| 107 | foreach ($lengths as $length => $columns) { |
||
| 108 | $rules[] = "[['" . implode("', '", $columns) . "'], 'string', 'max' => $length]"; |
||
| 109 | } |
||
| 110 | $db = $this->getDbConnection(); |
||
| 111 | // Unique indexes rules |
||
| 112 | try { |
||
| 113 | $uniqueIndexes = $db->getSchema()->findUniqueIndexes($table); |
||
| 114 | foreach ($uniqueIndexes as $uniqueColumns) { |
||
| 115 | // Avoid validating auto incremental columns |
||
| 116 | if (!$this->isColumnAutoIncremental($table, $uniqueColumns)) { |
||
| 117 | $attributesCount = count($uniqueColumns); |
||
| 118 | if ($attributesCount === 1) { |
||
| 119 | $rules[] = "[['" . $uniqueColumns[0] . "'], 'unique']"; |
||
| 120 | } elseif ($attributesCount > 1) { |
||
| 121 | $labels = array_intersect_key($this->generateLabels($table), array_flip($uniqueColumns)); |
||
| 122 | $lastLabel = array_pop($labels); |
||
| 123 | $columnsList = implode("', '", $uniqueColumns); |
||
| 124 | $rules[] = "[['$columnsList'], 'unique', 'targetAttribute' => ['$columnsList'], 'message' => 'The combination of " . implode(', ', $labels) . " and $lastLabel has already been taken.']"; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } catch (NotSupportedException $e) { |
||
| 129 | // doesn't support unique indexes information...do nothing |
||
| 130 | } |
||
| 131 | // Exist rules for foreign keys |
||
| 132 | foreach ($table->foreignKeys as $refs) { |
||
| 133 | $refTable = $refs[0]; |
||
| 134 | $refTableSchema = $db->getTableSchema($refTable); |
||
| 135 | if ($refTableSchema === null) { |
||
| 136 | // Foreign key could point to non-existing table: https://github.com/yiisoft/yii2-gii/issues/34 |
||
| 137 | continue; |
||
| 138 | } |
||
| 139 | $refClassName = $this->generateClassName($refTable); |
||
| 140 | unset($refs[0]); |
||
| 141 | $attributes = implode("', '", array_keys($refs)); |
||
| 142 | $targetAttributes = []; |
||
| 143 | foreach ($refs as $key => $value) { |
||
| 144 | $targetAttributes[] = "'$key' => '$value'"; |
||
| 145 | } |
||
| 146 | $targetAttributes = implode(', ', $targetAttributes); |
||
| 147 | $rules[] = "[['$attributes'], 'exist', 'skipOnError' => true, 'targetClass' => $refClassName::className(), 'targetAttribute' => [$targetAttributes]]"; |
||
| 148 | } |
||
| 149 | return $rules; |
||
| 150 | } |
||
| 151 | |||
| 240 |