Completed
Push — prototype ( 3bfdd7...aec713 )
by Peter
07:47
created

ConsoleHelp::handle()   C

Complexity

Conditions 11
Paths 100

Size

Total Lines 62
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 41
nc 100
nop 1
dl 0
loc 62
rs 6.1722
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino for the canonical source repository
6
 * @copyright   Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoAppLib\Console;
12
13
use WebinoAppLib\Event\ConsoleEvent;
14
use WebinoConfigLib\Feature\Route\ConsoleRoute;
15
16
/**
17
 * Class ConsoleHelp
18
 */
19
class ConsoleHelp extends AbstractConsoleCommand
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function configure(ConsoleRoute $route)
25
    {
26
        $route
27
            ->setPath('help [<command>]')
28
            ->setTitle('Show command description');
29
    }
30
31
    /**
32
     * @param ConsoleEvent $event
33
     */
34
    public function handle(ConsoleEvent $event)
35
    {
36
        $cfg = $event->getApp()->getConfig('console');
37
        if (empty($cfg->router->routes)) {
38
            return;
39
        }
40
41
        $routes = $cfg->router->routes;
42
        $requiredCommand = $event->getParam('command', 'help');
43
44
        // find route
45
        $route = null;
46
        foreach ($routes as $item) {
47
            if (!empty($item->options) && 0 === strpos($item->options->route, $requiredCommand)) {
48
                $route = $item;
49
                break;
50
            }
51
        }
52
53
        if (empty($route)) {
54
            // command not found
55
            $event->getCli()->draw('404')->br();
56
            return;
57
        }
58
59
        $title = (string) $route->options->defaults->title ?? null;
60
        $description = (string) $route->options->defaults->description ?? null;
61
        $argumentsDescription = (array) $route->options->defaults->argumentsDescription ?? [];
62
        $optionsDescription = (array) $route->options->defaults->optionsDescription ?? [];
63
64
        $newArgumentsDescription = [];
65
        foreach ($argumentsDescription as $key => $value) {
66
            $newArgumentsDescription[" <green>$key</green>"] = '- ' . $value;
67
        }
68
69
        $newOptionsDescription = [];
70
        foreach ($optionsDescription as $key => $value) {
71
            $newOptionsDescription[" <green>--$key</green>"] = '- ' . $value;
72
        }
73
74
        $cli = $event->getCli();
75
76
        $cli
77
            ->invert()->bold(" $title ")->br()
78
            ->yellow()->bold('Usage:')
79
            ->white()->blackBg(" {$route->options->route} ")->br();
80
81
        $description
82
            and $cli
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
83
                ->yellow()->bold('Description:')
84
                ->out(' ' . str_replace(PHP_EOL, PHP_EOL . ' ', $description))->br();
85
86
        $newArgumentsDescription
87
            and $cli
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
88
                ->yellow()->bold('Arguments:')
89
                ->columns($newArgumentsDescription)->br();
90
91
        $newOptionsDescription
92
            and $cli
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
93
                ->yellow()->bold('Options:')
94
                ->columns($newOptionsDescription)->br();
95
    }
96
}
97