Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
lotfio committed Jul 22, 2020
1 parent cd58324 commit fbdffc0
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 79 deletions.
3 changes: 1 addition & 2 deletions commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*
* @copyright 2019 Lotfio Lakehal
*/

$conso->command('command', Conso\Commands\Command::class);
$conso->command('command', Conso\Commands\Command::class);
9 changes: 3 additions & 6 deletions src/Conso/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
31 changes: 19 additions & 12 deletions src/Conso/CommandInvoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
*
Expand All @@ -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.");
Expand Down
34 changes: 19 additions & 15 deletions src/Conso/CommandLinker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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];
}
Expand All @@ -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;
Expand Down
13 changes: 8 additions & 5 deletions src/Conso/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

/**
* @author <[email protected]>
*
* @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
{
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
5 changes: 2 additions & 3 deletions src/Conso/Conso.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/Conso/ConsoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
10 changes: 4 additions & 6 deletions src/Conso/Contracts/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 2 additions & 3 deletions src/Conso/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
37 changes: 19 additions & 18 deletions src/Conso/hlprs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
}
foreach ($value as $a => $b) {
$output->writeLn(' '.$a.' : '.$b."\n\n");
}
}
}
}
8 changes: 4 additions & 4 deletions tests/Unit/CommandInvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Mocks/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

use Conso\Command as BaseCommand;
use Conso\Conso;
use Conso\Contracts\CommandInterface;
use Conso\Contracts\InputInterface;
use Conso\Contracts\OutputInterface;
Expand Down Expand Up @@ -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';
}
Expand Down

0 comments on commit fbdffc0

Please sign in to comment.