Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.

Commit 4bd8f08

Browse files
committed
Throw exception if level < 0
1 parent 0e1e693 commit 4bd8f08

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/Util/TextFormat.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Phpactor\CodeBuilder\Util;
44

5+
use RuntimeException;
6+
57
class TextFormat
68
{
79
/**
@@ -22,6 +24,11 @@ public function __construct(string $indentation = ' ', string $newLineChar =
2224

2325
public function indent(string $string, int $level = 0): string
2426
{
27+
if ($level < 0) {
28+
throw new RuntimeException(sprintf(
29+
'Level must be greater than or equal to 0, got "%s"', $level
30+
));
31+
}
2532
$lines = TextUtil::lines($string);
2633
$lines = array_map(function ($line) use ($level) {
2734
return str_repeat($this->indentation, $level) . $line;

tests/Unit/Util/TextFormatTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Phpactor\CodeBuilder\Util\TextFormat;
7+
use RuntimeException;
78

89
class TextFormatTest extends TestCase
910
{
@@ -139,6 +140,12 @@ public function provideIndent()
139140
];
140141
}
141142

143+
public function testIndentExceptionIfLevelLessThan0()
144+
{
145+
$this->expectException(RuntimeException::class);
146+
(new TextFormat())->indent('foobar', -1);
147+
}
148+
142149
public function testReplacesIndentation()
143150
{
144151
$this->assertEquals(<<<'EOT'

0 commit comments

Comments
 (0)