Conditions | 25 |
Paths | > 20000 |
Total Lines | 112 |
Code Lines | 66 |
Lines | 32 |
Ratio | 28.57 % |
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 |
||
11 | public function createTable(Table $table, array $columns = [], array $indexes = []) |
||
12 | { |
||
13 | $options = $table->getOptions(); |
||
14 | $parts = $this->getSchemaName($table->getName()); |
||
15 | |||
16 | // Add the default primary key |
||
17 | if (!isset($options['id']) || ( isset($options['id']) && $options['id'] === true )) { |
||
18 | $column = new Column(); |
||
19 | $column->setName('id') |
||
20 | ->setType('integer') |
||
21 | ->setIdentity(true); |
||
22 | |||
23 | array_unshift($columns, $column); |
||
24 | $options['primary_key'] = 'id'; |
||
25 | } elseif (isset($options['id']) && |
||
26 | is_string($options['id']) ) {// Handle id => "field_name" to support AUTO_INCREMENT |
||
27 | $column = new Column(); |
||
28 | $column->setName($options['id']) |
||
29 | ->setType('integer') |
||
30 | ->setIdentity(true); |
||
31 | |||
32 | array_unshift($columns, $column); |
||
33 | $options['primary_key'] = $options['id']; |
||
34 | } |
||
35 | |||
36 | // TODO - process table options like collation etc |
||
37 | $sql = 'CREATE TABLE '; |
||
38 | $sql .= $this->quoteTableName($table->getName()) . ' ('; |
||
39 | |||
40 | $this->columnsWithComments = []; |
||
41 | View Code Duplication | foreach ($columns as $column) { |
|
|
|||
42 | $sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . |
||
43 | ', '; |
||
44 | |||
45 | // set column comments, if needed |
||
46 | if ($column->getComment()) { |
||
47 | $this->columnsWithComments[] = $column; |
||
48 | } |
||
49 | } |
||
50 | |||
51 | // set the primary key(s) |
||
52 | View Code Duplication | if (isset($options['primary_key'])) { |
|
53 | $sql = rtrim($sql); |
||
54 | $sql .= sprintf(' CONSTRAINT %s PRIMARY KEY (', $this->quoteColumnName($parts['table'] . '_pkey')); |
||
55 | if (is_string($options['primary_key'])) { // handle primary_key => 'id' |
||
56 | $sql .= $this->quoteColumnName($options['primary_key']); |
||
57 | } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') |
||
58 | $sql .= implode(',', array_map([ $this, 'quoteColumnName' ], $options['primary_key'])); |
||
59 | } |
||
60 | $sql .= ')'; |
||
61 | } else { |
||
62 | $sql = rtrim($sql, ', '); // no primary keys |
||
63 | } |
||
64 | $sql .= ') '; |
||
65 | |||
66 | // process redshift sortkeys & distkey |
||
67 | $sortKeys = isset($options['sortkeys']) ? $options['sortkeys'] : null; |
||
68 | |||
69 | $distKey = isset($options['distkey']) ? $options['distkey'] : null; |
||
70 | |||
71 | $distStyle = isset($options['diststyle']) ? $options['diststyle'] : null; |
||
72 | |||
73 | $interleavedSortKey = isset($options['interleaved']) ? (bool)$options['interleaved'] : false; |
||
74 | |||
75 | if (!empty($distKey)) { |
||
76 | $sql .= ' distkey(' . addslashes($distKey) . ')'; |
||
77 | } |
||
78 | |||
79 | if (!empty($sortKeys)) { |
||
80 | $sortKeyStr = is_array($sortKeys) ? addslashes(implode(',', $sortKeys)) : addslashes($sortKeys); |
||
81 | |||
82 | if (!$interleavedSortKey) { |
||
83 | $sql .= sprintf(' compound sortkey (%s) ', $sortKeyStr); |
||
84 | } else { |
||
85 | $sql .= sprintf(' interleaved sortkey (%s) ', $sortKeyStr); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | if (!empty($distStyle)) { |
||
90 | $sql .= sprintf(' diststyle %s', $distStyle); |
||
91 | } |
||
92 | |||
93 | $sql .= ';'; |
||
94 | |||
95 | // process column comments |
||
96 | View Code Duplication | if (!empty($this->columnsWithComments)) { |
|
97 | foreach ($this->columnsWithComments as $column) { |
||
98 | $sql .= $this->getColumnCommentSqlDefinition($column, $table->getName()); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | // set the indexes |
||
103 | if (!empty($indexes)) { |
||
104 | foreach ($indexes as $index) { |
||
105 | $sql .= $this->getIndexSqlDefinition($index, $table->getName()); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | // execute the sql |
||
110 | $this->execute($sql); |
||
111 | |||
112 | // process table comments |
||
113 | View Code Duplication | if (isset($options['comment'])) { |
|
114 | $sql = sprintf( |
||
115 | 'COMMENT ON TABLE %s IS %s', |
||
116 | $this->quoteTableName($table->getName()), |
||
117 | $this->getConnection() |
||
118 | ->quote($options['comment']) |
||
119 | ); |
||
120 | $this->execute($sql); |
||
121 | } |
||
122 | } |
||
123 | |||
155 |
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.