Skip to content

Commit 6287f7f

Browse files
committed
Fix regression in File::getEol(), deprecate Convert::lineEndingsToUnix()
- Remove use of deprecated `auto_detect_line_endings` setting - Adopt `Str::setEol()` over `Convert::lineEndingsToUnix()`
1 parent 4ca58a9 commit 6287f7f

File tree

8 files changed

+18
-21
lines changed

8 files changed

+18
-21
lines changed

src/Console/ConsoleFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function formatTags(
250250
// fenced code blocks and code spans
251251
if (!Pcre::matchAll(
252252
Regex::delimit(self::PARSER_REGEX) . 'u',
253-
Convert::lineEndingsToUnix($string),
253+
Str::setEol($string),
254254
$matches,
255255
PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL
256256
)) {

src/Support/PhpDoc/PhpDoc.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Lkrms\Contract\IReadable;
77
use Lkrms\Support\Catalog\RegularExpression as Regex;
88
use Lkrms\Utility\Convert;
9+
use Lkrms\Utility\Str;
910
use UnexpectedValueException;
1011

1112
/**
@@ -141,7 +142,7 @@ public function __construct(
141142
'/(^\h*\* ?|\h+$)/um',
142143
'',
143144
// 2. Normalise line endings
144-
Convert::lineEndingsToUnix(
145+
Str::setEol(
145146
// 1. Extract text between "/**" and "*/"
146147
$matches['content']
147148
)

src/Utility/Convert.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,7 @@ public static function toNormal(string $text): string
13401340
/**
13411341
* Replace a string's CRLF or CR end-of-line sequences with LF
13421342
*
1343+
* @deprecated Use {@see Str::setEol()} instead
13431344
*/
13441345
public static function lineEndingsToUnix(string $string): string
13451346
{

src/Utility/Env.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static function load(string ...$path): void
6060
$queue = [];
6161
$errors = [];
6262
foreach ($path as $filename) {
63-
$lines = explode("\n", Convert::lineEndingsToUnix(file_get_contents($filename)));
63+
$lines = explode("\n", Str::setEol(file_get_contents($filename)));
6464
self::parse($lines, $queue, $errors, $filename);
6565
}
6666
self::doLoad($queue, $errors);

src/Utility/Filesystem.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ function (SplFileInfo $current, string $key) use (
152152
*/
153153
public function getEol(string $filename): ?string
154154
{
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-
161155
if (($f = fopen($filename, 'r')) === false ||
162156
($line = fgets($f)) === false ||
163157
fclose($f) === false) {
@@ -170,8 +164,8 @@ public function getEol(string $filename): ?string
170164
}
171165
}
172166

173-
if ($restore !== null) {
174-
ini_set('auto_detect_line_endings', $restore);
167+
if (strpos($line, "\r") !== false) {
168+
return "\r";
175169
}
176170

177171
return null;

src/Utility/Reflection.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ public function getAllClassDocComments(ReflectionClass $class): array
162162
$comments = [];
163163
do {
164164
if (($comment = $class->getDocComment()) !== false) {
165-
$comments[$class->getName()] = Convert::lineEndingsToUnix($comment);
165+
$comments[$class->getName()] = Str::setEol($comment);
166166
}
167167
} while ($class = $class->getParentClass());
168168

169169
foreach ($interfaces as $interface) {
170170
if (($comment = $interface->getDocComment()) !== false) {
171-
$comments[$interface->getName()] = Convert::lineEndingsToUnix($comment);
171+
$comments[$interface->getName()] = Str::setEol($comment);
172172
}
173173
}
174174

@@ -201,10 +201,10 @@ public function getAllMethodDocComments(ReflectionMethod $method, ?array &$class
201201
if ($interface->hasMethod($name) &&
202202
($comment = $interface->getMethod($name)->getDocComment()) !== false) {
203203
$class = $interface->getName();
204-
$comments[$class] = Convert::lineEndingsToUnix($comment);
204+
$comments[$class] = Str::setEol($comment);
205205
if (!is_null($classDocComments)) {
206206
$comment = $interface->getDocComment() ?: null;
207-
$classDocComments[$class] = $comment === null ? null : Convert::lineEndingsToUnix($comment);
207+
$classDocComments[$class] = $comment === null ? null : Str::setEol($comment);
208208
}
209209
}
210210
}
@@ -223,10 +223,10 @@ private function _getAllMethodDocComments(ReflectionMethod $method, string $name
223223
do {
224224
if (($comment = $method->getDocComment()) !== false) {
225225
$class = $method->getDeclaringClass()->getName();
226-
$comments[$class] = Convert::lineEndingsToUnix($comment);
226+
$comments[$class] = Str::setEol($comment);
227227
if (!is_null($classDocComments)) {
228228
$comment = $method->getDeclaringClass()->getDocComment() ?: null;
229-
$classDocComments[$class] = $comment === null ? null : Convert::lineEndingsToUnix($comment);
229+
$classDocComments[$class] = $comment === null ? null : Str::setEol($comment);
230230
}
231231
}
232232
// Interfaces don't have traits, so there's nothing else to do here
@@ -293,10 +293,10 @@ private function _getAllPropertyDocComments(
293293
do {
294294
if (($comment = $property->getDocComment()) !== false) {
295295
$class = $property->getDeclaringClass()->getName();
296-
$comments[$class] = Convert::lineEndingsToUnix($comment);
296+
$comments[$class] = Str::setEol($comment);
297297
if (!is_null($classDocComments)) {
298298
$comment = $property->getDeclaringClass()->getDocComment() ?: null;
299-
$classDocComments[$class] = $comment === null ? null : Convert::lineEndingsToUnix($comment);
299+
$classDocComments[$class] = $comment === null ? null : Str::setEol($comment);
300300
}
301301
}
302302
foreach ($property->getDeclaringClass()->getTraits() as $trait) {

src/Utility/Str.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static function wordwrap(
5252
*/
5353
public static function setEol(
5454
string $string,
55-
string $eol = PHP_EOL
55+
string $eol = "\n"
5656
): string {
5757
switch ($eol) {
5858
case "\n":

tests/unit/Polyfill/PhpTokenTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Lkrms\Polyfill\PhpToken;
66
use Lkrms\Utility\Convert;
7+
use Lkrms\Utility\Str;
78

89
final class PhpTokenTest extends \Lkrms\Tests\TestCase
910
{
@@ -14,7 +15,7 @@ final class PhpTokenTest extends \Lkrms\Tests\TestCase
1415
*/
1516
public function testTokenize(string $input, array $expected)
1617
{
17-
$actual = PhpToken::tokenize(Convert::lineEndingsToUnix($input), TOKEN_PARSE);
18+
$actual = PhpToken::tokenize(Str::setEol($input), TOKEN_PARSE);
1819
$actualCode = array_reduce(
1920
$actual,
2021
fn(string $code, PhpToken $token) => sprintf(

0 commit comments

Comments
 (0)