Conditions | 15 |
Paths | 2880 |
Total Lines | 96 |
Code Lines | 60 |
Lines | 6 |
Ratio | 6.25 % |
Changes | 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 |
||
165 | public function toString() { |
||
166 | $warnings = [ ]; |
||
167 | $statements = [ ]; |
||
168 | foreach( $this->columns as $column ) { |
||
169 | $statements[] = "\t" . $column->toString($this); |
||
170 | } |
||
171 | |||
172 | if( count($this->primaryKeys) > 0 ) { |
||
173 | $primary = "\tPRIMARY KEY ("; |
||
174 | $primary .= implode(",", array_map(function ( AbstractColumn $column ) { |
||
175 | return $this->mkString($column->getName()); |
||
176 | }, $this->primaryKeys)); |
||
177 | $primary .= ")"; |
||
178 | $statements[] = $primary; |
||
179 | } |
||
180 | |||
181 | if( !is_null($this->autoIncrement) ) { |
||
182 | if( $this->autoIncrement->isSigned() ) { |
||
183 | $warnings[] = $this->mkString($this->autoIncrement->getName()) . ' is a signed AUTO_INCREMENT'; |
||
184 | } |
||
185 | if( $this->autoIncrement->isNullable() ) { |
||
186 | $warnings[] = $this->mkString($this->autoIncrement->getName()) . ' is a nullable AUTO_INCREMENT'; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | foreach( $this->keys as $keyName => $key ) { |
||
191 | $keys = "\t"; |
||
192 | if( $key['type'] != 'NORMAL' ) { |
||
193 | $keys .= $key['type'] . ' '; |
||
194 | } |
||
195 | $keys .= "KEY " . $this->mkString($keyName) . " ("; |
||
196 | $keys .= implode(",", array_map(function ( AbstractColumn $column ) { |
||
197 | return $this->mkString($column->getName()); |
||
198 | }, $key['columns'])); |
||
199 | $keys .= ")"; |
||
200 | $statements[] = $keys; |
||
201 | } |
||
202 | |||
203 | foreach( $this->foreignKeys as $fks ) { |
||
204 | /** |
||
205 | * @var $local AbstractColumn |
||
206 | * @var $remote AbstractColumn |
||
207 | */ |
||
208 | $local = $fks['local']; |
||
209 | $remote = $fks['remote']; |
||
210 | |||
211 | $tables = $remote->getTables(); |
||
212 | // @todo doesn't really need to be a PK, just a key |
||
213 | $tables = array_filter($tables, function ( Table $a ) use ( $remote ) { |
||
214 | return $a->isPrimaryKey($remote); |
||
215 | }); |
||
216 | |||
217 | foreach( $tables as $tbl ) { |
||
218 | // @todo check length and perhaps other stuff |
||
219 | if( $local->getTypeName() != $remote->getTypeName() ) { |
||
220 | $warnings[] = $this->mkString($this->autoIncrement->getName()) . ' type does not match defined foreign key type'; |
||
221 | } |
||
222 | $localName = $this->mkString($local->getName()); |
||
223 | $remoteName = $this->mkString($remote->getName()); |
||
224 | $remoteTblName = $this->mkString($tbl->getName()); |
||
225 | |||
226 | $keys = "\tFOREIGN KEY ({$localName}) REFERENCES {$remoteTblName}({$remoteName})"; |
||
227 | $statements[] = $keys; |
||
228 | } |
||
229 | } |
||
230 | |||
231 | $charset = ''; |
||
232 | $collation = ''; |
||
233 | View Code Duplication | if( $this->getCharset() ) { |
|
|
|||
234 | $charset = ' CHARACTER SET ' . $this->getCharset(); |
||
235 | if( $this->getCollation() ) { |
||
236 | $collation = ' COLLATE ' . $this->getCollation(); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | |||
241 | $comment = ''; |
||
242 | if( $this->comment ) { |
||
243 | $comment = ' COMMENT ' . $this->mkString($this->comment, "'"); |
||
244 | } |
||
245 | |||
246 | $name = $this->mkString($this->name); |
||
247 | $stmnts = implode(",\n", $statements); |
||
248 | |||
249 | $warn = ''; |
||
250 | if( count($warnings) > 0 ) { |
||
251 | $warn = "\n# Warning: " . implode("\n# Warning: ", $warnings); |
||
252 | } |
||
253 | |||
254 | return <<<EOT |
||
255 | CREATE TABLE {$name} ( |
||
256 | {$stmnts} |
||
257 | ){$charset}{$collation}{$comment}{$warn}; |
||
258 | |||
259 | EOT; |
||
260 | } |
||
261 | } |
||
262 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.