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