Conditions | 35 |
Paths | 4080 |
Total Lines | 161 |
Code Lines | 92 |
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 |
||
202 | public function doBuild($quiet = false, $populate = true, $testMode = false) |
||
203 | { |
||
204 | if ($quiet) { |
||
205 | DB::quiet(); |
||
206 | } else { |
||
207 | $conn = DB::get_conn(); |
||
208 | // Assumes database class is like "MySQLDatabase" or "MSSQLDatabase" (suffixed with "Database") |
||
209 | $dbType = substr(get_class($conn), 0, -8); |
||
210 | $dbVersion = $conn->getVersion(); |
||
211 | $databaseName = (method_exists($conn, 'currentDatabase')) ? $conn->getSelectedDatabase() : ""; |
||
212 | |||
213 | if (Director::is_cli()) { |
||
214 | echo sprintf("\n\nBuilding database %s using %s %s\n\n", $databaseName, $dbType, $dbVersion); |
||
215 | } else { |
||
216 | echo sprintf("<h2>Building database %s using %s %s</h2>", $databaseName, $dbType, $dbVersion); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | // Set up the initial database |
||
221 | if (!DB::is_active()) { |
||
222 | if (!$quiet) { |
||
223 | echo '<p><b>Creating database</b></p>'; |
||
224 | } |
||
225 | |||
226 | // Load parameters from existing configuration |
||
227 | global $databaseConfig; |
||
228 | if (empty($databaseConfig) && empty($_REQUEST['db'])) { |
||
229 | user_error("No database configuration available", E_USER_ERROR); |
||
230 | } |
||
231 | $parameters = (!empty($databaseConfig)) ? $databaseConfig : $_REQUEST['db']; |
||
232 | |||
233 | // Check database name is given |
||
234 | if (empty($parameters['database'])) { |
||
235 | user_error( |
||
236 | "No database name given; please give a value for \$databaseConfig['database']", |
||
237 | E_USER_ERROR |
||
238 | ); |
||
239 | } |
||
240 | $database = $parameters['database']; |
||
241 | |||
242 | // Establish connection and create database in two steps |
||
243 | unset($parameters['database']); |
||
244 | DB::connect($parameters); |
||
245 | DB::create_database($database); |
||
246 | } |
||
247 | |||
248 | // Build the database. Most of the hard work is handled by DataObject |
||
249 | $dataClasses = ClassInfo::subclassesFor('SilverStripe\ORM\DataObject'); |
||
250 | array_shift($dataClasses); |
||
251 | |||
252 | if (!$quiet) { |
||
253 | if (Director::is_cli()) { |
||
254 | echo "\nCREATING DATABASE TABLES\n\n"; |
||
255 | } else { |
||
256 | echo "\n<p><b>Creating database tables</b></p>\n\n"; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | // Initiate schema update |
||
261 | $dbSchema = DB::get_schema(); |
||
262 | $dbSchema->schemaUpdate(function () use ($dataClasses, $testMode, $quiet) { |
||
263 | foreach ($dataClasses as $dataClass) { |
||
264 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
265 | if (!class_exists($dataClass)) { |
||
266 | continue; |
||
267 | } |
||
268 | |||
269 | // Check if this class should be excluded as per testing conventions |
||
270 | $SNG = singleton($dataClass); |
||
271 | if (!$testMode && $SNG instanceof TestOnly) { |
||
272 | continue; |
||
273 | } |
||
274 | |||
275 | // Log data |
||
276 | if (!$quiet) { |
||
277 | if (Director::is_cli()) { |
||
278 | echo " * $dataClass\n"; |
||
279 | } else { |
||
280 | echo "<li>$dataClass</li>\n"; |
||
281 | } |
||
282 | } |
||
283 | |||
284 | // Instruct the class to apply its schema to the database |
||
285 | $SNG->requireTable(); |
||
286 | } |
||
287 | }); |
||
288 | ClassInfo::reset_db_cache(); |
||
289 | |||
290 | if ($populate) { |
||
291 | if (!$quiet) { |
||
292 | if (Director::is_cli()) { |
||
293 | echo "\nCREATING DATABASE RECORDS\n\n"; |
||
294 | } else { |
||
295 | echo "\n<p><b>Creating database records</b></p>\n\n"; |
||
296 | } |
||
297 | } |
||
298 | |||
299 | foreach ($dataClasses as $dataClass) { |
||
300 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
301 | // Test_ indicates that it's the data class is part of testing system |
||
302 | if (strpos($dataClass, 'Test_') === false && class_exists($dataClass)) { |
||
303 | if (!$quiet) { |
||
304 | if (Director::is_cli()) { |
||
305 | echo " * $dataClass\n"; |
||
306 | } else { |
||
307 | echo "<li>$dataClass</li>\n"; |
||
308 | } |
||
309 | } |
||
310 | |||
311 | singleton($dataClass)->requireDefaultRecords(); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | // Remap obsolete class names |
||
316 | $schema = DataObject::getSchema(); |
||
317 | foreach ($this->config()->classname_value_remapping as $oldClassName => $newClassName) { |
||
318 | $baseDataClass = $schema->baseDataClass($newClassName); |
||
319 | $badRecordCount = DataObject::get($baseDataClass) |
||
320 | ->filter(["ClassName" => $oldClassName ]) |
||
321 | ->count(); |
||
322 | if ($badRecordCount > 0) { |
||
323 | if (Director::is_cli()) { |
||
324 | echo " * Correcting $badRecordCount obsolete classname values for $newClassName\n"; |
||
325 | } else { |
||
326 | echo "<li>Correcting $badRecordCount obsolete classname values for $newClassName</li>\n"; |
||
327 | } |
||
328 | $table = $schema->baseDataTable($baseDataClass); |
||
329 | |||
330 | $updateQuery = "UPDATE \"$table%s\" SET \"ClassName\" = ? WHERE \"ClassName\" = ?"; |
||
331 | $updateQueries = [sprintf($updateQuery, '')]; |
||
332 | |||
333 | // Remap versioned table ClassName values as well |
||
334 | $class = singleton($newClassName); |
||
335 | if ($class->has_extension(Versioned::class)) { |
||
336 | if ($class->hasStages()) { |
||
337 | $updateQueries[] = sprintf($updateQuery, '_Live'); |
||
338 | } |
||
339 | $updateQueries[] = sprintf($updateQuery, '_Versions'); |
||
340 | } |
||
341 | |||
342 | foreach ($updateQueries as $query) { |
||
343 | DB::prepared_query($query, [$newClassName, $oldClassName]); |
||
344 | } |
||
345 | } |
||
346 | } |
||
347 | } |
||
348 | |||
349 | touch(TEMP_FOLDER |
||
350 | . '/database-last-generated-' |
||
351 | . str_replace(array('\\', '/', ':'), '.', Director::baseFolder())); |
||
352 | |||
353 | if (isset($_REQUEST['from_installer'])) { |
||
354 | echo "OK"; |
||
355 | } |
||
356 | |||
357 | if (!$quiet) { |
||
358 | echo (Director::is_cli()) ? "\n Database build completed!\n\n" :"<p>Database build completed!</p>"; |
||
359 | } |
||
360 | |||
361 | ClassInfo::reset_db_cache(); |
||
362 | } |
||
363 | |||
417 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.