• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / cli / 9850825089

13 May 2024 08:15PM UTC coverage: 92.021%. Remained the same
9850825089

push

github

natanfelles
Allows showing commands separated by groups

31 of 31 new or added lines in 1 file covered. (100.0%)

10 existing lines in 1 file now uncovered.

519 of 564 relevant lines covered (92.02%)

5.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.01
/src/Commands/Index.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework CLI Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\CLI\Commands;
11

12
use Framework\CLI\CLI;
13
use Framework\CLI\Command;
14
use Framework\CLI\Styles\ForegroundColor;
15

16
/**
17
 * Class Index.
18
 *
19
 * @package cli
20
 */
21
class Index extends Command
22
{
23
    protected string $name = 'index';
24
    protected string $description = 'Show commands list';
25
    protected string $usage = 'index';
26
    protected array $options = [
27
        '-g' => 'Shows greeting.',
28
    ];
29

30
    public function run() : void
31
    {
32
        $this->showHeader();
4✔
33
        $this->showDate();
4✔
34
        if ($this->console->getOption('g')) {
4✔
35
            $this->greet();
1✔
36
        }
37
        $this->listCommands();
4✔
38
    }
39

40
    public function getDescription() : string
41
    {
42
        return $this->console->getLanguage()->render('cli', 'index.description');
5✔
43
    }
44

45
    public function getOptions() : array
46
    {
47
        return [
1✔
48
            '-g' => $this->console->getLanguage()->render('cli', 'index.option.greet'),
1✔
49
        ];
1✔
50
    }
51

52
    protected function listCommands() : void
53
    {
54
        $groupDefault = [];
4✔
55
        $groups = [];
4✔
56
        foreach ($this->console->getCommands() as $name => $command) {
4✔
57
            $group = $command->getGroup();
4✔
58
            if ($group === null) {
4✔
59
                $groupDefault[$name] = $command;
4✔
60
                continue;
4✔
61
            }
62
            $groups[$group][$name] = $command;
1✔
63
        }
64
        CLI::write(
4✔
65
            $this->console->getLanguage()->render('cli', 'availableCommands') . ':',
4✔
66
            ForegroundColor::yellow
4✔
67
        );
4✔
68
        [$width, $lengths] = $this->getWidthAndLengths($groupDefault);
4✔
69
        foreach ($groupDefault as $name => $command) {
4✔
70
            CLI::write(
4✔
71
                '  ' . CLI::style($name, ForegroundColor::green) . '  '
4✔
72
                // @phpstan-ignore-next-line
4✔
73
                . \str_repeat(' ', $width - $lengths[$name])
4✔
74
                . $command->getDescription()
4✔
75
            );
4✔
76
        }
77
        \ksort($groups);
4✔
78
        foreach ($groups as $groupName => $commands) {
4✔
79
            CLI::newLine();
1✔
80
            CLI::write(' ' . $groupName . ':', ForegroundColor::brightYellow);
1✔
81
            [$width, $lengths] = $this->getWidthAndLengths($commands);
1✔
82
            foreach ($commands as $name => $command) {
1✔
83
                CLI::write(
1✔
84
                    '  ' . CLI::style($name, ForegroundColor::green) . '  '
1✔
85
                    // @phpstan-ignore-next-line
1✔
86
                    . \str_repeat(' ', $width - $lengths[$name])
1✔
87
                    . $command->getDescription()
1✔
88
                );
1✔
89
            }
90
        }
91
    }
92

93
    /**
94
     * @param array<string,Command> $commands
95
     *
96
     * @return array<array<string,int>|int>
97
     */
98
    protected function getWidthAndLengths(array $commands) : array
99
    {
100
        $width = 0;
4✔
101
        $lengths = [];
4✔
102
        foreach (\array_keys($commands) as $name) {
4✔
103
            $lengths[$name] = \mb_strlen($name);
4✔
104
            if ($lengths[$name] > $width) {
4✔
105
                $width = $lengths[$name];
4✔
106
            }
107
        }
108
        return [$width, $lengths];
4✔
109
    }
110

111
    protected function showHeader() : void
112
    {
113
        $text = <<<'EOL'
4✔
114
                _          _              ____ _     ___
115
               / \   _ __ | |_   _ ___   / ___| |   |_ _|
116
              / _ \ | '_ \| | | | / __| | |   | |    | |
117
             / ___ \| |_) | | |_| \__ \ | |___| |___ | |
118
            /_/   \_\ .__/|_|\__,_|___/  \____|_____|___|
119
                    |_|
120

121
            EOL;
4✔
122
        CLI::write($text, ForegroundColor::green);
4✔
123
    }
124

125
    protected function showDate() : void
126
    {
127
        $text = $this->console->getLanguage()->date(\time(), 'full');
4✔
128
        $text = \ucfirst($text) . ' - '
4✔
129
            . \date('H:i:s') . ' - '
4✔
130
            . \date_default_timezone_get() . \PHP_EOL;
4✔
131
        CLI::write($text);
4✔
132
    }
133

134
    protected function greet() : void
135
    {
136
        $hour = \date('H');
1✔
137
        $timing = 'evening';
1✔
138
        if ($hour > 4 && $hour < 12) {
1✔
UNCOV
139
            $timing = 'morning';
×
140
        } elseif ($hour > 4 && $hour < 18) {
1✔
UNCOV
141
            $timing = 'afternoon';
×
142
        }
143
        $greeting = $this->console->getLanguage()
1✔
144
            ->render('cli', 'greet.' . $timing, [$this->getUser()]);
1✔
145
        CLI::write($greeting);
1✔
146
        CLI::newLine();
1✔
147
    }
148

149
    protected function getUser() : string
150
    {
151
        $username = \posix_getlogin();
1✔
152
        if ($username === false) {
1✔
153
            return $this->console->getLanguage()->render('cli', 'friend');
1✔
154
        }
UNCOV
155
        $info = \posix_getpwnam($username);
×
UNCOV
156
        if (!$info) {
×
UNCOV
157
            return $username;
×
158
        }
UNCOV
159
        $gecos = $info['gecos'] ?? '';
×
UNCOV
160
        if (!$gecos) {
×
UNCOV
161
            return $username;
×
162
        }
UNCOV
163
        $length = \strpos($gecos, ',') ?: \strlen($gecos);
×
UNCOV
164
        return \substr($gecos, 0, $length);
×
165
    }
166
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc