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

aplus-framework / cli / 12592625499

07 Nov 2024 10:29PM UTC coverage: 92.215%. Remained the same
12592625499

push

github

natanfelles
Make sure there is a dot at the end of the description

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

9 existing lines in 1 file now uncovered.

533 of 578 relevant lines covered (92.21%)

5.44 hits per line

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

87.65
/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
                . $this->editDescription($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
                    . $this->editDescription($command->getDescription())
1✔
88
                );
1✔
89
            }
90
        }
91
    }
92

93
    protected function editDescription(string $description) : string
94
    {
95
        $description = \trim($description);
4✔
96
        if (!\str_ends_with($description, '.')) {
4✔
97
            $description .= '.';
1✔
98
        }
99
        return $description;
4✔
100
    }
101

102
    /**
103
     * @param array<string,Command> $commands
104
     *
105
     * @return array<array<string,int>|int>
106
     */
107
    protected function getWidthAndLengths(array $commands) : array
108
    {
109
        $width = 0;
4✔
110
        $lengths = [];
4✔
111
        foreach (\array_keys($commands) as $name) {
4✔
112
            $lengths[$name] = \mb_strlen($name);
4✔
113
            if ($lengths[$name] > $width) {
4✔
114
                $width = $lengths[$name];
4✔
115
            }
116
        }
117
        return [$width, $lengths];
4✔
118
    }
119

120
    protected function showHeader() : void
121
    {
122
        $text = <<<'EOL'
4✔
123
                _          _              ____ _     ___
124
               / \   _ __ | |_   _ ___   / ___| |   |_ _|
125
              / _ \ | '_ \| | | | / __| | |   | |    | |
126
             / ___ \| |_) | | |_| \__ \ | |___| |___ | |
127
            /_/   \_\ .__/|_|\__,_|___/  \____|_____|___|
128
                    |_|
129

130
            EOL;
4✔
131
        CLI::write($text, ForegroundColor::green);
4✔
132
    }
133

134
    protected function showDate() : void
135
    {
136
        $text = $this->console->getLanguage()->date(\time(), 'full');
4✔
137
        $text = \ucfirst($text) . ' - '
4✔
138
            . \date('H:i:s') . ' - '
4✔
139
            . \date_default_timezone_get() . \PHP_EOL;
4✔
140
        CLI::write($text);
4✔
141
    }
142

143
    protected function greet() : void
144
    {
145
        $hour = \date('H');
1✔
146
        $timing = 'evening';
1✔
147
        if ($hour > 4 && $hour < 12) {
1✔
UNCOV
148
            $timing = 'morning';
×
149
        } elseif ($hour > 4 && $hour < 18) {
1✔
UNCOV
150
            $timing = 'afternoon';
×
151
        }
152
        $greeting = $this->console->getLanguage()
1✔
153
            ->render('cli', 'greet.' . $timing, [$this->getUser()]);
1✔
154
        CLI::write($greeting);
1✔
155
        CLI::newLine();
1✔
156
    }
157

158
    protected function getUser() : string
159
    {
160
        $username = \posix_getlogin();
1✔
161
        if ($username === false) {
1✔
162
            return $this->console->getLanguage()->render('cli', 'friend');
1✔
163
        }
UNCOV
164
        $info = \posix_getpwnam($username);
×
UNCOV
165
        if (!$info) {
×
UNCOV
166
            return $username;
×
167
        }
UNCOV
168
        $gecos = $info['gecos'] ?? '';
×
UNCOV
169
        if (!$gecos) {
×
UNCOV
170
            return $username;
×
171
        }
UNCOV
172
        $length = \strpos($gecos, ',') ?: \strlen($gecos);
×
173
        return \substr($gecos, 0, $length);
×
174
    }
175
}
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

© 2025 Coveralls, Inc