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

tempestphp / tempest-framework / 14024978163

23 Mar 2025 05:55PM UTC coverage: 79.391% (-0.05%) from 79.441%
14024978163

push

github

web-flow
feat(view): cache Blade and Twig templates in internal storage (#1061)

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

912 existing lines in 110 files now uncovered.

10478 of 13198 relevant lines covered (79.39%)

91.09 hits per line

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

95.0
/src/Tempest/Console/src/Components/Static/StaticMultipleChoiceComponent.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Console\Components\Static;
6

7
use Tempest\Console\Components\Option;
8
use Tempest\Console\Components\OptionCollection;
9
use Tempest\Console\Console;
10
use Tempest\Console\StaticConsoleComponent;
11
use Tempest\Support\Arr\ImmutableArray;
12

13
use function Tempest\Support\arr;
14
use function Tempest\Support\str;
15

16
final readonly class StaticMultipleChoiceComponent implements StaticConsoleComponent
17
{
18
    private OptionCollection $options;
19

20
    public function __construct(
7✔
21
        public string $label,
22
        iterable $options,
23
        public array $default = [],
24
    ) {
25
        $this->options = new OptionCollection($options);
7✔
26
    }
27

28
    public function render(Console $console): array
7✔
29
    {
30
        if (! $console->supportsPrompting()) {
7✔
UNCOV
31
            return $this->default;
×
32
        }
33

34
        do {
35
            $answers = $this->askQuestion($console);
7✔
36

37
            $answerAsString = arr($answers)
7✔
38
                ->map(fn (Option $option) => $option->displayValue)
7✔
39
                ->join(', ', ' and ')
7✔
40
                ->trim()
7✔
41
                ->toString() ?: 'no option';
7✔
42

43
            $continue = $console->confirm(
7✔
44
                question: $answers
7✔
45
                    ? "You picked {$answerAsString}; continue?"
5✔
46
                    : "Continue with {$answerAsString}?",
7✔
47
                default: true,
7✔
48
            );
7✔
49
        } while ($continue === false);
5✔
50

51
        if ($answers === []) {
5✔
52
            return $this->default;
2✔
53
        }
54

55
        return array_map(fn (Option $option) => $option->value, $answers);
3✔
56
    }
57

58
    private function askQuestion(Console $console): array
7✔
59
    {
60
        $console->writeln("<style='bold fg-blue'>{$this->label}</style> ");
7✔
61
        $console->write('<style="fg-gray">Select multiple items using comas</style>');
7✔
62

63
        $prompt = $this->options
7✔
64
            ->getOptions()
7✔
65
            ->map(
7✔
66
                fn (Option $option, int $index) => str($index)
7✔
67
                    ->when(
7✔
68
                        condition: in_array($option->value, $this->default, strict: true),
7✔
69
                        callback: fn ($s) => $s->wrap('<style="fg-blue">', '</style>'),
7✔
70
                    )
7✔
71
                    ->wrap('[', ']')
7✔
72
                    ->prepend('- ')
7✔
73
                    ->append(' ', $option->displayValue)
7✔
74
                    ->toString(),
7✔
75
            )
7✔
76
            ->implode(PHP_EOL)
7✔
77
            ->toString();
7✔
78

79
        $console->write(PHP_EOL . $prompt . PHP_EOL);
7✔
80

81
        return ImmutableArray::explode($console->readln(), separator: ',')
7✔
82
            ->map(function (string $answer) {
7✔
83
                $answer = trim($answer);
7✔
84

85
                return $this->options
7✔
86
                    ->getOptions()
7✔
87
                    ->first(function (Option $option, int $index) use ($answer) {
7✔
88
                        if ($answer === $option->displayValue) {
7✔
89
                            return true;
2✔
90
                        }
91

92
                        if ($answer === $option->value) {
7✔
UNCOV
93
                            return true;
×
94
                        }
95

96
                        if ($this->options->getOptions()->isList() && $answer === ((string) $index)) {
7✔
97
                            return true;
4✔
98
                        }
99

100
                        if ($this->options->getOptions()->isList() && $answer === ((string) $option->key)) {
6✔
UNCOV
101
                            return true;
×
102
                        }
103

104
                        return false;
6✔
105
                    });
7✔
106
            })
7✔
107
            ->filter()
7✔
108
            ->unique(fn (Option $option) => $option->value)
7✔
109
            ->toArray();
7✔
110
    }
111
}
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