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

Jagepard / Rudra-Cli / 27121675515

08 Jun 2026 07:09AM UTC coverage: 86.842% (-4.6%) from 91.429%
27121675515

push

github

web-flow
Merge pull request #46 from Jagepard/wip

fix(Console): improve error messages for missing commands and fix typo

3 of 5 new or added lines in 1 file covered. (60.0%)

33 of 38 relevant lines covered (86.84%)

2.21 hits per line

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

86.84
/src/Console.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * This Source Code Form is subject to the terms of the Mozilla Public
5
 * License, v. 2.0. If a copy of the MPL was not distributed with this
6
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
 *
8
 * @author  Korotkov Danila (Jagepard) <jagepard@yandex.ru>
9
 * @license https://mozilla.org/MPL/2.0/  MPL-2.0
10
 */
11

12
namespace Rudra\Cli;
13

14
use Rudra\Exceptions\LogicException;
15

16
class Console implements ConsoleInterface
17
{
18
    // Colors of text decoration in the console
19
    public const array COLOR = [
20
        "default"       => 39,
21
        "black"         => 30,
22
        "red"           => 31,
23
        "green"         => 32,
24
        "yellow"        => 33,
25
        "blue"          => 34,
26
        "magneta"       => 35,
27
        "cyan"          => 36,
28
        "light_gray"    => 37,
29
        "dark_gray"     => 90,
30
        "light_red"     => 91,
31
        "light_green"   => 92,
32
        "light_yellow"  => 93,
33
        "light_blue"    => 94,
34
        "light_magneta" => 95,
35
        "light_cyan"    => 96,
36
        "white"         => 97,
37
    ];
38

39
    private array $registry = [];
40

41
    /** @var resource|null */
42
    private mixed $stdin    = null;
43

44
    /**
45
     * @throws \InvalidArgumentException
46
     */
47
    public function setStdin(mixed $stream): void
3✔
48
    {
49
        if ($stream !== null && !is_resource($stream)) {
3✔
50
            throw new \InvalidArgumentException('Argument #1 ($stream) must be of type resource or null');
×
51
        }
52
        
53
        $this->stdin = $stream;
3✔
54
    }
55

56
    /**
57
     * Prints formatted text with foreground and background colors
58
     */
59
    #[\Override]
60
    public function printer(string $text, string $fg = "default", string $bg = "default"): void
4✔
61
    {
62
        $this->checkColorExists($fg);
4✔
63
        $this->checkColorExists($bg);
4✔
64

65
        $fgCode = self::COLOR[$fg];
4✔
66
        $bgCode = self::COLOR[$bg] + 10;
4✔
67

68
        echo "\e[{$fgCode};{$bgCode}m{$text}\e[0m";
4✔
69
    }
70

71
    /**
72
     * Get the data entered in the console
73
     * 
74
     * @throws LogicException
75
     */
76
    #[\Override]
77
    public function reader(): string
3✔
78
    {
79
        $this->stdin ??= fopen("php://stdin", "r");
3✔
80
        
81
        if ($this->stdin === false) {
3✔
82
            throw new LogicException('Failed to open stdin stream');
×
83
        }
84
        
85
        $result = fgets($this->stdin);
3✔
86

87
        if ($result === false) {
3✔
88
            throw new LogicException('Failed to read from stdin or EOF reached');
×
89
        }
90

91
        return $result;
3✔
92
    }
93

94
    /**
95
     * @throws LogicException
96
     */
97
    #[\Override]
98
    public function addCommand(string $name, array $command): void
2✔
99
    {
100
        if (array_key_exists($name, $this->registry)) {
2✔
101
            throw new LogicException("Command $name already exists");
1✔
102
        }
103
        
104
        $this->registry[$name] = $command;
2✔
105
    }
106

107
    /**
108
     * Calls command methods
109
     */
110
    #[\Override]
111
    public function invoke(array $inputArgs): void
2✔
112
    {
113
        $firstKey = array_key_first($inputArgs);
2✔
114

115
        if ($firstKey === null) {
2✔
NEW
116
            $this->printer("⚠️  No command provided" . PHP_EOL, 'light_yellow');
×
NEW
117
            return;
×
118
        }
119

120
        if (!array_key_exists($firstKey, $this->registry)) {
2✔
121
            $this->printer("⚠️  Command \"$firstKey\" not found" . PHP_EOL, 'light_yellow');
1✔
122
            return;
1✔
123
        }
124

125
        $class  = new $this->registry[$firstKey][0];
1✔
126
        $method = $this->registry[$firstKey][1] ?? "actionIndex";
1✔
127

128
        $class->$method();
1✔
129
    }
130

131
    /**
132
     * @return array<string, array>
133
     */
134
    #[\Override]
135
    public function getRegistry(): array
1✔
136
    {
137
        return $this->registry;
1✔
138
    }
139

140
    /**
141
     * @throws LogicException
142
     */
143
    private function checkColorExists(string $key): void
5✔
144
    {
145
        if (!array_key_exists($key, self::COLOR)) {
5✔
146
            throw new LogicException("Color $key doesn't exist");
1✔
147
        }
148
    }
149
}
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