Conditions | 59 |
Paths | 1808 |
Total Lines | 208 |
Lines | 5 |
Ratio | 2.4 % |
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 |
||
109 | protected function obsoletefields($deleteSafeOnes = false, $fixBrokenDataObject = false, $deleteAll = false) |
||
110 | { |
||
111 | increase_time_limit_to(600); |
||
112 | $dataClasses = ClassInfo::subclassesFor('DataObject'); |
||
113 | $notCheckedArray = array(); |
||
114 | $canBeSafelyDeleted = array(); |
||
115 | //remove dataobject |
||
116 | array_shift($dataClasses); |
||
117 | $rows = DB::query("SHOW TABLES;"); |
||
118 | $actualTables = array(); |
||
119 | if ($rows) { |
||
120 | foreach ($rows as $key => $item) { |
||
121 | foreach ($item as $table) { |
||
122 | $actualTables[$table] = $table; |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | echo "<h1>Report of fields that may not be required.</h1>"; |
||
127 | echo "<p>NOTE: it may contain fields that are actually required (e.g. versioning or many-many relationships) and it may also leave out some obsolete fields. Use as a guide only.</p>"; |
||
128 | foreach ($dataClasses as $dataClass) { |
||
129 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
130 | if (class_exists($dataClass)) { |
||
131 | $dataObject = $dataClass::create(); |
||
132 | if (!($dataObject instanceof TestOnly)) { |
||
133 | $requiredFields = $this->swapArray(DataObject::database_fields($dataObject->ClassName)); |
||
134 | if (count($requiredFields)) { |
||
135 | foreach ($requiredFields as $field) { |
||
136 | if (!$dataObject->hasOwnTableDatabaseField($field)) { |
||
137 | DB::alteration_message(" **** $dataClass.$field DOES NOT EXIST BUT IT SHOULD BE THERE!", "deleted"); |
||
138 | } |
||
139 | } |
||
140 | $actualFields = $this->swapArray(DB::fieldList($dataClass)); |
||
141 | if ($actualFields) { |
||
142 | foreach ($actualFields as $actualField) { |
||
143 | if ($deleteAll) { |
||
144 | $link = " !!!!!!!!!!! DELETED !!!!!!!!!"; |
||
145 | } else { |
||
146 | $warning = Config::inst()->get("DataIntegrityTest", "warning"); |
||
147 | $link = "<a href=\"".Director::absoluteBaseURL()."dev/tasks/DataIntegrityTest/?do=deleteonefield/".$dataClass."/".$actualField."/\" onclick=\"return confirm('".$warning."');\">delete field</a>"; |
||
148 | } |
||
149 | if (!in_array($actualField, array("ID", "Version"))) { |
||
150 | if (!in_array($actualField, $requiredFields)) { |
||
151 | $distinctCount = DB::query("SELECT COUNT(DISTINCT \"$actualField\") FROM \"$dataClass\" WHERE \"$actualField\" IS NOT NULL AND \"$actualField\" <> '' AND \"$actualField\" <> '0';")->value(); |
||
152 | DB::alteration_message("<br /><br />\n\n$dataClass.$actualField $link - unique entries: $distinctCount", "deleted"); |
||
153 | if ($distinctCount) { |
||
154 | $rows = DB::query(" |
||
155 | SELECT \"$actualField\" as N, COUNT(\"$actualField\") as C |
||
156 | FROM \"$dataClass\" |
||
157 | GROUP BY \"$actualField\" |
||
158 | ORDER BY C DESC |
||
159 | LIMIT 7"); |
||
160 | if ($rows) { |
||
161 | foreach ($rows as $row) { |
||
162 | DB::alteration_message(" ".$row["C"].": ".$row["N"]); |
||
163 | } |
||
164 | } |
||
165 | } else { |
||
166 | if (!isset($canBeSafelyDeleted[$dataClass])) { |
||
167 | $canBeSafelyDeleted[$dataClass] = array(); |
||
168 | } |
||
169 | $canBeSafelyDeleted[$dataClass][$actualField] = "$dataClass.$actualField"; |
||
170 | } |
||
171 | if ($deleteAll || ($deleteSafeOnes && $distinctCount == 0)) { |
||
172 | $this->deleteField($dataClass, $actualField); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | if ($actualField == "Version" && !in_array($actualField, $requiredFields)) { |
||
177 | $versioningPresent = $dataObject->hasVersioning(); |
||
178 | if (!$versioningPresent) { |
||
179 | DB::alteration_message("$dataClass.$actualField $link", "deleted"); |
||
180 | if ($deleteAll) { |
||
181 | $this->deleteField($dataClass, $actualField); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | $rawCount = DB::query("SELECT COUNT(\"ID\") FROM \"$dataClass\"")->value(); |
||
188 | Versioned::set_reading_mode("Stage.Stage"); |
||
189 | $realCount = 0; |
||
190 | $allSubClasses = array_unique(array($dataClass)+ClassInfo::subclassesFor($dataClass)); |
||
191 | $objects = $dataClass::get()->filter(array("ClassName" => $allSubClasses)); |
||
192 | if ($objects->count()) { |
||
193 | $realCount = $objects->count(); |
||
194 | } |
||
195 | if ($rawCount != $realCount) { |
||
196 | echo "<hr />"; |
||
197 | $sign = " > "; |
||
198 | if ($rawCount < $realCount) { |
||
199 | $sign = " < "; |
||
200 | } |
||
201 | DB::alteration_message("The DB Table Row Count does not seem to match the DataObject Count for <strong>$dataClass ($rawCount $sign $realCount)</strong>. This could indicate an error as generally these numbers should match.", "deleted"); |
||
202 | if ($fixBrokenDataObject) { |
||
203 | $objects = $dataClass::get()->where("LinkedTable.ID IS NULL")->leftJoin($dataClass, "$dataClass.ID = LinkedTable.ID", "LinkedTable"); |
||
204 | if ($objects->count() > 500) { |
||
205 | DB::alteration_message("It is recommended that you manually fix the difference in real vs object count in $dataClass. There are more than 500 records so it would take too long to do it now.", "deleted"); |
||
206 | } else { |
||
207 | DB::alteration_message("Now trying to recreate missing items... COUNT = ".$objects->count(), "created"); |
||
208 | foreach ($objects as $object) { |
||
209 | if (DB::query("SELECT COUNT(\"ID\") FROM \"$dataClass\" WHERE \"ID\" = ".$object->ID.";")->value() != 1) { |
||
210 | Config::inst()->update('DataObject', 'validation_enabled', false); |
||
211 | $object->write(true, false, true, false); |
||
212 | Config::inst()->update('DataObject', 'validation_enabled', true); |
||
213 | } |
||
214 | } |
||
215 | $objectCount = $dataClass::get()->count(); |
||
216 | DB::alteration_message("Consider deleting superfluous records from table $dataClass .... COUNT =".($rawCount - $objectCount)); |
||
217 | $ancestors = ClassInfo::ancestry($dataClass, true); |
||
218 | if ($ancestors && is_array($ancestors) && count($ancestors)) { |
||
219 | foreach ($ancestors as $ancestor) { |
||
220 | if ($ancestor != $dataClass) { |
||
221 | echo "DELETE `$dataClass`.* FROM `$dataClass` LEFT JOIN `$ancestor` ON `$dataClass`.`ID` = `$ancestor`.`ID` WHERE `$ancestor`.`ID` IS NULL;"; |
||
222 | DB::query("DELETE `$dataClass`.* FROM `$dataClass` LEFT JOIN `$ancestor` ON `$dataClass`.`ID` = `$ancestor`.`ID` WHERE `$ancestor`.`ID` IS NULL;"); |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | echo "<hr />"; |
||
229 | } |
||
230 | unset($actualTables[$dataClass]); |
||
231 | } else { |
||
232 | $db = DB::getConn(); |
||
233 | if ($db->hasTable($dataClass)) { |
||
234 | DB::alteration_message(" **** The $dataClass table exists, but according to the data-scheme it should not be there ", "deleted"); |
||
235 | } else { |
||
236 | $notCheckedArray[] = $dataClass; |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | |||
243 | if (count($canBeSafelyDeleted)) { |
||
244 | DB::alteration_message("<h2>Can be safely deleted: </h2>"); |
||
245 | foreach ($canBeSafelyDeleted as $table => $fields) { |
||
246 | DB::alteration_message($table.": ".implode(", ", $fields)); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | if (count($notCheckedArray)) { |
||
251 | echo "<h3>Did not check the following classes as no fields appear to be required and hence there is no database table.</h3>"; |
||
252 | foreach ($notCheckedArray as $table) { |
||
253 | View Code Duplication | if (DB::query("SHOW TABLES LIKE '".$table."'")->value()) { |
|
254 | DB::alteration_message($table ." - NOTE: a table exists for this Class, this is an unexpected result", "deleted"); |
||
255 | } else { |
||
256 | DB::alteration_message($table, "created"); |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | if (count($actualTables)) { |
||
262 | echo "<h3>Other Tables in Database not directly linked to a Silverstripe DataObject:</h3>"; |
||
263 | foreach ($actualTables as $table) { |
||
264 | $remove = true; |
||
265 | if (class_exists($table)) { |
||
266 | $classExistsMessage = " a PHP class with this name exists."; |
||
267 | $obj = singleton($table); |
||
268 | //not sure why we have this. |
||
269 | if ($obj instanceof DataExtension) { |
||
270 | $remove = false; |
||
271 | } elseif (class_exists("Versioned") && $obj->hasExtension("Versioned")) { |
||
272 | $remove = false; |
||
273 | } |
||
274 | } else { |
||
275 | $classExistsMessage = " NO PHP class with this name exists."; |
||
276 | if (substr($table, -5) == "_Live") { |
||
277 | $remove = false; |
||
278 | } |
||
279 | if (substr($table, -9) == "_versions") { |
||
280 | $remove = false; |
||
281 | } |
||
282 | //many 2 many tables... |
||
283 | if (strpos($table, "_")) { |
||
284 | $class = explode("_", $table); |
||
285 | $manyManyClass = substr($table, 0, strrpos($table, '_')); |
||
286 | $manyManyExtension = substr($table, strrpos($table, '_') + 1 - strlen($table)); |
||
287 | if (class_exists($manyManyClass)) { |
||
288 | $manyManys = Config::inst()->get($manyManyClass, "many_many"); |
||
289 | if (isset($manyManys[$manyManyExtension])) { |
||
290 | $remove = false; |
||
291 | } |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | if ($remove) { |
||
296 | if (substr($table, 0, strlen("_obsolete_")) != "_obsolete_") { |
||
297 | $rowCount = DB::query("SELECT COUNT(*) FROM $table")->value(); |
||
298 | DB::alteration_message($table.", rows ".$rowCount); |
||
299 | $obsoleteTableName = "_obsolete_".$table; |
||
300 | if (!$this->tableExists($obsoleteTableName)) { |
||
301 | DB::alteration_message("We recommend deleting $table or making it obsolete by renaming it to ".$obsoleteTableName, "deleted"); |
||
302 | if ($deleteAll) { |
||
303 | DB::getConn()->renameTable($table, $obsoleteTableName); |
||
304 | } else { |
||
305 | DB::alteration_message($table." - ".$classExistsMessage." It can be moved to _obsolete_".$table.".", "created"); |
||
306 | } |
||
307 | } else { |
||
308 | DB::alteration_message("I'd recommend to move <strong>$table</strong> to <strong>".$obsoleteTableName."</strong>, but that table already exists", "deleted"); |
||
309 | } |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | |||
315 | echo "<a href=\"".Director::absoluteURL("/dev/tasks/DataIntegrityTest/")."\">back to main menu.</a>"; |
||
316 | } |
||
317 | |||
476 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: