Skip to content

Commit 346c0f7

Browse files
committed
Prevent users from making factory functions invisible
1 parent 0bb1d31 commit 346c0f7

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/Output/Color.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ public static function style(string $name, array $style): void
192192
throw new InvalidArgumentException('Trying to set empty or invalid style');
193193
}
194194

195+
if (method_exists(static::class, $name) && isset($style['bg']) && isset($style['fg']) && $style['bg'] === $style['fg']) {
196+
throw new InvalidArgumentException('Built-in styles cannot be invisible (matching background and foreground)');
197+
}
198+
195199
static::$styles[$name] = $style;
196200
}
197201

tests/Output/ColorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public function test_invalid_custom_style()
4545
Color::style('alert', ['invalid' => true]);
4646
}
4747

48+
public function test_invisible_built_in_style()
49+
{
50+
$this->expectException(InvalidArgumentException::class);
51+
$this->expectExceptionMessage('Built-in styles cannot be invisible (matching background and foreground)');
52+
53+
Color::style('error', ['bg' => Color::RED, 'fg' => Color::RED]);
54+
}
55+
4856
public function test_colors()
4957
{
5058
$c = new Color;

0 commit comments

Comments
 (0)