Skip to content

Commit 4ca58a9

Browse files
committed
Merge branch 'eol-improvements'
2 parents cf2e9d0 + 2fc53da commit 4ca58a9

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

src/Facade/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @method static void unload() Clear the underlying Filesystem instance
1717
* @method static string createTemporaryDirectory() Create a temporary directory
1818
* @method static FluentIteratorInterface<string,SplFileInfo> find(string $directory, string|null $exclude = null, string|null $include = null, array<string,callable(SplFileInfo): bool> $excludeCallbacks = null, array<string,callable(SplFileInfo): bool> $includeCallbacks = null, bool $recursive = true, bool $withDirectories = false, bool $withDirectoriesFirst = true) Iterate over files in a directory (see {@see Filesystem::find()})
19-
* @method static string|false getEol(string $filename) Get a file's end-of-line sequence (see {@see Filesystem::getEol()})
19+
* @method static string|null getEol(string $filename) Get the end-of-line sequence used in a file (see {@see Filesystem::getEol()})
2020
* @method static string getStablePath(string $suffix = '', ?string $dir = null) Generate a filename unique to the current user and the path of the running script (see {@see Filesystem::getStablePath()})
2121
* @method static string|null getStreamUri(resource $stream) Get the URI or filename associated with a stream (see {@see Filesystem::getStreamUri()})
2222
* @method static bool isPhp(string $filename) True if a file appears to contain PHP code

src/Utility/Filesystem.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,41 @@ function (SplFileInfo $current, string $key) use (
140140
}
141141

142142
/**
143-
* Get a file's end-of-line sequence
143+
* Get the end-of-line sequence used in a file
144144
*
145-
* @param string $filename
146-
* @return string|false `"\r\n"` or `"\n"` on success, or `false` if the
147-
* file's line endings couldn't be determined.
145+
* Recognised line endings are LF (`"\n"`), CRLF (`"\r\n"`) and CR (`"\r"`).
146+
*
147+
* @return string|null `null` if there are no recognised line breaks in the
148+
* file.
149+
*
150+
* @see Inspect::getEol()
151+
* @see Str::setEol()
148152
*/
149-
public function getEol(string $filename)
153+
public function getEol(string $filename): ?string
150154
{
155+
// Enable PHP's detection of CR line endings
156+
$restore = ini_set('auto_detect_line_endings', '1');
157+
if ($restore === false || $restore) {
158+
$restore = null;
159+
}
160+
151161
if (($f = fopen($filename, 'r')) === false ||
152162
($line = fgets($f)) === false ||
153163
fclose($f) === false) {
154-
return false;
164+
throw new RuntimeException(sprintf('Error reading file: %s', $filename));
155165
}
156166

157-
foreach (["\r\n", "\n"] as $eol) {
158-
if (substr($line, -strlen($eol)) == $eol) {
167+
foreach (["\r\n", "\n", "\r"] as $eol) {
168+
if (substr($line, -strlen($eol)) === $eol) {
159169
return $eol;
160170
}
161171
}
162172

163-
return false;
173+
if ($restore !== null) {
174+
ini_set('auto_detect_line_endings', $restore);
175+
}
176+
177+
return null;
164178
}
165179

166180
/**

src/Utility/Inspect.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ final class Inspect
1111
/**
1212
* Get the end-of-line sequence used in a string
1313
*
14-
* Line endings recognised are LF (`"\n"`), CRLF (`"\r\n"`) and CR (`"\r"`).
14+
* Recognised line endings are LF (`"\n"`), CRLF (`"\r\n"`) and CR (`"\r"`).
1515
*
1616
* @return string|null `null` if there are no recognised line breaks in
1717
* `$string`.
18+
*
19+
* @see Filesystem::getEol()
20+
* @see Str::setEol()
1821
*/
1922
public static function getEol(string $string): ?string
2023
{

src/Utility/Str.php

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

33
namespace Lkrms\Utility;
44

5+
use LogicException;
6+
57
/**
68
* Manipulate strings
79
*
@@ -43,4 +45,27 @@ public static function wordwrap(
4345
$delta
4446
);
4547
}
48+
49+
/**
50+
* Apply an end-of-line sequence to a string
51+
*
52+
*/
53+
public static function setEol(
54+
string $string,
55+
string $eol = PHP_EOL
56+
): string {
57+
switch ($eol) {
58+
case "\n":
59+
return str_replace(["\r\n", "\r"], $eol, $string);
60+
61+
case "\r":
62+
return str_replace(["\r\n", "\n"], $eol, $string);
63+
64+
case "\r\n":
65+
return str_replace(["\r\n", "\r", "\n"], ["\n", "\n", $eol], $string);
66+
67+
default:
68+
throw new LogicException(sprintf('Invalid end-of-line sequence: %s', $eol));
69+
}
70+
}
4671
}

0 commit comments

Comments
 (0)