| Total Complexity | 47 |
| Total Lines | 246 |
| Duplicated Lines | 0 % |
| Changes | 21 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HelperMethods often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HelperMethods, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | trait HelperMethods |
||
| 14 | { |
||
| 15 | use FlushNow; |
||
| 16 | |||
| 17 | public function deleteObject($obj) |
||
| 18 | { |
||
| 19 | if ($obj->exists()) { |
||
| 20 | FlushNowImplementor::do_flush('DELETING ' . $obj->ClassName . '.' . $obj->ID, 'deleted'); |
||
| 21 | if ($obj->hasExtension(Versioned::class)) { |
||
| 22 | $obj->DeleteFromStage(Versioned::LIVE); |
||
| 23 | $obj->DeleteFromStage(Versioned::DRAFT); |
||
| 24 | } else { |
||
| 25 | $obj->delete(); |
||
| 26 | } |
||
| 27 | @$obj->flushCache(); |
||
| 28 | } else { |
||
| 29 | FlushNowImplementor::do_flush('DOES NOT EXIST', 'added'); |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $queries list of queries |
||
| 35 | * @param string $name what is this list about? |
||
| 36 | */ |
||
| 37 | protected function runSQLQueries($queries, $name = 'UPDATE QUERIES') |
||
| 38 | { |
||
| 39 | if ([] !== $queries) { |
||
| 40 | $this->flushNow('<h3>Performing ' . $name . ' Queries</h3>'); |
||
| 41 | foreach ($queries as $sqlQuery) { |
||
| 42 | $this->runUpdateQuery($sqlQuery); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $sqlQuery list of queries |
||
| 49 | * @param int $indents what is this list about? |
||
| 50 | */ |
||
| 51 | protected function runUpdateQuery(string $sqlQuery, ?int $indents = 1) |
||
| 52 | { |
||
| 53 | $this->flushNow(str_replace('"', '`', $sqlQuery), 'created'); |
||
| 54 | $prefix = str_repeat(' ... ', $indents); |
||
| 55 | |||
| 56 | try { |
||
| 57 | DB::query($sqlQuery); |
||
| 58 | $this->flushNow($prefix . ' DONE ' . DB::affected_rows() . ' rows affected'); |
||
| 59 | } catch (Exception $exception) { |
||
| 60 | $this->flushNow($prefix . "ERROR: Unable to run '{$sqlQuery}'", 'deleted'); |
||
| 61 | $this->flushNow('' . $exception->getMessage() . '', 'deleted'); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param array $publishClasses list of class names to write / publish |
||
| 67 | */ |
||
| 68 | protected function runPublishClasses(array $publishClasses) |
||
| 69 | { |
||
| 70 | if ([] !== $publishClasses) { |
||
| 71 | $this->flushNow('<h3>Publish classes</h3>'); |
||
| 72 | foreach ($publishClasses as $publishClass) { |
||
| 73 | $this->flushNow('<h6>Publishing ' . $publishClass . '</h6>'); |
||
| 74 | |||
| 75 | try { |
||
| 76 | $count = 0; |
||
| 77 | $publishItems = $publishClass::get(); |
||
| 78 | foreach ($publishItems as $publishItem) { |
||
| 79 | ++$count; |
||
| 80 | $this->flushNow( |
||
| 81 | 'Publishing ' . $count . ' of ' . $publishItems->count() . |
||
| 82 | ' ' . |
||
| 83 | $publishClass . |
||
| 84 | ' item' . (1 === $publishItems->exists() ? '' : 's') . '.' |
||
| 85 | ); |
||
| 86 | $publishItem->write(); |
||
| 87 | if ($publishItem->hasMethod('publishRecursive')) { |
||
| 88 | $publishItem->publishRecursive(); |
||
| 89 | $this->flushNow('... DONE - PUBLISHED'); |
||
| 90 | } else { |
||
| 91 | $this->flushNow('... DONE - WRITE ONLY'); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } catch (Exception $e) { |
||
| 95 | $this->flushNow('Unable to publish ' . $publishClass . '', 'error'); |
||
| 96 | $this->flushNow('' . $e->getMessage() . '', 'error'); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | protected function makeTableObsolete(string $tableName, ?bool $doEvenIfAlreadyObsolete = false): bool |
||
| 103 | { |
||
| 104 | $schema = $this->getSchema(); |
||
| 105 | if ($this->tableExists($tableName)) { |
||
| 106 | if (!$this->tableExists('_obsolete_' . $tableName) || $doEvenIfAlreadyObsolete) { |
||
| 107 | $schema->dontRequireTable($tableName); |
||
| 108 | |||
| 109 | return true; |
||
| 110 | } |
||
| 111 | $this->flushNow('Table ' . $tableName . ' is already obsolete'); |
||
| 112 | } else { |
||
| 113 | $this->flushNow('Table ' . $tableName . ' does not exist.'); |
||
| 114 | } |
||
| 115 | |||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | protected function tableExists(string $tableName): bool |
||
| 120 | { |
||
| 121 | $schema = $this->getSchema(); |
||
| 122 | |||
| 123 | return (bool) $schema->hasTable($tableName); |
||
| 124 | } |
||
| 125 | |||
| 126 | protected function clearTable(string $tableName) |
||
| 129 | } |
||
| 130 | |||
| 131 | protected function replaceTable(string $a, string $b, ?bool $keepBackup = false) |
||
| 132 | { |
||
| 133 | if ($this->tableExists($a)) { |
||
| 134 | if ($this->tableExists($b)) { |
||
| 135 | $itemsInDB = DB::query('SELECT DISTINCT ID FROM ' . stripslashes($b) . ';'); |
||
| 136 | if ($itemsInDB->numRecords() > 0 && $keepBackup) { |
||
| 137 | $this->makeTableObsolete($b, true); |
||
| 138 | $this->flushNow('Backing up ' . $b, 'deleted'); |
||
| 139 | } |
||
| 140 | $this->dropTable($b); |
||
| 141 | } |
||
| 142 | |||
| 143 | if (!$this->tableExists($b)) { |
||
| 144 | $this->renameTable($a, $b); |
||
| 145 | } else { |
||
| 146 | $this->flushNow('Could not delete ' . $b, 'deleted'); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * delete the table both with and without slashes. |
||
| 153 | */ |
||
| 154 | protected function dropTable(string $tableName) |
||
| 159 | } |
||
| 160 | |||
| 161 | protected function renameTable(string $a, string $b) |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | protected function fieldExists(string $tableName, string $fieldName): bool |
||
| 183 | { |
||
| 184 | $key = $tableName; |
||
| 185 | if (!isset($this->_cacheFieldExists[$key])) { |
||
| 186 | $schema = $this->getSchema(); |
||
| 187 | $this->_cacheFieldExists[$key] = $schema->fieldList($tableName); |
||
| 188 | } |
||
| 189 | |||
| 190 | return $this->_cacheFieldExists[$key][$fieldName] ?? false; |
||
| 191 | } |
||
| 192 | |||
| 193 | protected function renameField(string $table, string $oldFieldName, string $newFieldName) |
||
| 194 | { |
||
| 195 | $this->getSchema()->dontRequireField($table, $oldFieldName, $newFieldName); |
||
| 196 | } |
||
| 197 | |||
| 198 | protected function getSchema() |
||
| 208 | } |
||
| 209 | |||
| 210 | protected function getSchemaForDataObject() |
||
| 217 | } |
||
| 218 | |||
| 219 | protected function getListOfIDs(string $tableName, ?array $leftJoin = [], ?string $where = '') |
||
| 220 | { |
||
| 221 | return $this->getListAsIterableQuery($tableName, $leftJoin = [], $where = '') |
||
| 222 | ->keyedColumn('ID'); |
||
| 223 | } |
||
| 224 | |||
| 225 | protected function getListAsIterableQuery(string $tableName, ?array $leftJoin = [], ?string $where = '') |
||
| 226 | { |
||
| 227 | $sqlSelect = new SQLSelect(); |
||
| 228 | $sqlSelect->setFrom($tableName); |
||
| 229 | |||
| 230 | if ($leftJoin) { |
||
| 231 | $sqlSelect->addLeftJoin($leftJoin['table'], $leftJoin['onPredicate']); |
||
| 232 | } |
||
| 233 | |||
| 234 | if ($where) { |
||
| 235 | $sqlSelect->addWhere($where); |
||
| 236 | } |
||
| 237 | |||
| 238 | $sqlSelect->setOrderBy($tableName . '.ID'); |
||
| 239 | |||
| 240 | return $sqlSelect->execute(); |
||
| 241 | } |
||
| 242 | |||
| 243 | protected function writeObject($obj, ?array $row = [], ?bool $isPage = false) |
||
| 254 | } |
||
| 255 | |||
| 256 | protected function writePage($obj, $row) |
||
| 257 | { |
||
| 259 | } |
||
| 260 | } |
||
| 261 |
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