| Total Complexity | 55 |
| Total Lines | 282 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractColumnSchema 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.
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 AbstractColumnSchema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | abstract class AbstractColumnSchema implements ColumnSchemaInterface |
||
| 47 | { |
||
| 48 | public function __construct(string $name) |
||
| 49 | { |
||
| 50 | $this->name = $name; |
||
| 51 | } |
||
| 52 | |||
| 53 | private bool $allowNull = false; |
||
| 54 | private bool $autoIncrement = false; |
||
| 55 | private string|null $comment = null; |
||
| 56 | private bool $computed = false; |
||
| 57 | private string|null $dbType = null; |
||
| 58 | private mixed $defaultValue = null; |
||
| 59 | private array|null $enumValues = null; |
||
| 60 | private string|null $extra = null; |
||
| 61 | private bool $isPrimaryKey = false; |
||
| 62 | private string $name; |
||
| 63 | private string|null $phpType = null; |
||
| 64 | private int|null $precision = null; |
||
| 65 | private int|null $scale = null; |
||
| 66 | private int|null $size = null; |
||
| 67 | private string $type = ''; |
||
| 68 | private bool $unsigned = false; |
||
| 69 | |||
| 70 | public function allowNull(bool $value): void |
||
| 71 | { |
||
| 72 | $this->allowNull = $value; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function autoIncrement(bool $value): void |
||
| 76 | { |
||
| 77 | $this->autoIncrement = $value; |
||
| 78 | } |
||
| 79 | |||
| 80 | public function comment(string|null $value): void |
||
| 81 | { |
||
| 82 | $this->comment = $value; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function computed(bool $value): void |
||
| 86 | { |
||
| 87 | $this->computed = $value; |
||
| 88 | } |
||
| 89 | |||
| 90 | public function dbType(string|null $value): void |
||
| 91 | { |
||
| 92 | $this->dbType = $value; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function dbTypecast(mixed $value): mixed |
||
| 96 | { |
||
| 97 | /** |
||
| 98 | * The default implementation does the same as casting for PHP, but it should be possible to override this with |
||
| 99 | * annotation of an explicit PDO type. |
||
| 100 | */ |
||
| 101 | return $this->typecast($value); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function defaultValue(mixed $value): void |
||
| 105 | { |
||
| 106 | $this->defaultValue = $value; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function enumValues(array|null $value): void |
||
| 110 | { |
||
| 111 | $this->enumValues = $value; |
||
| 112 | } |
||
| 113 | |||
| 114 | public function extra(string|null $value): void |
||
| 115 | { |
||
| 116 | $this->extra = $value; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getComment(): string|null |
||
| 120 | { |
||
| 121 | return $this->comment; |
||
| 122 | } |
||
| 123 | |||
| 124 | public function getDbType(): string|null |
||
| 125 | { |
||
| 126 | return $this->dbType; |
||
| 127 | } |
||
| 128 | |||
| 129 | public function getDefaultValue(): mixed |
||
| 130 | { |
||
| 131 | return $this->defaultValue; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getEnumValues(): array|null |
||
| 135 | { |
||
| 136 | return $this->enumValues; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function getExtra(): string|null |
||
| 140 | { |
||
| 141 | return $this->extra; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getName(): string |
||
| 145 | { |
||
| 146 | return $this->name; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function getPrecision(): int|null |
||
| 150 | { |
||
| 151 | return $this->precision; |
||
| 152 | } |
||
| 153 | |||
| 154 | public function getPhpType(): string|null |
||
| 155 | { |
||
| 156 | return $this->phpType; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getScale(): int|null |
||
| 160 | { |
||
| 161 | return $this->scale; |
||
| 162 | } |
||
| 163 | |||
| 164 | public function getSize(): int|null |
||
| 165 | { |
||
| 166 | return $this->size; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function getType(): string |
||
| 170 | { |
||
| 171 | return $this->type; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function isAllowNull(): bool |
||
| 175 | { |
||
| 176 | return $this->allowNull; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function isAutoIncrement(): bool |
||
| 180 | { |
||
| 181 | return $this->autoIncrement; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function isComputed(): bool |
||
| 185 | { |
||
| 186 | return $this->computed; |
||
| 187 | } |
||
| 188 | |||
| 189 | public function isPrimaryKey(): bool |
||
| 190 | { |
||
| 191 | return $this->isPrimaryKey; |
||
| 192 | } |
||
| 193 | |||
| 194 | public function isUnsigned(): bool |
||
| 195 | { |
||
| 196 | return $this->unsigned; |
||
| 197 | } |
||
| 198 | |||
| 199 | public function phpType(string|null $value): void |
||
| 200 | { |
||
| 201 | $this->phpType = $value; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function phpTypecast(mixed $value): mixed |
||
| 205 | { |
||
| 206 | return $this->typecast($value); |
||
| 207 | } |
||
| 208 | |||
| 209 | public function precision(int|null $value): void |
||
| 210 | { |
||
| 211 | $this->precision = $value; |
||
| 212 | } |
||
| 213 | |||
| 214 | public function primaryKey(bool $value): void |
||
| 215 | { |
||
| 216 | $this->isPrimaryKey = $value; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function scale(int|null $value): void |
||
| 220 | { |
||
| 221 | $this->scale = $value; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function size(int|null $value): void |
||
| 225 | { |
||
| 226 | $this->size = $value; |
||
| 227 | } |
||
| 228 | |||
| 229 | public function type(string $value): void |
||
| 232 | } |
||
| 233 | |||
| 234 | public function unsigned(bool $value): void |
||
| 235 | { |
||
| 236 | $this->unsigned = $value; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Converts the input value according to {@see phpType} after retrieval from the database. |
||
| 241 | * |
||
| 242 | * If the value is null or an {@see Expression}, it won't be converted. |
||
| 243 | * |
||
| 244 | * @param mixed $value The value to be converted. |
||
| 245 | * |
||
| 246 | * @return mixed The converted value. |
||
| 247 | */ |
||
| 248 | protected function typecast(mixed $value): mixed |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return int[] Array of numbers that represent possible PDO parameter types |
||
| 318 | */ |
||
| 319 | private function getPdoParamTypes(): array |
||
| 328 | ]; |
||
| 329 | } |
||
| 330 | } |
||
| 331 |