From fbdffc041b81514a11ea55098f9062e711086355 Mon Sep 17 00:00:00 2001 From: lotfio lakehal Date: Wed, 22 Jul 2020 22:54:51 +0000 Subject: [PATCH] Apply fixes from StyleCI --- commands.php | 3 +- src/Conso/Command.php | 9 ++---- src/Conso/CommandInvoker.php | 31 ++++++++++++-------- src/Conso/CommandLinker.php | 34 ++++++++++++---------- src/Conso/Commands/Command.php | 13 +++++---- src/Conso/Conso.php | 5 ++-- src/Conso/ConsoTrait.php | 7 +++-- src/Conso/Contracts/CommandInterface.php | 10 +++---- src/Conso/Input.php | 5 ++-- src/Conso/hlprs.php | 37 ++++++++++++------------ tests/Unit/CommandInvokerTest.php | 8 ++--- tests/Unit/Mocks/Make.php | 3 +- 12 files changed, 86 insertions(+), 79 deletions(-) diff --git a/commands.php b/commands.php index 8c5a032..4bd1cea 100644 --- a/commands.php +++ b/commands.php @@ -11,5 +11,4 @@ * * @copyright 2019 Lotfio Lakehal */ - -$conso->command('command', Conso\Commands\Command::class); \ No newline at end of file +$conso->command('command', Conso\Commands\Command::class); diff --git a/src/Conso/Command.php b/src/Conso/Command.php index 289e1c5..e0956c6 100644 --- a/src/Conso/Command.php +++ b/src/Conso/Command.php @@ -14,25 +14,22 @@ * @copyright 2019 Lotfio Lakehal */ -use Conso\Contracts\InputInterface; -use Conso\Contracts\OutputInterface; - /** * This class is base command class. */ class Command { /** - * app instance + * app instance. * * @var obj */ protected $app; /** - * base constructor + * base constructor. * - * @param Conso $app + * @param Conso $app */ public function __construct(Conso $app) { diff --git a/src/Conso/CommandInvoker.php b/src/Conso/CommandInvoker.php index 7277162..b1389c8 100644 --- a/src/Conso/CommandInvoker.php +++ b/src/Conso/CommandInvoker.php @@ -53,9 +53,9 @@ class CommandInvoker */ public function __construct(InputInterface $input, OutputInterface $output, Conso $app) { - $this->input = $input; + $this->input = $input; $this->output = $output; - $this->app = $app; + $this->app = $app; } /** @@ -94,9 +94,10 @@ public function showConsoleInformation(array $commands) } /** - * show console commands + * show console commands. * * @param array $commands + * * @return void */ public function showConsoleCommands(array $commands) @@ -126,23 +127,28 @@ public function invoke(?array $command) $commands = $this->app->getCommands(); // defined commands // disable ansi - if($this->input->flag(0) == '--no-ansi') $this->output->disableAnsi(); + if ($this->input->flag(0) == '--no-ansi') { + $this->output->disableAnsi(); + } // command help - if($this->input->command() && ($this->input->flag(0) == '-h' || $this->input->flag(0) == '--help')) - return commandHelp($command, $this->output); + if ($this->input->command() && ($this->input->flag(0) == '-h' || $this->input->flag(0) == '--help')) { + return commandHelp($command, $this->output); + } // version - if(($this->input->flag(0) == '-v' || $this->input->flag(0) == '--version')) + if (($this->input->flag(0) == '-v' || $this->input->flag(0) == '--version')) { return $this->output->writeLn("\n ".$this->app->getName().' version '.$this->app->getVersion()."\n", 'yellow'); + } - if(($this->input->flag(0) == '-c' || $this->input->flag(0) == '--commands')) + if (($this->input->flag(0) == '-c' || $this->input->flag(0) == '--commands')) { return $this->showConsoleCommands($commands); + } - if(is_null($this->input->command())) // no command - { + if (is_null($this->input->command())) { // no command $this->showConsoleInformation($commands); $this->showConsoleCommands($commands); + return; } @@ -165,11 +171,12 @@ public function invoke(?array $command) protected function invokeCallback(array $command) { $closure = \Closure::bind($command['action'], $this->app); + return call_user_func_array($closure, [$this->input, $this->output]); } /** - * invoke class method + * invoke class method. * * @param array $command * @@ -186,7 +193,7 @@ protected function invokeClassMethod(array $command) $method = ($subCommand !== null) ? $subCommand : 'execute'; - $obj = new $class($this->app); + $obj = new $class($this->app); if (!method_exists($obj, $method)) { throw new InvokerException("command method ($method) is not defined."); diff --git a/src/Conso/CommandLinker.php b/src/Conso/CommandLinker.php index 1240ea7..8a41fb8 100644 --- a/src/Conso/CommandLinker.php +++ b/src/Conso/CommandLinker.php @@ -18,7 +18,6 @@ use Conso\Contracts\OutputInterface; use Conso\Exceptions\InputException; - /** * This class is responsible of linking input command with * defined commands in commands table. @@ -60,27 +59,30 @@ public function __construct(InputInterface $input, OutputInterface $output) */ public function link(array &$commands): ?array { - $command = $this->input->command(); + $command = $this->input->command(); $subCommand = $this->input->subCommand(); - $flags = $this->input->flags(); + $flags = $this->input->flags(); // if command is a class for ($i = 0; $i < count($commands); $i++) { readCommandPropertiesFromClass($commands[$i]); } // fill array info - if ($command) { // if command for ($i = 0; $i < count($commands); $i++) { - if ($command == $commands[$i]['name'] || in_array($command, $commands[$i]['aliases'])) - { - if ($subCommand && !in_array($subCommand, $commands[$i]['sub']))// match sub commands + if ($command == $commands[$i]['name'] || in_array($command, $commands[$i]['aliases'])) { + if ($subCommand && !in_array($subCommand, $commands[$i]['sub'])) {// match sub commands + throw new InputException("sub command ({$subCommand}) is not defined."); + } + if (count($flags)) { // match flags + foreach ($flags as $flag) { + if (!in_array($flag, array_merge($commands[$i]['flags'], $this->input->reservedFlags()))) { // allow only defined and reserved flags - if (count($flags)) // match flags - foreach ($flags as $flag) - if (!in_array($flag, array_merge($commands[$i]['flags'], $this->input->reservedFlags()))) // allow only defined and reserved flags throw new InputException("flag ({$flag}) is not defined."); + } + } + } return $commands[$i]; } @@ -89,12 +91,14 @@ public function link(array &$commands): ?array throw new InputException("command ({$command}) is not defined."); } - if(!$command) - { - if (count($flags)) // no command link flags - foreach ($flags as $flag) - if(!in_array($flag, $this->input->reservedFlags())) + if (!$command) { + if (count($flags)) { // no command link flags + foreach ($flags as $flag) { + if (!in_array($flag, $this->input->reservedFlags())) { throw new InputException("flag ({$flag}) is not defined."); + } + } + } } return null; diff --git a/src/Conso/Commands/Command.php b/src/Conso/Commands/Command.php index fff8e08..447b8f2 100644 --- a/src/Conso/Commands/Command.php +++ b/src/Conso/Commands/Command.php @@ -4,19 +4,22 @@ /** * @author + * * @version 1.5.0 + * * @license MIT + * * @category CLI + * * @copyright 2019 Lotfio Lakehal */ use Conso\Command as BaseCommand; -use Conso\Conso; +use function Conso\commandHelp; use Conso\Contracts\CommandInterface; use Conso\Contracts\InputInterface; use Conso\Contracts\OutputInterface; use Conso\Exceptions\InputException; -use function Conso\commandHelp; class Command extends BaseCommand implements CommandInterface { @@ -59,7 +62,7 @@ class Command extends BaseCommand implements CommandInterface * * @return void */ - public function execute(InputInterface $input, OutputInterface $output) : void + public function execute(InputInterface $input, OutputInterface $output): void { commandHelp($this->app->invokedCommand, $output); } @@ -72,7 +75,7 @@ public function execute(InputInterface $input, OutputInterface $output) : void * * @return void */ - public function make(InputInterface $input, OutputInterface $output) : void + public function make(InputInterface $input, OutputInterface $output): void { $name = $input->option(0); @@ -107,7 +110,7 @@ public function make(InputInterface $input, OutputInterface $output) : void * * @return void */ - public function delete(InputInterface $input, OutputInterface $output) : void + public function delete(InputInterface $input, OutputInterface $output): void { $name = $input->option(0); diff --git a/src/Conso/Conso.php b/src/Conso/Conso.php index d897f6d..c97d92a 100644 --- a/src/Conso/Conso.php +++ b/src/Conso/Conso.php @@ -98,12 +98,11 @@ public function run(int $env = 0) $this->invokedCommand = $this->linker->link($this->getCommands()); return $this->invoker->invoke($this->invokedCommand); - } catch (\Exception $e) { + if ($this->output->isTestMode()) { // is test mode - if ($this->output->isTestMode()) // is test mode throw new \Exception($e); - + } $this->output->exception($e, $env); } } diff --git a/src/Conso/ConsoTrait.php b/src/Conso/ConsoTrait.php index 6a1b1a6..89fb180 100644 --- a/src/Conso/ConsoTrait.php +++ b/src/Conso/ConsoTrait.php @@ -182,14 +182,15 @@ public function getCommandsNamespace(): string } /** - * call additional command method + * call additional command method. * * @param string $command + * * @return void */ - public function call(string $command) : void + public function call(string $command): void { - $command = 'php ' . $command; + $command = 'php '.$command; passthru($command); } diff --git a/src/Conso/Contracts/CommandInterface.php b/src/Conso/Contracts/CommandInterface.php index 9151872..e146412 100644 --- a/src/Conso/Contracts/CommandInterface.php +++ b/src/Conso/Contracts/CommandInterface.php @@ -13,18 +13,16 @@ * * @copyright 2019 Lotfio Lakehal */ - -use Conso\Conso; - interface CommandInterface { /** * execute command method * this is main command method. * - * @param InputInterface $input - * @param OutputInterface $output + * @param InputInterface $input + * @param OutputInterface $output + * * @return void */ - public function execute(InputInterface $input, OutputInterface $output) : void; + public function execute(InputInterface $input, OutputInterface $output): void; } diff --git a/src/Conso/Input.php b/src/Conso/Input.php index c65e742..4244494 100644 --- a/src/Conso/Input.php +++ b/src/Conso/Input.php @@ -98,13 +98,12 @@ private function extract(array $argv): void // set command if no sub command if (isset($argv[0]) && !preg_match('/^[\-]{1,2}[A-z]+(\-[A-z]+)?$/', $argv[0])) { - $this->command = $argv[0] ?? null; // if command & sub command if (preg_match('/^([A-z]+)(\:)([A-z]+)$/', $argv[0])) { - $command = explode(':', $argv[0]); - $this->command = $command[0] ?? null; + $command = explode(':', $argv[0]); + $this->command = $command[0] ?? null; $this->subCommand = $command[1] ?? null; } diff --git a/src/Conso/hlprs.php b/src/Conso/hlprs.php index f5496d1..d0efc5f 100644 --- a/src/Conso/hlprs.php +++ b/src/Conso/hlprs.php @@ -79,26 +79,27 @@ function readCommandPropertiesFromClass(array &$command): void } /** -* display command help helper function -* -* @param array $command -* @return void -*/ + * display command help helper function. + * + * @param array $command + * + * @return void + */ function commandHelp(array $command, $output) // create a better method to display help { - $name = $command['name']; - $help = $command['help']; + $name = $command['name']; + $help = $command['help']; - $output->writeLn("\n help for [".$name."] command:\n\n", 'yellow'); - $output->writeLn(" php conso $name:{sub command} {options}\n\n"); + $output->writeLn("\n help for [".$name."] command:\n\n", 'yellow'); + $output->writeLn(" php conso $name:{sub command} {options}\n\n"); - if (is_array($help) && count($help) > 0) { - foreach ($help as $key => $value) { - $output->writeLn(' ['.$key."]\n\n", 'yellow'); + if (is_array($help) && count($help) > 0) { + foreach ($help as $key => $value) { + $output->writeLn(' ['.$key."]\n\n", 'yellow'); - foreach ($value as $a => $b) { - $output->writeLn(' '.$a.' : '.$b."\n\n"); - } - } - } -} \ No newline at end of file + foreach ($value as $a => $b) { + $output->writeLn(' '.$a.' : '.$b."\n\n"); + } + } + } +} diff --git a/tests/Unit/CommandInvokerTest.php b/tests/Unit/CommandInvokerTest.php index 0956183..9b2156f 100644 --- a/tests/Unit/CommandInvokerTest.php +++ b/tests/Unit/CommandInvokerTest.php @@ -67,10 +67,10 @@ public function setUp(): void */ public function testInvokeCallback() { - $this->commands[0]['action'] = function(){ return 'from make callback';}; + $this->commands[0]['action'] = function () { return 'from make callback'; }; - $inp = new Input('make'); - $app = new Conso($inp, $this->output); + $inp = new Input('make'); + $app = new Conso($inp, $this->output); $invoker = new CommandInvoker($inp, $this->output, $app); $this->assertEquals( @@ -86,7 +86,7 @@ public function testInvokeCallback() */ public function testInvokeClassMethod() { - $inp = new Input('make'); + $inp = new Input('make'); $invoker = new CommandInvoker($inp, $this->output, new Conso($inp, $this->output)); ob_start(); diff --git a/tests/Unit/Mocks/Make.php b/tests/Unit/Mocks/Make.php index c3d154a..beac700 100644 --- a/tests/Unit/Mocks/Make.php +++ b/tests/Unit/Mocks/Make.php @@ -15,7 +15,6 @@ */ use Conso\Command as BaseCommand; -use Conso\Conso; use Conso\Contracts\CommandInterface; use Conso\Contracts\InputInterface; use Conso\Contracts\OutputInterface; @@ -61,7 +60,7 @@ class Make extends BaseCommand implements CommandInterface * * @return void */ - public function execute(InputInterface $input, OutputInterface $output) : void + public function execute(InputInterface $input, OutputInterface $output): void { echo 'from make class command'; }