| Total Complexity | 46 |
| Total Lines | 179 |
| 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 |
||
| 11 | abstract class LaragenType |
||
| 12 | { |
||
| 13 | protected $unique; |
||
| 14 | protected $required; |
||
| 15 | protected $isDisplay; |
||
| 16 | protected $dataType; |
||
| 17 | protected $formType; |
||
| 18 | protected $stubs = []; |
||
| 19 | protected $size = false; |
||
| 20 | protected $validationRule = null; |
||
| 21 | protected $moduleName; |
||
| 22 | protected $columnName; |
||
| 23 | protected $optionString; |
||
| 24 | protected $optionArray; |
||
| 25 | protected $typeOption; |
||
| 26 | |||
| 27 | public function __construct($moduleName, $columnName, $optionString) |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | protected function __call($method, $params) { |
||
| 61 | } |
||
| 62 | |||
| 63 | public function getSchema() |
||
| 64 | { |
||
| 65 | $schema = '$table->'.$this->getDataType()."('{$this->getColumn()}'"; |
||
| 66 | $schema .= $this->getSize() ? ", {$this->getSize()})" : ")"; |
||
| 67 | $schema .= $this->isUnique() ? "->unique()" : ""; |
||
| 68 | $schema .= $this->isRequired() ? "" : "->nullable()"; |
||
| 69 | $schema .= ";"; |
||
| 70 | |||
| 71 | return $schema; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getFilteredColumns($options = [], $columnsOnly = false) |
||
| 75 | { |
||
| 76 | $filteredTypes = []; |
||
| 77 | $options = is_array($options) ? $options : [$options]; |
||
| 78 | foreach($this->getPivotColumns() as $type){ |
||
| 79 | foreach ($options as $option) { |
||
| 80 | if($type->$option()){ |
||
| 81 | $filteredTypes[] = $columnsOnly ? $type->getColumn() : $type; |
||
| 82 | break; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | return $filteredTypes; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function getFormOptions() { |
||
| 90 | $options = ""; |
||
| 91 | $options .= $this->isRequired() ? 'required="required" ' : ''; |
||
| 92 | return $options; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function getForeignKey() |
||
| 96 | { |
||
| 97 | return $this->columnName . "_id"; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function getRelatedModel() |
||
| 101 | { |
||
| 102 | return $this->getChildModel(); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function getRelatedModule() |
||
| 106 | { |
||
| 107 | return Str::plural(strtolower(Str::snake($this->getRelatedModel()))); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function getRelatedModelLowercase() |
||
| 111 | { |
||
| 112 | return strtolower($this->getRelatedModel()); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function getChildModel() |
||
| 118 | } |
||
| 119 | |||
| 120 | public function getParentModel() |
||
| 121 | { |
||
| 122 | return ucfirst(Str::camel(Str::singular($this->moduleName))); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function getParentModule() |
||
| 126 | { |
||
| 127 | return $this->moduleName; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getParentModelLowercase() |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getStub($type) |
||
| 136 | { |
||
| 137 | return isset($this->stubs[$type]) ? $this->stubs[$type] : false; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function getTextRows() { |
||
| 141 | if (!$this->size) { |
||
| 142 | return 4; |
||
| 143 | } |
||
| 144 | |||
| 145 | return floor($this->getsize() / 120); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function isUnique() { |
||
| 149 | return $this->unique; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function isRequired() { |
||
| 153 | return $this->required; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getDisplay() |
||
| 159 | } |
||
| 160 | |||
| 161 | public function getColumn() |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getColumnKey() |
||
| 167 | { |
||
| 168 | return $this->columnName; |
||
| 169 | } |
||
| 170 | |||
| 171 | protected function setOptions($optionType, $optionParam) { |
||
| 172 | switch ($optionType) { |
||
| 173 | case 'max': |
||
| 174 | $this->setSize($optionParam); |
||
| 175 | break; |
||
| 176 | |||
| 177 | default: |
||
| 178 | $this->$optionType = $optionParam; |
||
| 179 | break; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getTabs($number) |
||
| 190 | } |
||
| 191 | } |
||
| 192 |