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

Jagepard / Rudra-Cli / 26520341004

27 May 2026 03:16PM UTC coverage: 93.939% (-6.1%) from 100.0%
26520341004

push

github

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

refactor(Cli/Console): PHP 8.4 compatibility

14 of 16 new or added lines in 1 file covered. (87.5%)

31 of 33 relevant lines covered (93.94%)

2.39 hits per line

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

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

41
    private array $registry = [];
42

43
    /** @var resource|null */
44
    private mixed $stdin    = null;
45

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

59
    /**
60
     * Prints formatted text with foreground and background colors.
61
     *
62
     * @param string $text Text to output
63
     * @param string $fg   Foreground color (key from self::COLOR)
64
     * @param string $bg   Background color (key from self::COLOR)
65
     * @return void
66
     */
67
    #[\Override]
68
    public function printer(string $text, string $fg = "default", string $bg = "default"): void
4✔
69
    {
70
        $this->checkColorExists($fg);
4✔
71
        $this->checkColorExists($bg);
4✔
72

73
        $fgCode = self::COLOR[$fg];
4✔
74
        $bgCode = self::COLOR[$bg] + 10;
4✔
75

76
        echo "\e[{$fgCode};{$bgCode}m{$text}\e[0m";
4✔
77
    }
78

79
    /**
80
     * Get the data entered in the console.
81
     *
82
     * @return string The input line or empty string on EOF/error.
83
     */
84
    #[\Override]
85
    public function reader(): string
3✔
86
    {
87
        $this->stdin ??= fopen("php://stdin", "r");
3✔
88
        $result = fgets($this->stdin);
3✔
89

90
        if ($result === false) {
3✔
NEW
91
            throw new LogicException('Failed to read from stdin or EOF reached');
×
92
        }
93

94
        return $result;
3✔
95
    }
96

97
    /**
98
     * Adds a command to the registry
99
     * 
100
     * @param string $name
101
     * @param array  $command
102
     * @return void
103
     */
104
    #[\Override]
105
    public function addCommand(string $name, array $command): void
2✔
106
    {
107
        if (array_key_exists($name, $this->registry)) {
2✔
108
            throw new LogicException("Command $name already exist");
1✔
109
        }
110
        
111
        $this->registry[$name] = $command;
2✔
112
    }
113

114
    /**
115
     * Calls command methods
116
     * 
117
     * @param  array $inputArgs
118
     * @return void
119
     */
120
    #[\Override]
121
    public function invoke(array $inputArgs): void
2✔
122
    {
123
        $firstKey = array_key_first($inputArgs);
2✔
124

125
        if ($firstKey === null || !array_key_exists($firstKey, $this->registry)) {
2✔
126
            $this->printer("⚠️  Command \"$firstKey\" not found" . PHP_EOL, 'light_yellow');
1✔
127
            return;
1✔
128
        }
129

130
        $class  = new $this->registry[$firstKey][0];
1✔
131
        $method = $this->registry[$firstKey][1] ?? "actionIndex";
1✔
132

133
        $class->$method();
1✔
134
    }
135

136
    /**
137
     * Retrieves the commands registry
138
     * 
139
     * @return array<string, array>
140
     */
141
    #[\Override]
142
    public function getRegistry(): array
1✔
143
    {
144
        return $this->registry;
1✔
145
    }
146

147
    /**
148
     * Checks if there is a color in the array
149
     * 
150
     * @param  string $key
151
     * @return void
152
     * @throws LogicException
153
     */
154
    private function checkColorExists(string $key): void
5✔
155
    {
156
        if (!array_key_exists($key, self::COLOR)) {
5✔
157
            throw new LogicException("Color $key doesn't exist");
1✔
158
        }
159
    }
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc