From 1261887802c02a4e951750e9aa375a096c649e56 Mon Sep 17 00:00:00 2001 From: Henry Paradiz Date: Mon, 21 May 2018 14:11:25 -0400 Subject: [PATCH] php-cs-fixer --- .gitignore | 3 +- .php_cs.dist | 22 +++++++++ src/Command.php | 10 ++-- src/Controllers/CommandLineHandler.php | 7 ++- src/Controllers/Commands/Basics.php | 14 +++--- src/Controllers/Commands/Database.php | 34 +++++++------- src/Controllers/Commands/Initialize.php | 62 ++++++++++++------------- src/Controllers/Commands/Tester.php | 20 ++++---- src/Env.php | 44 ++++++++++-------- 9 files changed, 122 insertions(+), 94 deletions(-) create mode 100644 .php_cs.dist diff --git a/.gitignore b/.gitignore index 88e99d5..c0257d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor -composer.lock \ No newline at end of file +composer.lock +.php_cs.cache \ No newline at end of file diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..b9257b5 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,22 @@ +exclude('somedir') + ->in(__DIR__) +; + +return PhpCsFixer\Config::create() + ->setRules([ + '@PSR1' => true, + '@PSR2' => true, + 'array_syntax' => ['syntax' => 'short'], + 'trailing_comma_in_multiline_array' => true, + 'ternary_operator_spaces' => true, + 'trim_array_spaces' => true, + 'ordered_imports' => [ + 'sortAlgorithm' => 'length' + ], + 'ordered_class_elements' => true + ]) + ->setFinder($finder) +; \ No newline at end of file diff --git a/src/Command.php b/src/Command.php index a108193..566e326 100644 --- a/src/Command.php +++ b/src/Command.php @@ -10,12 +10,12 @@ namespace Divergence\CLI; use Divergence\CLI\Env; -use Divergence\CLI\Controllers\CommandLineHandler; +use \League\CLImate\CLImate; use Divergence\CLI\Controllers\Commands\Basics; use Divergence\CLI\Controllers\Commands\Tester; -use Divergence\CLI\Controllers\Commands\Initialize; +use Divergence\CLI\Controllers\CommandLineHandler; -use \League\CLImate\CLImate; +use Divergence\CLI\Controllers\Commands\Initialize; class Command extends CommandLineHandler { @@ -36,7 +36,7 @@ public static function handle() //static::$climate->style->addCommand('orange', '38;5;208' /*'38;5;208'*/); //static::$climate->orange('test'); exit; - switch($action = static::shiftArgs()) { + switch ($action = static::shiftArgs()) { case 'init': Initialize::init(); break; @@ -65,4 +65,4 @@ public static function handle() Basics::usage(); } } -} \ No newline at end of file +} diff --git a/src/Controllers/CommandLineHandler.php b/src/Controllers/CommandLineHandler.php index 8903f66..15feb73 100644 --- a/src/Controllers/CommandLineHandler.php +++ b/src/Controllers/CommandLineHandler.php @@ -11,13 +11,12 @@ abstract class CommandLineHandler { - abstract public static function handle(); - public static $_args; + abstract public static function handle(); protected static function setArgs($path = null) { - if(!static::$_args) { + if (!static::$_args) { static::$_args = $_SERVER['argv']; } } @@ -53,4 +52,4 @@ protected static function unshiftArgs($string) } return array_unshift(static::$_args, $string); } -} \ No newline at end of file +} diff --git a/src/Controllers/Commands/Basics.php b/src/Controllers/Commands/Basics.php index 0a52cc5..3d50fa4 100644 --- a/src/Controllers/Commands/Basics.php +++ b/src/Controllers/Commands/Basics.php @@ -9,8 +9,8 @@ */ namespace Divergence\CLI\Controllers\Commands; -use Divergence\CLI\Command; use Divergence\CLI\Env; +use Divergence\CLI\Command; class Basics { @@ -24,24 +24,24 @@ public static function status() { $climate = Command::getClimate(); - if(!Env::$hasComposer) { + if (!Env::$hasComposer) { $climate->backgroundYellow()->black()->out("No composer.json detected."); return; } - $climate->info(sprintf('Found %s',Env::$package['name'])); + $climate->info(sprintf('Found %s', Env::$package['name'])); - if(!Env::$isRequired && !Env::$isRequireDev) { + if (!Env::$isRequired && !Env::$isRequireDev) { $climate->backgroundYellow()->black()->out('Did not find divergence/divergence in composer.json require'); $climate->backgroundYellow()->black()->out('Run divergence init to bootstrap your project'); return; } - if(Env::$isRequired) { + if (Env::$isRequired) { $climate->info('Found divergence/divergence in composer.json require'); } - if(Env::$isRequireDev) { + if (Env::$isRequireDev) { $climate->info('Found divergence/divergence in composer.json require-dev'); } } @@ -67,4 +67,4 @@ public static function usage() $climate->out("\ttest [subcommand]\t\t"); $climate->out("\t dbconfig\tChecks if DB config works. Asks you to choose a label name or provide one as the next argument."); } -} \ No newline at end of file +} diff --git a/src/Controllers/Commands/Database.php b/src/Controllers/Commands/Database.php index b31a410..928cd2c 100644 --- a/src/Controllers/Commands/Database.php +++ b/src/Controllers/Commands/Database.php @@ -9,9 +9,9 @@ */ namespace Divergence\CLI\Controllers\Commands; -use Divergence\CLI\Command; -use Divergence\CLI\Env; use PDO; +use Divergence\CLI\Env; +use Divergence\CLI\Command; class Database { @@ -23,13 +23,13 @@ public static function wizard($config) $climate->info('For your reference the config is:'); /* do not remove */ - dump($config); + dump($config); $retest = true; do { // retest config until success or abort $climate->inline('Testing config.......... '); $valid = static::connectionTester($config); - if($valid) { + if ($valid) { $climate->green('Success.'); return $config; } else { @@ -38,10 +38,10 @@ public static function wizard($config) $input->defaultTo('y'); $retest = $input->confirmed(); } - } while($retest); + } while ($retest); $input = $climate->confirm('Continue with untested config?'); $input->defaultTo('n'); - if($input->confirmed()) { + if ($input->confirmed()) { $valid = true; // human override } } while (!$valid); @@ -58,34 +58,34 @@ public static function configBuilder($defaults) $new = []; - $suggestedName = explode('/',Env::$package['name'])[1]; + $suggestedName = explode('/', Env::$package['name'])[1]; // hostname or socket - $input = $climate->input(sprintf('Hostname (You can also provide a socket) [%s]',$defaults['host'])); + $input = $climate->input(sprintf('Hostname (You can also provide a socket) [%s]', $defaults['host'])); $input->defaultTo($defaults['host']); $new['host'] = $input->prompt(); - if(substr($new['host'], -5) === '.sock') { // if ends with .sock treat as socket + if (substr($new['host'], -5) === '.sock') { // if ends with .sock treat as socket $new['socket'] = $new['host']; unset($new['host']); } // database name - $input = $climate->input(sprintf('Database name [%s]',$suggestedName)); + $input = $climate->input(sprintf('Database name [%s]', $suggestedName)); $input->defaultTo($suggestedName); $new['database'] = $input->prompt(); // database username - $input = $climate->input(sprintf('Database username [%s]',$suggestedName)); + $input = $climate->input(sprintf('Database username [%s]', $suggestedName)); $input->defaultTo($suggestedName); $new['username'] = $input->prompt(); // database password $input = $climate->input('Database password: '); $new['password'] = $input->prompt(); - if(!$new['password']) { + if (!$new['password']) { $input = $climate->confirm('Generate one?'); $input->defaultTo('y'); - if($input->confirmed()) { + if ($input->confirmed()) { $new['password'] = static::createPassword(); } } @@ -115,13 +115,13 @@ public static function connectionTester($config) /* * Uses ascii table chars decimal 33 (!) -> 126 (~) * covers basic symbols and letters - */ + */ public static function createPassword($length=20) { $password = ''; - while(strlen($password)<$length) { - $password .= chr(mt_rand(33,126)); + while (strlen($password)<$length) { + $password .= chr(mt_rand(33, 126)); } return $password; } -} \ No newline at end of file +} diff --git a/src/Controllers/Commands/Initialize.php b/src/Controllers/Commands/Initialize.php index 6b960d6..60fa683 100644 --- a/src/Controllers/Commands/Initialize.php +++ b/src/Controllers/Commands/Initialize.php @@ -10,8 +10,8 @@ namespace Divergence\CLI\Controllers\Commands; use Divergence\App; -use Divergence\CLI\Command; use Divergence\CLI\Env; +use Divergence\CLI\Command; use Divergence\CLI\Controllers\Commands\Database; /* @@ -19,25 +19,26 @@ * @author Henry Paradiz */ -class Initialize { +class Initialize +{ public static function init() { $climate = Command::getClimate(); - if(!Env::$hasComposer) { + if (!Env::$hasComposer) { $climate->backgroundYellow()->black()->out("No composer.json detected."); $climate->backgroundYellow()->black()->out('Run composer init first!'); return; } - if(Env::$isRequired) { + if (Env::$isRequired) { $climate->info('divergence/divergence is already in your composer.json require.'); } else { $climate->info('divergence/divergence is not in your composer.json require.'); $input = $climate->confirm('Do you want to run composer require divergence/divergence for this project?'); $input->defaultTo('y'); - if($input->confirmed()) { + if ($input->confirmed()) { shell_exec("composer require divergence/divergence --ansi"); Env::getEnvironment(); // force recheck } @@ -72,30 +73,30 @@ public static function initDirectories() 'views/dwoo/design.tpl', ]; - foreach($requiredFiles as $file) { - if(!file_exists(getcwd().'/'.$file)) { + foreach ($requiredFiles as $file) { + if (!file_exists(getcwd().'/'.$file)) { $climate->error($file.' missing.'); } else { $freshInstall = false; } } - if($freshInstall) { + if ($freshInstall) { $climate->info('Looks like this is a fresh install'); $input = $climate->confirm('Do you want to bootstrap this project with framework defaults?'); $input->defaultTo('y'); - if($input->confirmed()) { + if ($input->confirmed()) { $climate->info('Creating directories...'); - foreach($requiredFiles as $file) { + foreach ($requiredFiles as $file) { $source = 'vendor/divergence/divergence/'.$file; $dest = getcwd().'/'.$file; - if(!file_exists(dirname($dest))) { - mkdir(dirname($dest),0777,true); + if (!file_exists(dirname($dest))) { + mkdir(dirname($dest), 0777, true); } $climate->info($dest); - copy($source,$dest); + copy($source, $dest); } $freshInstall = false; } @@ -105,24 +106,24 @@ public static function initDirectories() } /* - * + * * Adds a PSR-4 namespace to composer.json * Asks if you want to run `composer install` and runs it * Runs Env::getEnvironment() once to make sure it was succesful */ public static function installAutoloader() { - $suggestedName = explode('/',Env::$package['name'])[1]; + $suggestedName = explode('/', Env::$package['name'])[1]; $input = $climate->confirm('Do you want to create a namespace called '.$suggestedName.' mapped to directory src?'); $input->defaultTo('y'); - if($input->confirmed()) { + if ($input->confirmed()) { Env::$package['autoload']['psr-4'][$suggestedName."\\"] = 'src/'; - Env::setPKG(getcwd(),Env::$package); + Env::setPKG(getcwd(), Env::$package); Env::getEnvironment(); $input = $climate->confirm('Run composer install to register new autoloaded folder?'); $input->defaultTo('y'); - if($input->confirmed()) { + if ($input->confirmed()) { shell_exec('composer install --ansi'); } } @@ -132,7 +133,7 @@ public static function installAutoloader() * Checks autoloader config for a namespace to use. * Installs one if none found. * Asks to use if only one found. - * TODO: prompt to select if more than + * TODO: prompt to select if more than * Sets Env::$namespace once it's found. */ public static function initAutoloader() @@ -141,25 +142,24 @@ public static function initAutoloader() $autoloaders = Env::$autoloaders; - if(!count($autoloaders)) { + if (!count($autoloaders)) { $climate->info('No local autoloaded directory found!'); static::installAutoloader(); $autoloaders = Env::$autoloaders; } // if only one autoloader ask if we should use this to initialize - if(count($autoloaders) === 1) { + if (count($autoloaders) === 1) { $key = array_keys($autoloaders)[0]; - $climate->info(sprintf('Found a single autoloaded namespace: %s => loaded from ./%s',$key,$autoloaders[$key])); + $climate->info(sprintf('Found a single autoloaded namespace: %s => loaded from ./%s', $key, $autoloaders[$key])); $input = $climate->confirm('Initialize at this namespace?'); $input->defaultTo('y'); - if($input->confirmed()) { + if ($input->confirmed()) { Env::$namespace = $key; } - } - elseif(count($autoloaders) > 1) { + } elseif (count($autoloaders) > 1) { // prompt: found count($autoloaders) autoloader configs. Which one is your namespace? } } @@ -173,18 +173,18 @@ public static function initDatabase() $climate = Command::getClimate(); $config = App::config('db'); $defaults = require getcwd().'/vendor/divergence/divergence/config/db.php'; - foreach($config as $label=>$dbconf) { - if($dbconf === $defaults[$label]) { - if(in_array($label,['mysql','dev-mysql'])) { - $climate->info(sprintf('Detected default database config %s',$label)); + foreach ($config as $label=>$dbconf) { + if ($dbconf === $defaults[$label]) { + if (in_array($label, ['mysql','dev-mysql'])) { + $climate->info(sprintf('Detected default database config %s', $label)); $input = $climate->confirm('Build database config '.$label.'?'); $input->defaultTo('y'); $thisConfig = $defaults[$label]; // use default - if($input->confirmed()) { + if ($input->confirmed()) { Database::wizard($thisConfig); } } } // if } // foreach } -} \ No newline at end of file +} diff --git a/src/Controllers/Commands/Tester.php b/src/Controllers/Commands/Tester.php index 3caaf60..ededc03 100644 --- a/src/Controllers/Commands/Tester.php +++ b/src/Controllers/Commands/Tester.php @@ -10,15 +10,15 @@ namespace Divergence\CLI\Controllers\Commands; use Divergence\App; -use Divergence\CLI\Command; use Divergence\CLI\Env; +use Divergence\CLI\Command; use Divergence\CLI\Controllers\CommandLineHandler; class Tester extends CommandLineHandler { public static function handle() { - switch($action = static::shiftArgs()) { + switch ($action = static::shiftArgs()) { case 'dbconfig': static::dbconfig(); break; @@ -33,21 +33,22 @@ public static function error($error) Command::$climate->error($error); } - public static function dbconfig() { + public static function dbconfig() + { $climate = Command::$climate; $configs = App::config('db'); $labels = array_keys($configs); - if(!$label = static::shiftArgs()) { + if (!$label = static::shiftArgs()) { $input = $climate->radio('Choose a config to test:', $labels); $response = $input->prompt(); - if(in_array($response,$labels)) { + if (in_array($response, $labels)) { static::testDatabaseConfig($configs[$response]); } } else { - if(in_array($label,$labels)) { + if (in_array($label, $labels)) { static::testDatabaseConfig($configs[$label]); } else { $climate->yellow('No database config found with that label.'); @@ -55,13 +56,14 @@ public static function dbconfig() { } } - public static function testDatabaseConfig($config) { + public static function testDatabaseConfig($config) + { $climate = Command::$climate; $climate->inline('Testing config.......... '); - if(Database::connectionTester($config)) { + if (Database::connectionTester($config)) { $climate->green('Success.'); } else { $climate->red('Failed.'); } } -} \ No newline at end of file +} diff --git a/src/Env.php b/src/Env.php index a212982..b4043ff 100644 --- a/src/Env.php +++ b/src/Env.php @@ -11,7 +11,8 @@ use Divergence\App; -class Env { +class Env +{ public static $me; // the command used to launch this binary public static $self; // json data from composer.json for \Divergence\Cli public static $package; // json data from composer.json for the folder from where you launched this binary @@ -25,13 +26,13 @@ class Env { public static function findComposerJSON($path) { - while(!file_exists($path.'/composer.json')) { + while (!file_exists($path.'/composer.json')) { $path = dirname($path); - if($path == '/') { + if ($path == '/') { return false; // no composer file } } - if(file_exists($path.'/composer.json')) { + if (file_exists($path.'/composer.json')) { return $path.'/composer.json'; } return false; @@ -39,33 +40,35 @@ public static function findComposerJSON($path) public static function getPKG($path) { - if($composer = static::findComposerJSON($path)) { - return json_decode(file_get_contents($composer),true); + if ($composer = static::findComposerJSON($path)) { + return json_decode(file_get_contents($composer), true); } } - public static function setPKG($path,$json) { - if($composer = static::findComposerJSON($path)) { + public static function setPKG($path, $json) + { + if ($composer = static::findComposerJSON($path)) { file_put_contents($composer, json_encode($json, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)); } } - public static function getEnvironment() { + public static function getEnvironment() + { static::$self = static::getPKG(__DIR__); // this gets the composer.json info for Divergence\CLI no matter where you ran the binary from static::$package = static::getPKG(getcwd()); // this gets the composer.json info for the directory from which you ran the binary - if(static::$package) { + if (static::$package) { static::$hasComposer = true; } - if(in_array('divergence/divergence',array_keys(static::$package['require']))) { + if (in_array('divergence/divergence', array_keys(static::$package['require']))) { static::$isRequired = true; - } - if(in_array('divergence/divergence',array_keys(static::$package['require-dev']))) { + } + if (in_array('divergence/divergence', array_keys(static::$package['require-dev']))) { static::$isRequireDev = true; } - if(!static::$package['autoload']) { + if (!static::$package['autoload']) { static::$namespace = null; } else { static::$autoloaders = static::getAutoloaders(); @@ -75,14 +78,15 @@ public static function getEnvironment() { error_reporting(E_ALL ^E_WARNING ^E_NOTICE); // Fix error reporting cause App::init acts like it's in production } - public static function getAutoloaders() { + public static function getAutoloaders() + { $autoloaders = []; - if(static::$package['autoload']['psr-4']) { - $autoloaders = array_merge($autoloaders,Env::$package['autoload']['psr-4']); + if (static::$package['autoload']['psr-4']) { + $autoloaders = array_merge($autoloaders, Env::$package['autoload']['psr-4']); } - if(static::$package['autoload']['psr-0']) { - $autoloaders = array_merge($autoloaders,Env::$package['autoload']['psr-4']); + if (static::$package['autoload']['psr-0']) { + $autoloaders = array_merge($autoloaders, Env::$package['autoload']['psr-4']); } return $autoloaders; } -} \ No newline at end of file +}