CodeSniffer

This commit is contained in:
Paminger
2016-04-10 21:43:45 +02:00
parent 705ee7c62c
commit 27af06cbb0
4 changed files with 59 additions and 374 deletions
+57 -45
View File
@@ -1,6 +1,7 @@
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if (! defined('BASEPATH'))
exit('No direct script access allowed');
/**
* Database Class
@@ -15,28 +16,27 @@ class DBTools extends FHC_Controller
*
* @var string
*/
protected $_seed_path = APPPATH.'seeds/';
protected $seed_path = APPPATH.'seeds/';
/**
* Seed basename regex
*
* @var string
*/
protected $_seed_regex = '/^\d{3}_(\w+)$/';
protected $seed_regex = '/^\d{3}_(\w+)$/';
/**
* Initialize DB-Tools Class
*
* @return void
*/
function __construct()
public function __construct()
{
parent::__construct();
if ($this->input->is_cli_request())
{
$cli=true;
$cli = true;
}
else
{
@@ -45,16 +45,15 @@ class DBTools extends FHC_Controller
}
// can only be run in the development environment
if (ENVIRONMENT !== 'development') {
if (ENVIRONMENT !== 'development')
exit('Wowsers! You don\'t want to do that!');
}
$this->load->database('system'); //Use the system-Connection for DB-Manipulation
$this->load->library('migration');
// If not set, set it
//$this->_seed_path !== '' OR $this->_seed_path = APPPATH.'seeds/';
//$this->seed_path !== '' OR $this->seed_path = APPPATH.'seeds/';
// Add trailing slash if not set
//$this->_seed_path = rtrim($this->_seed_path, '/').'/';
//$this->seed_path = rtrim($this->seed_path, '/').'/';
// Load seed language
$this->lang->load('seed');
@@ -68,43 +67,51 @@ class DBTools extends FHC_Controller
log_message('info', 'DB-Tools Controller Initialized');
}
public function index()
/**
* Main function index as help
*
* @return void
*/
public function index()
{
$result = "The following are the available command line interface commands\n\n";
$result .= "php index.ci.php DBTools migrate [\"version_number\"] Run all migrations. The version number is optional.\n";
$result .= "php index.ci.php DBTools migrate [\"version_number\"] Run all migrations. "
$result .= "The version number is optional.\n";
$result .= "php index.ci.php DBTools seed \"file_name\" Run the specified seed file.\n";
echo $result . PHP_EOL;
echo $result.PHP_EOL;
}
/**
* Migrate to latest or current version
*
* @param string $version One of either "latest" or "current"
* @return void
*/
public function migrate($version = 'latest')
{
if ($this->cli && $this->migration->current() === FALSE)
if ($this->cli && $this->migration->current() === false)
{
show_error($this->migration->error_string());
}
elseif ($version != 'latest' && $version != 'current')
{
$this->_failed('Migration version must be either latest or current');
$this->__failed('Migration version must be either latest or current');
}
if (!$this->migration->$version())
{
$this->_failed();
$this->__failed();
}
$this->_succeeded();
$this->__succeeded();
}
/**
* Migrate to a specific version
*
* @return void
*/
public function version()
{
@@ -116,36 +123,39 @@ class DBTools extends FHC_Controller
if (!$this->migrate->version($version))
{
$this->_failed();
$this->__failed();
}
$this->_succeeded();
$this->__succeeded();
}
/**
* Roll-back to the last version before current
*
* @param int $version The migration to rollback to, defaults to previous
* @return void
*/
public function rollback($version = null)
{
if (is_null($version))
{
$version = $this->_get_version() ?: 1;
$version = $this->__getVersion() ?: 1;
$version--;
}
// Check it's definitely false, we could be rolling back to v0
if (false === $this->migration->version($version))
{
$this->_failed();
$this->__failed();
}
$this->_succeeded('rolled back');
$this->__succeeded('rolled back');
}
/**
* ROLLBACK ALL THE THINGS!
*
* @return void
*/
public function uninstall()
{
@@ -155,17 +165,17 @@ class DBTools extends FHC_Controller
/**
* Seeds DB with Testdata
*
* @param string $name
* @param string $name Name of the seed file.
* @return bool
*/
function seed($name = null)
public function seed($name = null)
{
$seeds = $this->find_seeds();
$seeds = $this->findSeeds();
if (empty($seeds))
{
$this->_error_string = $this->lang->line('seed_none_found');
return FALSE;
return false;
}
$method = 'seed';
@@ -173,21 +183,21 @@ class DBTools extends FHC_Controller
foreach ($seeds as $number => $file)
{
include_once($file);
$class = 'Seed_'.ucfirst(strtolower($this->_get_seed_name(basename($file, '.php'))));
$class = 'Seed_'.ucfirst(strtolower($this->_getSeedName(basename($file, '.php'))));
// Validate the seed file structure
if ( ! class_exists($class, FALSE))
if (! class_exists($class, false))
{
$this->_error_string = sprintf($this->lang->line('seed_class_doesnt_exist'), $class);
return FALSE;
return false;
}
// method_exists() returns true for non-public methods,
// while is_callable() can't be used without instantiating.
// Only get_class_methods() satisfies both conditions.
elseif ( ! in_array($method, array_map('strtolower', get_class_methods($class))))
elseif (! in_array($method, array_map('strtolower', get_class_methods($class))))
{
$this->_error_string = sprintf($this->lang->line('seed_missing_'.$method.'_method'), $class);
return FALSE;
return false;
}
$pending[$number] = array($class, $method);
@@ -207,19 +217,19 @@ class DBTools extends FHC_Controller
*
* @return array list of seed file paths sorted by version
*/
public function find_seeds()
public function findSeeds()
{
$seeds = array();
// Load all *_*.php files in the seeds path
foreach (glob($this->_seed_path.'*_*.php') as $file)
foreach (glob($this->seed_path.'*_*.php') as $file)
{
$name = basename($file, '.php');
// Filter out non-seed files
if (preg_match($this->_seed_regex, $name))
if (preg_match($this->seed_regex, $name))
{
$number = $this->_get_seed_number($name);
$number = $this->_getSeedNumber($name);
// There cannot be duplicate seed numbers
if (isset($seeds[$number]))
@@ -239,10 +249,10 @@ class DBTools extends FHC_Controller
/**
* Extracts the seed number from a filename
*
* @param string $seed
* @param string $seed Filename of the seed.
* @return string Numeric portion of a seed filename
*/
protected function _get_seed_number($seed)
protected function _getSeedNumber($seed)
{
return sscanf($seed, '%[0-9]+', $number)
? $number : '0';
@@ -250,10 +260,10 @@ class DBTools extends FHC_Controller
/**
* Extracts the seed class name from a filename
*
* @param string $seed
* @param string $seed Filename of the seed.
* @return string text portion of a migration filename
*/
protected function _get_seed_name($seed)
protected function _getSeedName($seed)
{
$parts = explode('_', $seed);
array_shift($parts);
@@ -264,10 +274,11 @@ class DBTools extends FHC_Controller
* Yay, it worked! Tell the user.
*
* @param string $task What did we just do? We...
* @return void
*/
private function _succeeded($task = 'migrated')
private function __succeeded($task = 'migrated')
{
$version = $this->_get_version();
$version = $this->__getVersion();
exit('Successfully '.$task.' to version '.$version);
}
@@ -275,19 +286,20 @@ class DBTools extends FHC_Controller
* Output an error message when it all goes tits up
*
* @param string $message Error to output (default to CI's migration error)
* @return void
*/
private function _failed($message = null)
private function __failed($message = null)
{
$message = $message ?: $this->migration->error_string();
show_error($message);
}
/**
* Carbon copy of parent::_get_version, but that's protected.
* Carbon copy of parent::__getVersion, but that's protected.
*
* @return int Currently installed migration number
*/
private function _get_version()
private function __getVersion()
{
$row = $this->db->get('ci_migrations')->row();
return $row ? $row->version : 0;