Conditions | 16 |
Paths | 74 |
Total Lines | 56 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
102 | public function fromDbFieldDescription(DbFieldDescription $fieldDescription) { |
||
103 | $this->field = $fieldDescription->Field; |
||
104 | $this->setName($fieldDescription->Field); |
||
105 | |||
106 | $nonMandatory = false; |
||
107 | |||
108 | switch (true) { |
||
109 | case strpos($fieldDescription->Type, 'int') === 0: |
||
110 | case strpos($fieldDescription->Type, 'tinyint') === 0: |
||
111 | $this->type = 'integer'; |
||
112 | $this->default = !empty($fieldDescription->Default) ? intval($fieldDescription->Default) : 0; |
||
113 | $this->toProperty = [self::class, 'toInteger']; |
||
114 | $this->fromUser = [self::class, 'intFromUser']; |
||
115 | break; |
||
116 | |||
117 | /** @noinspection PhpMissingBreakStatementInspection */ |
||
118 | case strpos($fieldDescription->Type, 'mediumtext') === 0: |
||
119 | $nonMandatory = true; |
||
|
|||
120 | case strpos($fieldDescription->Type, 'varchar') === 0: |
||
121 | $this->type = 'string'; |
||
122 | $this->default = !empty($fieldDescription->Default) || $fieldDescription->Default === null |
||
123 | ? $fieldDescription->Default : ''; |
||
124 | break; |
||
125 | |||
126 | case strpos($fieldDescription->Type, 'datetime') === 0: |
||
127 | $this->type = 'datetime'; |
||
128 | // $this->default = !empty($fieldDescription->Default) || $fieldDescription->Default === null |
||
129 | // ? $fieldDescription->Default : '0000-00-00 00:00:00'; |
||
130 | // TODO - current timestamp ???? |
||
131 | if ($fieldDescription->Default === null) { |
||
132 | $this->default = null; |
||
133 | } elseif (!empty($fieldDescription->Default)) { |
||
134 | $this->default = strtotime($fieldDescription->Default); |
||
135 | } else { |
||
136 | $this->default = 0; |
||
137 | } |
||
138 | $this->toProperty = [self::class, 'toUnixTime']; |
||
139 | $this->fromProperty = [self::class, 'fromUnixTime']; |
||
140 | $this->fromUser = [self::class, 'datetimeFromUser']; |
||
141 | // $this->toPtl = [self::class, 'ptlUnixTime']; |
||
142 | break; |
||
143 | |||
144 | default: |
||
145 | die("Unknown field type '{$fieldDescription->Type}' in " . get_called_class()); |
||
146 | break; |
||
147 | } |
||
148 | |||
149 | // Main index is non mandatory |
||
150 | if ($fieldDescription->Extra === 'auto_increment' && $fieldDescription->Key === 'PRI') { |
||
151 | $nonMandatory = true; |
||
152 | } |
||
153 | |||
154 | |||
155 | // If this field is not null and default value is not set - then this field is mandatory |
||
156 | if ($fieldDescription->Null === 'NO' && $fieldDescription->Default === null && !$nonMandatory) { |
||
157 | $this->mandatory = true; |
||
158 | } |
||
228 |