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

tempestphp / tempest-framework / 11714560237

06 Nov 2024 06:46PM UTC coverage: 82.608% (+0.003%) from 82.605%
11714560237

push

github

web-flow
feat(container): support injecting properties using `#[Inject]` (#690)

6 of 6 new or added lines in 2 files covered. (100.0%)

95 existing lines in 9 files now uncovered.

7210 of 8728 relevant lines covered (82.61%)

49.34 hits per line

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

95.45
/src/Tempest/Console/src/Components/Interactive/MultipleChoiceComponent.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Console\Components\Interactive;
6

7
use Tempest\Console\Components\Static\StaticMultipleChoiceComponent;
8
use Tempest\Console\HandlesKey;
9
use Tempest\Console\HasStaticComponent;
10
use Tempest\Console\InteractiveConsoleComponent;
11
use Tempest\Console\Key;
12
use Tempest\Console\StaticConsoleComponent;
13

14
final class MultipleChoiceComponent implements InteractiveConsoleComponent, HasStaticComponent
15
{
16
    public array $selectedOptions = [];
17

18
    public int|string $activeOption;
19

20
    public function __construct(
11✔
21
        public string $question,
22
        public array $options,
23
        public array $default = [],
24
    ) {
25
        foreach ($this->default as $key => $value) {
11✔
26
            $this->selectedOptions[array_is_list($options) ? $key : $value] = true;
9✔
27
        }
28

29
        $this->activeOption = array_key_first($this->options);
11✔
30
    }
31

32
    public function render(): string
2✔
33
    {
34
        $output = "<question>{$this->question}</question>";
2✔
35

36
        foreach ($this->options as $key => $option) {
2✔
37
            $output .= PHP_EOL;
2✔
38

39
            $output .= $this->isActive($key) ? '> ' : '  ';
2✔
40
            $output .= $this->isSelected($key) ? '[x]' : '[ ]';
2✔
41
            $output .= $this->isActive($key) ? '<em>' : '';
2✔
42
            $output .= " {$option}";
2✔
43
            $output .= $this->isActive($key) ? '</em>' : '';
2✔
44
        }
45

46
        return $output;
2✔
47
    }
48

UNCOV
49
    public function renderFooter(): string
×
50
    {
UNCOV
51
        return "Press <em>space</em> to select, <em>enter</em> to confirm, <em>ctrl+c</em> to cancel";
×
52
    }
53

54
    public function isActive(int|string $key): bool
2✔
55
    {
56
        return $this->activeOption === $key;
2✔
57
    }
58

59
    public function isSelected(int|string $key): bool
3✔
60
    {
61
        return $this->selectedOptions[$key] ?? false;
3✔
62
    }
63

64
    #[HandlesKey(Key::SPACE)]
2✔
65
    public function toggleSelected(): void
66
    {
67
        $this->selectedOptions[$this->activeOption] = ! $this->isSelected($this->activeOption);
2✔
68
    }
69

70
    #[HandlesKey(Key::ENTER)]
3✔
71
    public function enter(): array
72
    {
73
        $result = [];
3✔
74

75
        foreach ($this->options as $key => $option) {
3✔
76
            if ($this->isSelected($key)) {
3✔
77
                $result[$key] = $option;
3✔
78
            }
79
        }
80

81
        return $result;
3✔
82
    }
83

84
    #[HandlesKey(Key::UP)]
2✔
85
    #[HandlesKey(Key::LEFT)]
86
    public function up(): void
87
    {
88
        $previousValue = prev($this->options);
2✔
89

90
        if ($previousValue === false) {
2✔
91
            end($this->options);
2✔
92
        }
93

94
        $this->activeOption = key($this->options);
2✔
95
    }
96

97
    #[HandlesKey(Key::DOWN)]
2✔
98
    #[HandlesKey(Key::RIGHT)]
99
    public function down(): void
100
    {
101
        $nextValue = next($this->options);
2✔
102

103
        if ($nextValue === false) {
2✔
104
            reset($this->options);
2✔
105
        }
106

107
        $this->activeOption = key($this->options);
2✔
108
    }
109

110
    public function getStaticComponent(): StaticConsoleComponent
8✔
111
    {
112
        return new StaticMultipleChoiceComponent(
8✔
113
            question: $this->question,
8✔
114
            options: $this->options,
8✔
115
            default: $this->default,
8✔
116
        );
8✔
117
    }
118
}
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