| Total Complexity | 61 |
| Total Lines | 229 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 7 | abstract class LaragenType |
||
| 8 | {
|
||
| 9 | protected $uniqueFlag; |
||
| 10 | protected $requiredFlag; |
||
| 11 | protected $isDisplay; |
||
| 12 | protected $dataType; |
||
| 13 | protected $formType; |
||
| 14 | protected $stubs = []; |
||
| 15 | protected $size = false; |
||
| 16 | protected $validationRule = null; |
||
| 17 | protected $moduleName; |
||
| 18 | protected $columnName; |
||
| 19 | protected $optionString; |
||
| 20 | |||
| 21 | public function __construct($moduleName, $columnName, $optionString) |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | function __call($method, $params) {
|
||
| 44 | $var = lcfirst(substr($method, 3)); |
||
| 45 | |||
| 46 | if (strncasecmp($method, "get", 3) === 0) {
|
||
| 47 | return property_exists($this, $var) ? $this->$var : ""; |
||
| 48 | } |
||
| 49 | |||
| 50 | if (strncasecmp($method, "set", 3) === 0 && isset($params[0])) {
|
||
| 51 | $this->$var = $params[0]; |
||
| 52 | } |
||
| 53 | |||
| 54 | return property_exists($this, $method) ? $this->$method : ""; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function isRelational() |
||
| 58 | {
|
||
| 59 | return $this->relationalType; |
||
| 60 | } |
||
| 61 | |||
| 62 | public function getSchema() |
||
| 63 | {
|
||
| 64 | $schema = '$table->'.$this->getDataType()."('{$this->getColumn()}'";
|
||
| 65 | $schema .= $this->getSize() ? ", {$this->getSize()})" : ")";
|
||
| 66 | $schema .= $this->isUnique() ? "->unique()" : ""; |
||
| 67 | $schema .= $this->isRequired() ? "" : "->nullable()"; |
||
| 68 | $schema .= ";"; |
||
| 69 | |||
| 70 | return $schema; |
||
| 71 | } |
||
| 72 | |||
| 73 | public function getValidationLine() |
||
| 74 | {
|
||
| 75 | $validationSegments = []; |
||
| 76 | $modelname = strtolower(Str::camel(str_singular($this->moduleName))); |
||
| 77 | |||
| 78 | $validationSegments[] = $this->isRequired() ? 'required' : 'nullable'; |
||
| 79 | $validationSegments[] = $this->getValidationRule() ?? $this->getDataType(); |
||
| 80 | $rules = implode('|', $validationSegments);
|
||
| 81 | |||
| 82 | if ($this->isUnique()) {
|
||
| 83 | $validationLine = '($this->'.$modelname.') ? \''; |
||
| 84 | $validationLine .= $rules . '|unique:'.$this->moduleName.','.$this->getColumn().','.'\''.'.$this->'.$modelname.'->id : \''; |
||
| 85 | $validationLine .= $rules . '|unique:'.$this->moduleName.'\''; |
||
| 86 | } else{
|
||
| 87 | $validationLine = "'{$rules}'";
|
||
| 88 | } |
||
| 89 | return $validationLine; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function getFormOptions() {
|
||
| 93 | $options = ""; |
||
| 94 | $options .= $this->isRequired() ? 'required="required" ' : ''; |
||
| 95 | return $options; |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | public function getForeignKey() |
||
| 100 | {
|
||
| 101 | return $this->columnName . "_id"; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function getFilteredColumns($options = [], $columnsOnly = false) |
||
| 105 | {
|
||
| 106 | $filteredTypes = []; |
||
| 107 | $options = is_array($options) ? $options : [$options]; |
||
| 108 | foreach($this->getPivotColumns() as $type){
|
||
| 109 | foreach ($options as $option) {
|
||
| 110 | if($type->$option()){
|
||
| 111 | $filteredTypes[] = $columnsOnly ? $type->getColumn() : $type; |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | return $filteredTypes; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getRelatedModel() |
||
| 120 | {
|
||
| 121 | return $this->getChildModel(); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function getRelatedModule() |
||
| 125 | {
|
||
| 126 | return Str::snake(Str::plural($this->getRelatedModel())); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function getRelatedModelLowercase() |
||
| 130 | {
|
||
| 131 | return strtolower($this->getRelatedModel()); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getChildModel() |
||
| 137 | } |
||
| 138 | |||
| 139 | public function getParentModel() |
||
| 140 | {
|
||
| 141 | return ucfirst(Str::camel(Str::singular($this->moduleName))); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getParentModule() |
||
| 145 | {
|
||
| 146 | return $this->moduleName; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function getParentModelLowercase() |
||
| 150 | {
|
||
| 151 | return Str::singular($this->moduleName); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function getStub($type) |
||
| 155 | {
|
||
| 156 | return isset($this->stubs[$type]) ? $this->stubs[$type] : false; |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | public function getTextRows() {
|
||
| 161 | if (!$this->size) |
||
| 162 | return 4; |
||
| 163 | |||
| 164 | return floor($this->getsize() / 120); |
||
| 165 | } |
||
| 166 | public function isUnique() {
|
||
| 167 | return $this->uniqueFlag; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function isRequired() {
|
||
| 171 | return $this->requiredFlag; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function optionArray() {
|
||
| 175 | return $this->optionArray; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function getDisplay() |
||
| 179 | {
|
||
| 180 | return Str::title(str_replace("_", " ", $this->columnName));
|
||
| 181 | } |
||
| 182 | |||
| 183 | public function getColumn() |
||
| 184 | {
|
||
| 185 | return $this->columnName; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getColumnKey() |
||
| 189 | {
|
||
| 190 | return $this->columnName; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getDataType() {
|
||
| 195 | } |
||
| 196 | |||
| 197 | public function getValidationRule() {
|
||
| 198 | return $this->validationRule; |
||
| 199 | } |
||
| 200 | |||
| 201 | protected function setUnique($set = true) {
|
||
| 202 | $this->uniqueFlag = ($set === true) ? true : false; |
||
| 203 | } |
||
| 204 | |||
| 205 | protected function setRequired($set = true) {
|
||
| 206 | $this->requiredFlag = ($set === true) ? true : false; |
||
| 207 | } |
||
| 208 | |||
| 209 | protected function setIsDisplay($set = true) {
|
||
| 210 | $this->isDisplay = ($set === true) ? true : false; |
||
| 211 | } |
||
| 212 | |||
| 213 | protected function setSize($size = null) {
|
||
| 215 | } |
||
| 216 | |||
| 217 | protected function setOptions($optionType, $optionParam) {
|
||
| 218 | switch ($optionType) {
|
||
| 219 | case 'max': |
||
| 220 | $this->setSize($optionParam); |
||
| 221 | break; |
||
| 222 | |||
| 223 | default: |
||
| 224 | $this->$optionType = $optionParam; |
||
| 225 | break; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | public function getTabs($number) |
||
| 236 | } |
||
| 237 | } |
||
| 238 |