Conditions | 32 |
Paths | 144 |
Total Lines | 145 |
Code Lines | 102 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
136 | protected function fixClassNames(string $tableName, string $objectClassName, ?string $fieldName = 'ClassName', ?bool $versionedTable = false) |
||
137 | { |
||
138 | $this->flushNow('... CHECKING ' . $tableName . '.' . $fieldName . ' ...'); |
||
139 | $count = DB::query('SELECT COUNT("ID") FROM "' . $tableName . '"')->value(); |
||
140 | $where = '"' . $fieldName . '" NOT IN (\'' . implode("', '", array_keys($this->listOfAllClasses)) . "')"; |
||
141 | $whereA = $where . ' AND ' . '(' . '"' . $fieldName . '" IS NULL OR "' . $fieldName . '" = \'\' )'; |
||
142 | $whereB = $where . ' AND NOT ' . '(' . '"' . $fieldName . '" IS NULL OR "' . $fieldName . '" = \'\' )'; |
||
143 | $rowsToFix = DB::query('SELECT COUNT("ID") FROM "' . $tableName . '" WHERE ' . $where)->value(); |
||
144 | $rowsToFixA = DB::query('SELECT COUNT("ID") FROM "' . $tableName . '" WHERE ' . $whereA)->value(); |
||
145 | $rowsToFixB = DB::query('SELECT COUNT("ID") FROM "' . $tableName . '" WHERE ' . $whereB)->value(); |
||
146 | if ($rowsToFix > 0) { |
||
147 | if ($count === $rowsToFix) { |
||
148 | $this->flushNow('... All rows ' . $count . ' in table ' . $tableName . ' are broken: ', 'error'); |
||
149 | } else { |
||
150 | $this->flushNow('... ' . $rowsToFix . ' errors in "' . $fieldName . '" values:'); |
||
151 | if ($rowsToFixA) { |
||
152 | $this->flushNow('... ... ' . $rowsToFixA . ' in table ' . $tableName . ' do not have a ' . $fieldName . ' at all and ', 'error'); |
||
153 | } |
||
154 | if ($rowsToFixB) { |
||
155 | $this->flushNow('... ... ' . $rowsToFixB . ' in table ' . $tableName . ' have a bad ' . $fieldName . ''); |
||
156 | } |
||
157 | } |
||
158 | if ($this->fixErrors) { |
||
159 | if ($this->extendFieldSize) { |
||
160 | $this->fixFieldSize($tableName); |
||
161 | } |
||
162 | //work out if we can set it to the long form of a short ClassName |
||
163 | $rows = DB::query( |
||
164 | ' |
||
165 | SELECT "' . $fieldName . '", COUNT("ID") AS C |
||
166 | FROM "' . $tableName . '" |
||
167 | GROUP BY "' . $fieldName . '" |
||
168 | HAVING ' . $where . ' |
||
169 | ORDER BY C DESC' |
||
170 | ); |
||
171 | foreach ($rows as $row) { |
||
172 | if (! $row[$fieldName]) { |
||
173 | $row[$fieldName] = '--- NO VALUE ---'; |
||
174 | } |
||
175 | $this->flushNow('... ... ' . $row['C'] . ' ' . $row[$fieldName]); |
||
176 | if (isset($this->countsOfAllClasses[$row[$fieldName]])) { |
||
177 | if (1 === $this->countsOfAllClasses[$row[$fieldName]]) { |
||
178 | $longNameAlreadySlashed = array_search($row[$fieldName], $this->listOfAllClasses, true); |
||
179 | if ($longNameAlreadySlashed) { |
||
180 | $this->flushNow('... ... ... Updating ' . $row[$fieldName] . ' to ' . $longNameAlreadySlashed . ' - based in short to long mapping of the ' . $fieldName . ' field. ', 'created'); |
||
181 | if ($this->forReal) { |
||
182 | $this->runUpdateQuery( |
||
183 | ' |
||
184 | UPDATE "' . $tableName . '" |
||
185 | SET "' . $tableName . '"."' . $fieldName . '" = \'' . $longNameAlreadySlashed . '\' |
||
186 | WHERE "' . $fieldName . '" = \'' . $row[$fieldName] . "'", |
||
187 | 2 |
||
188 | ); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | //only try to work out what is going on when it is a ClassName Field! |
||
196 | if ('ClassName' === $fieldName) { |
||
197 | $options = ClassInfo::subclassesFor($objectClassName); |
||
198 | $checkTables = []; |
||
199 | foreach ($options as $key => $optionClassName) { |
||
200 | if ($optionClassName !== $objectClassName) { |
||
201 | $optionTableName = $this->dataObjectSchema->tableName($objectClassName); |
||
202 | if (! $this->tableExists($optionTableName) || $optionTableName === $tableName) { |
||
203 | unset($options[$key]); |
||
204 | } else { |
||
205 | $checkTables[$optionClassName] = $optionTableName; |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | //fix bad rows.... |
||
210 | $rows = DB::query('SELECT "ID", "' . $fieldName . '" FROM "' . $tableName . '" WHERE ' . $where); |
||
211 | foreach ($rows as $row) { |
||
212 | //check if it is the short name ... |
||
213 | $optionCount = 0; |
||
214 | $matchedClassName = ''; |
||
215 | foreach ($checkTables as $optionClassName => $optionTableName) { |
||
216 | $hasMatch = DB::query(' |
||
217 | SELECT COUNT("' . $tableName . '"."ID") |
||
218 | FROM "' . $tableName . '" |
||
219 | INNER JOIN "' . $optionTableName . '" |
||
220 | ON "' . $optionTableName . '"."ID" = "' . $tableName . '"."ID" |
||
221 | WHERE "' . $tableName . '"."ID" = ' . $row['ID'])->value(); |
||
222 | if (1 === $hasMatch) { |
||
223 | ++$optionCount; |
||
224 | $matchedClassName = $optionClassName; |
||
225 | if ($optionCount > 1) { |
||
226 | break; |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | if (0 === $optionCount) { |
||
231 | if (! $row[$fieldName]) { |
||
232 | $row[$fieldName] = '--- NO VALUE ---'; |
||
233 | } |
||
234 | $this->flushNow('... Updating ' . $fieldName . ' to ' . $objectClassName . ' for ID = ' . $row['ID'] . ' from ' . $fieldName . ' = ' . $row[$fieldName] . ' - based on inability to find matching IDs in any child class tables', 'created'); |
||
235 | if ($this->forReal) { |
||
236 | $this->runUpdateQuery( |
||
237 | ' |
||
238 | UPDATE "' . $tableName . '" |
||
239 | SET "' . $tableName . '"."' . $fieldName . '" = \'' . addslashes($objectClassName) . '\' |
||
240 | WHERE ID = ' . $row['ID'], |
||
241 | 2 |
||
242 | ); |
||
243 | } |
||
244 | } elseif (1 === $optionCount && $matchedClassName) { |
||
245 | $this->flushNow('... Updating ' . $fieldName . ' to ' . $matchedClassName . ' ID = ' . $row['ID'] . ', ' . $fieldName . ' = ' . $row[$fieldName] . ' - based on matching row in exactly one child class table', 'created'); |
||
246 | if ($this->forReal) { |
||
247 | $this->runUpdateQuery( |
||
248 | 'UPDATE "' . $tableName . '" |
||
249 | SET "' . $tableName . '"."' . $fieldName . '" = \'' . addslashes($matchedClassName) . '\' |
||
250 | WHERE ID = ' . $row['ID'], |
||
251 | 2 |
||
252 | ); |
||
253 | } |
||
254 | } else { |
||
255 | $bestValue = $this->bestClassName($objectClassName, $tableName, $fieldName); |
||
256 | $this->flushNow('... ERROR: can not find best ' . $fieldName . ' for ' . $tableName . '.ID = ' . $row['ID'] . ' current value: ' . $row[$fieldName] . ' we recommend: ' . $bestValue, 'error'); |
||
257 | $this->runUpdateQuery( |
||
258 | 'UPDATE "' . $tableName . '" |
||
259 | SET "' . $tableName . '"."' . $fieldName . '" = \'' . addslashes($bestValue) . '\' |
||
260 | WHERE ID = ' . $row['ID'], |
||
261 | 2 |
||
262 | ); |
||
263 | } |
||
264 | } |
||
265 | } else { |
||
266 | $this->flushNow('... Updating "' . $tableName . '"."' . $fieldName . '" TO NULL WHERE ' . $where, 'created'); |
||
267 | if ($this->forReal) { |
||
268 | $this->runUpdateQuery('UPDATE "' . $tableName . '" SET "' . $fieldName . '" = \'\' WHERE ' . $where, 2); |
||
269 | } |
||
270 | } |
||
271 | } |
||
272 | } |
||
273 | //run again with versioned tables ... |
||
274 | if (false === $versionedTable) { |
||
275 | foreach (['_Live', '_Versions'] as $extension) { |
||
276 | $testTable = $tableName . $extension; |
||
277 | if ($this->tableExists($testTable)) { |
||
278 | $this->fixClassNames($testTable, $objectClassName, $fieldName, true); |
||
279 | } else { |
||
280 | $this->flushNow('... ... there is no table called: ' . $testTable); |
||
281 | } |
||
334 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths