Skip to content

Commit 7fc96ae

Browse files
Merge branch '7.4' into 8.0
* 7.4: (21 commits) [ObjectMapper] lazy loading [Filesystem] Unify logic for isAbsolute() in Path [DependencyInjection] Include return type in AppReference shape [Finder] Make method calls explicit in ExcludeDirectoryFilterIterator [Cache] Remove unset call on undefined variable in PhpArrayAdapter [Twig] Ensure WrappedTemplatedEmail::getReturnPath() returns a string [Routing] Simplify importing routes defined on controller services Fix tests [DependendcyInjection] Improve shape for "from_callable" definitions [PHPDoc] Fix various PHPDoc syntax errors [Console] Add missing VERBOSITY_SILENT case in CommandDataCollector [Notifier] Remove unused $transportName argument in EmailChannel::notify() [Translation] Remove an unused argument passed to parseNode() method [HttpClient] Reject 3xx pushed responses [Serializer] Use Asia/Tokyo instead of Japan in tests [ProxyManagerBridge] Remove comment that reference github discussion [JsonPath] Remove unused "nothing" property from JsonCrawler [ErrorHandler] Improve PHPDoc precision in SerializerErrorRenderer [Routing] Fix matching the "0" URL [Form] Fix EnumType choice_label logic for grouped choices ...
2 parents 63006b8 + 581d9d4 commit 7fc96ae

File tree

3 files changed

+36
-43
lines changed

3 files changed

+36
-43
lines changed

Filesystem.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -576,17 +576,11 @@ public function mirror(string $originDir, string $targetDir, ?\Traversable $iter
576576
}
577577

578578
/**
579-
* Returns whether the file path is an absolute path.
579+
* Returns whether the given path is absolute.
580580
*/
581581
public function isAbsolutePath(string $file): bool
582582
{
583-
return '' !== $file && (strspn($file, '/\\', 0, 1)
584-
|| (\strlen($file) > 3 && ctype_alpha($file[0])
585-
&& ':' === $file[1]
586-
&& strspn($file, '/\\', 2, 1)
587-
)
588-
|| null !== parse_url($file, \PHP_URL_SCHEME)
589-
);
583+
return Path::isAbsolute($file);
590584
}
591585

592586
/**

Path.php

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -358,38 +358,18 @@ public static function changeExtension(string $path, string $extension): string
358358
return substr($path, 0, -\strlen($actualExtension)).$extension;
359359
}
360360

361+
/**
362+
* Returns whether the given path is absolute.
363+
*/
361364
public static function isAbsolute(string $path): bool
362365
{
363-
if ('' === $path) {
364-
return false;
365-
}
366-
367-
// Strip scheme
368-
if (false !== ($schemeSeparatorPosition = strpos($path, '://')) && 1 !== $schemeSeparatorPosition) {
369-
$path = substr($path, $schemeSeparatorPosition + 3);
370-
}
371-
372-
$firstCharacter = $path[0];
373-
374-
// UNIX root "/" or "\" (Windows style)
375-
if ('/' === $firstCharacter || '\\' === $firstCharacter) {
376-
return true;
377-
}
378-
379-
// Windows root
380-
if (\strlen($path) > 1 && ctype_alpha($firstCharacter) && ':' === $path[1]) {
381-
// Special case: "C:"
382-
if (2 === \strlen($path)) {
383-
return true;
384-
}
385-
386-
// Normal case: "C:/ or "C:\"
387-
if ('/' === $path[2] || '\\' === $path[2]) {
388-
return true;
389-
}
390-
}
391-
392-
return false;
366+
return '' !== $path && (strspn($path, '/\\', 0, 1)
367+
|| (\strlen($path) > 3 && ctype_alpha($path[0])
368+
&& ':' === $path[1]
369+
&& strspn($path, '/\\', 2, 1)
370+
)
371+
|| null !== parse_url($path, \PHP_URL_SCHEME)
372+
);
393373
}
394374

395375
public static function isRelative(string $path): bool

Tests/PathTest.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,31 +354,50 @@ public function testChangeExtension(string $path, string $extension, string $pat
354354

355355
public static function provideIsAbsolutePathTests(): \Generator
356356
{
357+
// UNIX-style absolute paths
357358
yield ['/css/style.css', true];
358359
yield ['/', true];
359360
yield ['css/style.css', false];
360361
yield ['', false];
361362

363+
// UNIX-style absolute paths with backslashes
362364
yield ['\\css\\style.css', true];
363365
yield ['\\', true];
364366
yield ['css\\style.css', false];
365367

368+
// Windows-style absolute paths
366369
yield ['C:/css/style.css', true];
367370
yield ['D:/', true];
368371
yield ['C:///windows', true];
369372
yield ['C://test', true];
370373

374+
// Windows-style absolute paths with backslashes
371375
yield ['E:\\css\\style.css', true];
372376
yield ['F:\\', true];
373377

378+
// Windows special case (drive only)
379+
yield ['C:', true];
380+
381+
// URLs and stream wrappers are considered absolute
374382
yield ['phar:///css/style.css', true];
375383
yield ['phar:///', true];
384+
yield ['http://example.com', true];
385+
yield ['ftp://user@server/path', true];
386+
yield ['vfs://root/file.txt', true];
387+
388+
// "C:" without a slash is treated as a scheme by parse_url()
389+
yield ['C:css/style.css', true];
390+
391+
// Relative paths
392+
yield ['/var/lib', true];
393+
yield ['c:\\\\var\\lib', true]; // c:\\var\lib
394+
yield ['\\var\\lib', true];
395+
yield ['var/lib', false];
396+
yield ['../var/lib', false];
397+
yield ['', false];
376398

377-
// Windows special case
378-
yield ['C:', true];
379-
380-
// Not considered absolute
381-
yield ['C:css/style.css', false];
399+
// Empty path
400+
yield ['', false];
382401
}
383402

384403
#[DataProvider('provideIsAbsolutePathTests')]

0 commit comments

Comments
 (0)