Skip to content

Commit 736e7a2

Browse files
committed
Refactor EOL detection
1 parent 1b0cd5a commit 736e7a2

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

src/EnvFile/EOLType.php

+10
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,14 @@ final class EOLType
88
const WINDOWS = "\r\n";
99
const UNIX = "\n";
1010

11+
public static function detect(string $content, string $default = EOLType::UNIX): string
12+
{
13+
if (str_contains($content, EOLType::WINDOWS)) {
14+
return EOLType::WINDOWS;
15+
} elseif (str_contains($content, EOLType::UNIX)) {
16+
return EOLType::UNIX;
17+
}
18+
19+
return $default;
20+
}
1121
}

src/Parser.php

+1-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function parse(string $content): EnvFile
1919

2020
$file = new EnvFile();
2121

22-
$file->EOL = $this->EOL ?? $this->detectEOLType($content);
22+
$file->EOL = $this->EOL ?? EOLType::detect($content);
2323

2424
$blockStart = "^";
2525
$blockEnd = "(?:{$file->EOL}|$)";
@@ -105,17 +105,4 @@ public function parse(string $content): EnvFile
105105

106106
}
107107

108-
public function detectEOLType(string $content, string $default = EOLType::UNIX): string
109-
{
110-
111-
if (str_contains($content, EOLType::WINDOWS)) {
112-
return EOLType::WINDOWS;
113-
} elseif (str_contains($content, EOLType::UNIX)) {
114-
return EOLType::UNIX;
115-
}
116-
117-
return $default;
118-
119-
}
120-
121108
}

tests/Unit/EnvFile/EOLTypeTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Tests\Unit\EnvFile;
4+
5+
use EnvEditor\EnvFile\EOLType;
6+
use Tests\TestCase;
7+
8+
class EOLTypeTest extends TestCase
9+
{
10+
11+
public function testDetectUnix(): void
12+
{
13+
$unix = "#test\n#test";
14+
$this->assertEquals(EOLType::UNIX, EOLType::detect($unix));
15+
}
16+
17+
public function testDetectWindows(): void
18+
{
19+
$win = "#test\r\n#test";
20+
$this->assertEquals(EOLType::WINDOWS, EOLType::detect($win));
21+
}
22+
23+
public function testDetectDefault(): void
24+
{
25+
$empty = "";
26+
$this->assertEquals(EOLType::UNIX, EOLType::detect($empty));
27+
$this->assertEquals(EOLType::UNIX, EOLType::detect($empty, EOLType::UNIX));
28+
$this->assertEquals(EOLType::WINDOWS, EOLType::detect($empty, EOLType::WINDOWS));
29+
}
30+
31+
}

tests/Unit/ParserTest.php

+1-20
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,7 @@
88

99
class ParserTest extends TestCase
1010
{
11-
12-
public function testDetectEOLType(): void
13-
{
14-
$parser = new Parser();
15-
16-
$win = "#test\r\n#test";
17-
$unix = "#test\n#test";
18-
$empty = "";
19-
20-
$this->assertEquals(EOLType::WINDOWS, $parser->detectEOLType($win));
21-
$this->assertEquals(EOLType::UNIX, $parser->detectEOLType($unix));
22-
$this->assertEquals(EOLType::UNIX, $parser->detectEOLType($empty));
23-
$this->assertEquals(EOLType::UNIX, $parser->detectEOLType($empty, EOLType::UNIX));
24-
$this->assertEquals(EOLType::WINDOWS, $parser->detectEOLType($empty, EOLType::WINDOWS));
25-
26-
}
27-
28-
/**
29-
* @depends testDetectEOLType
30-
*/
11+
3112
public function testFileEOLType(): void
3213
{
3314

0 commit comments

Comments
 (0)