Conditions | 29 |
Paths | 4080 |
Total Lines | 110 |
Code Lines | 60 |
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 |
||
174 | public function doBuild($quiet = false, $populate = true, $testMode = false) { |
||
175 | if($quiet) { |
||
176 | DB::quiet(); |
||
177 | } else { |
||
178 | $conn = DB::get_conn(); |
||
179 | // Assumes database class is like "MySQLDatabase" or "MSSQLDatabase" (suffixed with "Database") |
||
180 | $dbType = substr(get_class($conn), 0, -8); |
||
181 | $dbVersion = $conn->getVersion(); |
||
182 | $databaseName = (method_exists($conn, 'currentDatabase')) ? $conn->getSelectedDatabase() : ""; |
||
183 | |||
184 | if(Director::is_cli()) { |
||
185 | echo sprintf("\n\nBuilding database %s using %s %s\n\n", $databaseName, $dbType, $dbVersion); |
||
186 | } else { |
||
187 | echo sprintf("<h2>Building database %s using %s %s</h2>", $databaseName, $dbType, $dbVersion); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | // Set up the initial database |
||
192 | if(!DB::is_active()) { |
||
193 | if(!$quiet) { |
||
194 | echo '<p><b>Creating database</b></p>'; |
||
195 | } |
||
196 | |||
197 | // Load parameters from existing configuration |
||
198 | global $databaseConfig; |
||
199 | if(empty($databaseConfig) && empty($_REQUEST['db'])) { |
||
200 | user_error("No database configuration available", E_USER_ERROR); |
||
201 | } |
||
202 | $parameters = (!empty($databaseConfig)) ? $databaseConfig : $_REQUEST['db']; |
||
203 | |||
204 | // Check database name is given |
||
205 | if(empty($parameters['database'])) { |
||
206 | user_error("No database name given; please give a value for \$databaseConfig['database']", |
||
207 | E_USER_ERROR); |
||
208 | } |
||
209 | $database = $parameters['database']; |
||
210 | |||
211 | // Establish connection and create database in two steps |
||
212 | unset($parameters['database']); |
||
213 | DB::connect($parameters); |
||
214 | DB::create_database($database); |
||
215 | } |
||
216 | |||
217 | // Build the database. Most of the hard work is handled by DataObject |
||
218 | $dataClasses = ClassInfo::subclassesFor('DataObject'); |
||
219 | array_shift($dataClasses); |
||
220 | |||
221 | if(!$quiet) { |
||
222 | if(Director::is_cli()) echo "\nCREATING DATABASE TABLES\n\n"; |
||
223 | else echo "\n<p><b>Creating database tables</b></p>\n\n"; |
||
224 | } |
||
225 | |||
226 | // Initiate schema update |
||
227 | $dbSchema = DB::get_schema(); |
||
228 | $dbSchema->schemaUpdate(function() use($dataClasses, $testMode, $quiet){ |
||
229 | foreach($dataClasses as $dataClass) { |
||
230 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
231 | if(!class_exists($dataClass)) continue; |
||
232 | |||
233 | // Check if this class should be excluded as per testing conventions |
||
234 | $SNG = singleton($dataClass); |
||
235 | if(!$testMode && $SNG instanceof TestOnly) continue; |
||
236 | |||
237 | // Log data |
||
238 | if(!$quiet) { |
||
239 | if(Director::is_cli()) echo " * $dataClass\n"; |
||
240 | else echo "<li>$dataClass</li>\n"; |
||
241 | } |
||
242 | |||
243 | // Instruct the class to apply its schema to the database |
||
244 | $SNG->requireTable(); |
||
245 | } |
||
246 | }); |
||
247 | ClassInfo::reset_db_cache(); |
||
248 | |||
249 | if($populate) { |
||
250 | if(!$quiet) { |
||
251 | if(Director::is_cli()) echo "\nCREATING DATABASE RECORDS\n\n"; |
||
252 | else echo "\n<p><b>Creating database records</b></p>\n\n"; |
||
253 | } |
||
254 | |||
255 | foreach($dataClasses as $dataClass) { |
||
256 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
257 | // Test_ indicates that it's the data class is part of testing system |
||
258 | if(strpos($dataClass,'Test_') === false && class_exists($dataClass)) { |
||
259 | if(!$quiet) { |
||
260 | if(Director::is_cli()) echo " * $dataClass\n"; |
||
261 | else echo "<li>$dataClass</li>\n"; |
||
262 | } |
||
263 | |||
264 | singleton($dataClass)->requireDefaultRecords(); |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | |||
269 | touch(TEMP_FOLDER |
||
270 | . '/database-last-generated-' |
||
271 | . str_replace(array('\\', '/', ':'), '.', Director::baseFolder()) |
||
272 | ); |
||
273 | |||
274 | if(isset($_REQUEST['from_installer'])) { |
||
275 | echo "OK"; |
||
276 | } |
||
277 | |||
278 | if(!$quiet) { |
||
279 | echo (Director::is_cli()) ? "\n Database build completed!\n\n" :"<p>Database build completed!</p>"; |
||
280 | } |
||
281 | |||
282 | ClassInfo::reset_db_cache(); |
||
283 | } |
||
284 | |||
343 |