fucongcong/framework

View on GitHub
core/Group/Console/Command/SqlRollBackCommand.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php
 
namespace Group\Console\Command;
 
use Group\Console\Command as Command;
use Group\Console\Command\SqlCleanCommand as SqlCleanCommand;
use Group\Common\ArrayToolkit;
 
class SqlRollBackCommand extends Command
{
protected $fileList = [];
 
protected $toVersion;
 
protected $versions = [];
 
protected $dao;
 
public function init()
{
Missing class import via use statement (line '21', column '26').
$this->dao = new \Dao();
$input = $this->getArgv();
$this->toVersion = isset($input[0]) ? $input[0] : null;
 
$versions = $this->doSql($this->getMigrations(), false)->fetchAll();
$this->versions = array_values(ArrayToolkit::column($versions, "version"));
 
Inline control structures are not allowed
if (!in_array($this->toVersion, $this->versions)) $this->error('找不到指定的版本');
 
$this->ListSql(__ROOT__."app/sql/");
}
 
Function `ListSql` has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
Method name "SqlRollBackCommand::ListSql" is not in camel caps format
The method ListSql is not named in camelCase.
private function ListSql($sqlDir)
Whitespace found at end of line
{
$files = [];
if (is_dir($sqlDir)) {
$dir = opendir($sqlDir);
while (($file = readdir($dir)) !== false) {
$file = explode(".", $file);
$fileName = $file[0];
 
if ($fileName && isset($file[1]) && $file[1] == "php") {
Whitespace found at end of line
$files[] = $fileName;
}
}
closedir($dir);
}
 
krsort($files);
foreach ($files as $fileName) {
if ($fileName >= $this->toVersion && in_array($fileName, $this->versions)) {
$migrateClass = "\\app\\sql\\".$fileName;
$sqlMigrate = new $migrateClass;
$sqlMigrate->back();
$sqlArr = $sqlMigrate->getSqlArr();
 
$this->startRollBack($sqlArr);
 
$this->doSql($this->dropVersion($fileName), false);
}
}
}
 
private function startRollBack($sqlArr)
{
foreach ($sqlArr as $sql) {
$this->doSql($sql);
}
}
 
The method doSql has a boolean flag argument $needOutput, which is a certain sign of a Single Responsibility Principle violation.
private function doSql($sql, $needOutput = true)
Whitespace found at end of line
{
Inline control structures are not allowed
if ($needOutput) $this->outPut($sql);
 
return $this->dao->querySql($sql, 'default');
}
 
private function dropVersion($version)
{
return "DELETE FROM `migration_versions` WHERE version = '{$version}'";
}
 
private function getMigrations()
{
return "SELECT * FROM `migration_versions`";
}
}