| Total Complexity | 44 |
| Total Lines | 236 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Complex classes like LaragenType 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 LaragenType, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class LaragenType |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Denotes if the column is unique. |
||
| 23 | * |
||
| 24 | * @var bool |
||
| 25 | */ |
||
| 26 | protected $isUnique; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Denotes if the column is required. |
||
| 30 | * |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | protected $isRequired; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Denotes if the column is used as title. Can be in case of select or checkboxes. |
||
| 37 | * |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | protected $isDisplay; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The data type of column used in the database. |
||
| 44 | * Stores name of a column creation method of \Illuminate\Database\Schema\Blueprint |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $dataType; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The stub to be used in form generation of column. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $formType; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Defines additional stubs to be used for |
||
| 59 | * Defines stubs for relational types to be used in models generation |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $stubs = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The size of column in the database. |
||
| 67 | * |
||
| 68 | * @var int |
||
| 69 | */ |
||
| 70 | protected $size = 0; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Validation rule for the column to be used in Request file. |
||
| 74 | * |
||
| 75 | * @var string|null |
||
| 76 | */ |
||
| 77 | protected $validationRule = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Name of the module the column belongs to. |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $moduleName; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Name of the column. |
||
| 88 | * e.g for 'short_description' => 'string|max:512', 'short_description' is the columnName. |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $columnName; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Special type assigned to the column e.g parent, related |
||
| 96 | * |
||
| 97 | * @var string|null |
||
| 98 | */ |
||
| 99 | protected $typeOption; |
||
| 100 | |||
| 101 | public function __construct($moduleName, $columnName, $optionString) |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public function __call($method, $params) { |
||
| 123 | $var = lcfirst(substr($method, 3)); |
||
| 124 | |||
| 125 | if (strncasecmp($method, "get", 3) === 0) { |
||
| 126 | return property_exists($this, $var) ? $this->$var : ""; |
||
| 127 | } |
||
| 128 | |||
| 129 | if (strncasecmp($method, "set", 3) === 0 && isset($params[0])) { |
||
| 130 | $this->$var = $params[0]; |
||
| 131 | } |
||
| 132 | |||
| 133 | return property_exists($this, $method) ? $this->$method : ""; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getSchema() |
||
| 137 | { |
||
| 138 | $schema = '$table->'.$this->getDataType()."('{$this->getColumn()}'"; |
||
| 139 | $schema .= $this->getSize() ? ", {$this->getSize()})" : ")"; |
||
| 140 | $schema .= $this->isUnique() ? "->unique()" : ""; |
||
| 141 | $schema .= $this->isRequired() ? "" : "->nullable()"; |
||
| 142 | $schema .= ";"; |
||
| 143 | |||
| 144 | return $schema; |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getFilteredColumns($options = [], $columnsOnly = false) |
||
| 148 | { |
||
| 149 | $filteredTypes = []; |
||
| 150 | $options = is_array($options) ? $options : [$options]; |
||
| 151 | foreach($this->getPivotColumns() as $type){ |
||
| 152 | foreach ($options as $option) { |
||
| 153 | if($type->$option()){ |
||
| 154 | $filteredTypes[] = $columnsOnly ? $type->getColumn() : $type; |
||
| 155 | break; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | return $filteredTypes; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function getFormOptions() { |
||
| 163 | $options = ""; |
||
| 164 | $options .= $this->isRequired() ? 'required="required" ' : ''; |
||
| 165 | return $options; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getForeignKey() |
||
| 169 | { |
||
| 170 | return $this->columnName . "_id"; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function getRelatedModel() |
||
| 174 | { |
||
| 175 | return $this->getChildModel(); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function getRelatedModule() |
||
| 179 | { |
||
| 180 | return Str::plural(strtolower(Str::snake($this->getRelatedModel()))); |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getRelatedModelLowercase() |
||
| 184 | { |
||
| 185 | return strtolower($this->getRelatedModel()); |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getChildModel() |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getParentModel() |
||
| 194 | { |
||
| 195 | return ucfirst(Str::camel(Str::singular($this->moduleName))); |
||
| 196 | } |
||
| 197 | |||
| 198 | public function getParentModule() |
||
| 199 | { |
||
| 200 | return $this->moduleName; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function getParentModelLowercase() |
||
| 206 | } |
||
| 207 | |||
| 208 | public function getStub($type) |
||
| 209 | { |
||
| 210 | return isset($this->stubs[$type]) ? $this->stubs[$type] : false; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getTextRows() { |
||
| 214 | if (!$this->size) { |
||
| 215 | return 4; |
||
| 216 | } |
||
| 217 | |||
| 218 | return floor($this->getsize() / 120); |
||
| 219 | } |
||
| 220 | |||
| 221 | public function getDisplay() |
||
| 224 | } |
||
| 225 | |||
| 226 | public function getColumn() |
||
| 229 | } |
||
| 230 | |||
| 231 | public function getColumnKey() |
||
| 232 | { |
||
| 233 | return $this->columnName; |
||
| 234 | } |
||
| 235 | |||
| 236 | protected function setOptions($optionType, $optionParam) { |
||
| 237 | switch ($optionType) { |
||
| 238 | case 'max': |
||
| 239 | $this->setSize($optionParam); |
||
| 240 | break; |
||
| 241 | |||
| 242 | default: |
||
| 243 | $this->$optionType = $optionParam; |
||
| 244 | break; |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | public function getTabs($number) |
||
| 255 | } |
||
| 256 | } |
||
| 257 |