ConsoleDefault::showSystemBanner()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
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 Webino;
14
use WebinoAppLib\Event\ConsoleEvent;
15
use WebinoConfigLib\Feature\Route\ConsoleRoute;
16
17
/**
18
 * Class ConsoleDefault
19
 */
20
class ConsoleDefault extends AbstractConsoleCommand
21
{
22
    /**
23
     * @inheritDoc
24
     */
25
    protected function init()
26
    {
27
        parent::init();
28
        $this->listenConsole(ConsoleEvent::ROUTE_MATCH, 'showSystemBanner');
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function configure(ConsoleRoute $console)
35
    {
36
        $console->setType('catchall');
37
    }
38
39
    /**
40
     * @param ConsoleEvent $event
41
     */
42
    public function showSystemBanner(ConsoleEvent $event)
43
    {
44
        $cli = $event->getCli();
45
46
        $cli
47
            ->br()
48
            ->white()->greenBg()->inline(' Webino™ ')
49
            ->inline(' version ')
50
            ->yellow()->out(Webino::VERSION)
51
            ->br();
52
    }
53
54
    /**
55
     * @param ConsoleEvent $event
56
     */
57
    public function handle(ConsoleEvent $event)
58
    {
59
        $cfg = $event->getApp()->getConfig('console');
60
        if (empty($cfg->router->routes)) {
61
            return;
62
        }
63
64
        $routes    = $cfg->router->routes;
65
        $maxLen    = 0;
66
        $newRoutes = [];
67
68
        foreach ($routes as $route) {
69
            if ('catchall' === $route->type) {
70
                continue;
71
            }
72
73
            $routeLen = strlen($this->resolveRouteCommand($route->options->route));
74
            $routeLen > $maxLen and $maxLen = $routeLen;
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...
75
76
            $newRoutes[$route->options->route] = $route;
77
        }
78
79
        $cli = $event->getCli();
80
81
        ksort($newRoutes);
82
        foreach ($newRoutes as $route) {
83
84
            $command = $this->resolveRouteCommand($route->options->route);
85
            $title = isset($route->options->defaults->title)
86
                ? $route->options->defaults->title
87
                : null;
88
89
            $cli->bold()->green()->inline($command);
0 ignored issues
show
Security Bug introduced by
It seems like $command defined by $this->resolveRouteComma...$route->options->route) on line 84 can also be of type false; however, WebinoAppLib\Service\Console::inline() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
90
91
            if (empty($title)) {
92
                $cli->br();
93
                continue;
94
            }
95
96
            $space = str_repeat(' ', $maxLen + 10 - strlen($command));
97
            $cli->out($space . ' - ' . $title);
98
        }
99
100
        $cli->br();
101
    }
102
103
    /**
104
     * Return console route command name
105
     *
106
     * @param string $route
107
     * @return string
108
     */
109
    private function resolveRouteCommand($route)
110
    {
111
        $matches = [];
112
        preg_match('~^[a-zA-Z:_-]+~', $route, $matches);
113
        return current($matches);
114
    }
115
}
116