| Total Lines | 196 |
| Code Lines | 88 |
| 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 |
||
| 219 | protected function setup() |
||
| 220 | { |
||
| 221 | /** |
||
| 222 | * Setup System Migrate Database |
||
| 223 | */ |
||
| 224 | $db = database()->loadConnection($this->optionGroup); |
||
| 225 | |||
| 226 | if (method_exists($db, 'hasTable')) { |
||
| 227 | // SQL Migrate Setup |
||
| 228 | if ( ! $db->hasTable('sys_migrations')) { |
||
| 229 | $forge = $db->getForge(); |
||
| 230 | $forge->createTable('sys_migrations', [ |
||
| 231 | 'id' => [ |
||
| 232 | 'type' => 'INT', |
||
| 233 | 'length' => 10, |
||
| 234 | 'unsigned' => true, |
||
| 235 | 'auto_increment' => true, |
||
| 236 | 'null' => false, |
||
| 237 | 'primary_key' => true, |
||
| 238 | ], |
||
| 239 | 'filename' => [ |
||
| 240 | 'type' => 'VARCHAR', |
||
| 241 | 'length' => 255, |
||
| 242 | 'null' => false, |
||
| 243 | ], |
||
| 244 | 'version' => [ |
||
| 245 | 'type' => 'VARCHAR', |
||
| 246 | 'length' => 255, |
||
| 247 | 'null' => false, |
||
| 248 | 'default' => 'v0.0.0', |
||
| 249 | ], |
||
| 250 | 'timestamp' => [ |
||
| 251 | 'type' => 'datetime', |
||
| 252 | 'null' => false, |
||
| 253 | ], |
||
| 254 | 'batch' => [ |
||
| 255 | 'type' => 'INT', |
||
| 256 | 'length' => 10, |
||
| 257 | 'unsigned' => true, |
||
| 258 | 'null' => false, |
||
| 259 | 'default' => 0, |
||
| 260 | ], |
||
| 261 | 'record_status' => [ |
||
| 262 | 'type' => 'enum', |
||
| 263 | 'value' => ['DELETE', 'TRASH', 'DRAFT', 'UNPUBLISH', 'PUBLISH'], |
||
| 264 | 'null' => false, |
||
| 265 | 'default' => 'PUBLISH', |
||
| 266 | ], |
||
| 267 | 'record_create_timestamp' => [ |
||
| 268 | 'type' => 'datetime', |
||
| 269 | 'null' => true, |
||
| 270 | ], |
||
| 271 | 'record_create_user' => [ |
||
| 272 | 'type' => 'int', |
||
| 273 | 'length' => 10, |
||
| 274 | 'unsigned' => true, |
||
| 275 | 'null' => true, |
||
| 276 | 'default' => 0, |
||
| 277 | ], |
||
| 278 | 'record_update_timestamp' => [ |
||
| 279 | 'type' => 'datetime', |
||
| 280 | 'null' => true, |
||
| 281 | 'timestamp' => true, |
||
| 282 | ], |
||
| 283 | 'record_update_user' => [ |
||
| 284 | 'type' => 'int', |
||
| 285 | 'length' => 10, |
||
| 286 | 'unsigned' => true, |
||
| 287 | 'null' => true, |
||
| 288 | 'default' => 0, |
||
| 289 | ], |
||
| 290 | ], true); |
||
| 291 | } |
||
| 292 | |||
| 293 | $this->model = new class extends Model |
||
| 294 | { |
||
| 295 | /** |
||
| 296 | * Model::$table |
||
| 297 | * |
||
| 298 | * @var string |
||
| 299 | */ |
||
| 300 | public $table = 'sys_migrations'; |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Model::$visibleColumns |
||
| 304 | * |
||
| 305 | * @var array |
||
| 306 | */ |
||
| 307 | public $visibleColumns = [ |
||
| 308 | 'id', |
||
| 309 | 'filename', |
||
| 310 | 'version', |
||
| 311 | 'timestamp', |
||
| 312 | 'batch', |
||
| 313 | ]; |
||
| 314 | |||
| 315 | // ------------------------------------------------------------------------ |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Model::__construct |
||
| 319 | */ |
||
| 320 | public function register() |
||
| 321 | { |
||
| 322 | $batch = $this->getLatestBatch(); |
||
| 323 | $files = glob(PATH_DATABASE . "migrations/*.php"); |
||
| 324 | |||
| 325 | foreach ($files as $filename) { |
||
| 326 | $sets = [ |
||
| 327 | 'filename' => $this->getFilename($filename), |
||
| 328 | 'version' => $this->getFileVersion($filename), |
||
| 329 | 'timestamp' => $this->getFileTimestamp($filename), |
||
| 330 | 'batch' => $batch, |
||
| 331 | ]; |
||
| 332 | |||
| 333 | $this->insertOrUpdate($sets, ['filename' => $sets[ 'filename' ]]); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | // ------------------------------------------------------------------------ |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Migrate::getFilename |
||
| 341 | * |
||
| 342 | * Extracts the migration timestamp from a filename |
||
| 343 | * |
||
| 344 | * @param string $filename |
||
| 345 | * |
||
| 346 | * @return false|string |
||
| 347 | */ |
||
| 348 | protected function getFilename($filename) |
||
| 349 | { |
||
| 350 | return str_replace(PATH_DATABASE . 'migrations' . DIRECTORY_SEPARATOR, '', $filename); |
||
| 351 | } |
||
| 352 | |||
| 353 | // ------------------------------------------------------------------------ |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Migrate::getFileTimestamp |
||
| 357 | * |
||
| 358 | * Extracts the migration timestamp from a filename |
||
| 359 | * |
||
| 360 | * @param string $filename |
||
| 361 | * |
||
| 362 | * @return false|string |
||
| 363 | */ |
||
| 364 | protected function getFileTimestamp($filename) |
||
| 365 | { |
||
| 366 | $timestamp = filemtime($filename); |
||
| 367 | preg_match('/\d{4}[-]?\d{2}[-]?\d{2}[-]?\d{2}[-]?\d{2}[-]?\d{2}/', $filename, $matches); |
||
| 368 | |||
| 369 | $timestamp = count($matches) ? strtotime($matches[ 0 ]) : $timestamp; |
||
| 370 | |||
| 371 | return date('Y-m-d H:i:s', $timestamp); |
||
| 372 | } |
||
| 373 | |||
| 374 | // ------------------------------------------------------------------------ |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Migrate::getFileVersion |
||
| 378 | * |
||
| 379 | * Extracts the migration timestamp from a filename |
||
| 380 | * |
||
| 381 | * @param string $filename |
||
| 382 | * |
||
| 383 | * @return false|string |
||
| 384 | */ |
||
| 385 | protected function getFileVersion($filename) |
||
| 386 | { |
||
| 387 | $version = 'v0.0.0'; |
||
| 388 | preg_match('/v\d*[.]?\d*[.]\d*/', $filename, $matches); |
||
| 389 | |||
| 390 | return count($matches) ? $matches[ 0 ] : $version; |
||
| 391 | } |
||
| 392 | |||
| 393 | // ------------------------------------------------------------------------ |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Model::getLatestBatch |
||
| 397 | */ |
||
| 398 | public function getLatestBatch() |
||
| 399 | { |
||
| 400 | $batch = 1; |
||
| 401 | if ($result = $this->qb->table('sys_migrations')->max('batch', 'lastBatch')->get()) { |
||
| 402 | if ($result->count()) { |
||
| 403 | $batch = (int)$result->first()->lastBatch; |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | return $batch == 0 ? 1 : $batch; |
||
| 408 | } |
||
| 409 | }; |
||
| 410 | |||
| 411 | $this->model->register(); |
||
| 412 | } elseif (method_exists($db, 'hasCollection')) { |
||
| 413 | // NoSQL Migrate Setup |
||
| 414 | if ( ! $db->hasCollection('sys_migrations')) { |
||
| 415 | // Coming Soon |
||
| 628 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.