Skip to content

Commit 372b40c

Browse files
Simplify condition handling
1 parent 1010597 commit 372b40c

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/Runner/Config/Visualizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private function isApplicationSettingsOnly(): bool
213213
Visualizer\Settings::OPT_OPTIONS,
214214
Visualizer\Settings::OPT_CONFIG,
215215
];
216-
// if so app config is not the only thing to display
216+
// if the app config is not the only thing to display
217217
foreach ($actionRelatedStuff as $option) {
218218
if ($this->settings->show($option)) {
219219
return false;

src/Runner/Config/Visualizer/Output.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,9 @@ public function printAction(Config\Action $action): void
237237
*/
238238
public function printConditions(array $conditions, string $prefix = ''): void
239239
{
240-
if (empty($conditions)) {
240+
if (!$this->isConditionToBeDisplayed($conditions)) {
241241
return;
242242
}
243-
if (!$this->settings->show(Settings::OPT_CONDITIONS)) {
244-
return;
245-
}
246-
247243
if (empty($prefix)) {
248244
$this->io->write($prefix . ' <comment>Conditions:</comment>');
249245
}
@@ -264,7 +260,7 @@ public function printCondition(Config\Condition $condition, string $prefix = '')
264260
$this->io->write($prefix . ' - <fg=cyan>' . $condition->getExec() . '</>');
265261

266262
// handle logic conditions
267-
if (in_array(strtoupper($condition->getExec()), ['OR', 'AND'])) {
263+
if ($condition->isLogicCondition()) {
268264
$conditions = [];
269265
foreach ($condition->getArgs() as $conf) {
270266
$conditions[] = new Config\Condition($conf['exec'], $conf['args'] ?? []);
@@ -333,16 +329,11 @@ public function printActionConfig(Config\Action $action): void
333329
return;
334330
}
335331

336-
$config = [];
337-
if ($action->getLabel() != $action->getAction()) {
338-
$config['label'] = $action->getLabel();
339-
}
340-
if ($action->isFailureAllowed()) {
341-
$config['failureAllowed'] = true;
342-
}
332+
$config = $this->extractActionConfig($action);
343333
if (empty($config)) {
344334
return;
345335
}
336+
346337
$this->io->write(' <comment>Config:</comment>');
347338
foreach ($config as $key => $value) {
348339
$this->io->write(' - ' . $key . ': <fg=gray>' . Util::escapeLineBreaks((string)$value) . '</>');
@@ -359,4 +350,39 @@ private function extendedAction(Config\Action $action): bool
359350
{
360351
return $action->hasLabel() && $this->settings->show(Settings::OPT_ACTIONS);
361352
}
353+
354+
/**
355+
* Check if conditions should be displayed
356+
*
357+
* @param array<\CaptainHook\App\Config\Condition> $conditions
358+
* @return bool
359+
*/
360+
private function isConditionToBeDisplayed(array $conditions): bool
361+
{
362+
if (empty($conditions)) {
363+
return false;
364+
}
365+
if (!$this->settings->show(Settings::OPT_CONDITIONS)) {
366+
return false;
367+
}
368+
return true;
369+
}
370+
371+
/**
372+
* Extract the action config settings
373+
*
374+
* @param \CaptainHook\App\Config\Action $action
375+
* @return array<string, string|bool>
376+
*/
377+
private function extractActionConfig(Config\Action $action): array
378+
{
379+
$config = [];
380+
if ($action->hasLabel()) {
381+
$config['label'] = $action->getLabel();
382+
}
383+
if ($action->isFailureAllowed()) {
384+
$config['failureAllowed'] = true;
385+
}
386+
return $config;
387+
}
362388
}

0 commit comments

Comments
 (0)