Conditions | 9 |
Paths | 5 |
Total Lines | 71 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
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 namespace Tequilarapido\Cli\Commands; |
||
126 | protected function processTable($tableName, $tableInfos, $replacements) |
||
127 | { |
||
128 | $queriesCount = 0; |
||
129 | |||
130 | // Is there columns ? |
||
131 | if (empty($tableInfos['columns'])) { |
||
132 | return; |
||
133 | } |
||
134 | |||
135 | // Iterates in each column and looks for the old string |
||
136 | foreach ($tableInfos['columns'] as $field_name) { |
||
137 | |||
138 | $this->beginTransaction(); |
||
139 | |||
140 | foreach ($replacements as $replacement) { |
||
141 | |||
142 | // Search |
||
143 | $searchQuery = $this->searchQuery($tableName, $tableInfos, $field_name, $replacement); |
||
144 | $search_results = $this->db->select($searchQuery); |
||
145 | if (empty($search_results)) { |
||
146 | continue; |
||
147 | } |
||
148 | |||
149 | // Loop through result and search/replace |
||
150 | foreach ($search_results as $found_data) { |
||
151 | |||
152 | // Pk check |
||
153 | if (isset($found_data['_id'])) { |
||
154 | $id = $found_data['_id']; |
||
155 | unset($found_data['_id']); |
||
156 | } |
||
157 | $found_data = current($found_data); |
||
158 | |||
159 | // Try to replace string |
||
160 | try { |
||
161 | $sr = new SearchReplace; |
||
162 | $edited_data = $sr->run($replacement->from, $replacement->to, $found_data); |
||
163 | } catch |
||
164 | (\Exception $e) { |
||
165 | continue; |
||
166 | } |
||
167 | |||
168 | // Update entry / value |
||
169 | if (isset($id)) { |
||
170 | $this->db |
||
171 | ->table($tableName) |
||
172 | ->where($tableInfos['pk'], '=', $id) |
||
173 | ->update(array($field_name => $edited_data)); |
||
174 | |||
175 | } else { |
||
176 | $this->db |
||
177 | ->table($tableName) |
||
178 | ->where($field_name, '=', $found_data) |
||
179 | ->update(array($field_name => $edited_data)); |
||
180 | } |
||
181 | |||
182 | $queriesCount++; |
||
183 | |||
184 | // display progress |
||
185 | ShellHelper::progress($this->output); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | // Commit updates if using transactions |
||
190 | $this->endTransaction(); |
||
191 | } |
||
192 | |||
193 | // End shell progress |
||
194 | ShellHelper::progressEnd($this->output); |
||
195 | return $queriesCount; |
||
196 | } |
||
197 | |||
223 |
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.