Skip to content

Commit a7cb394

Browse files
committed
Apply path
1 parent 247a0a4 commit a7cb394

File tree

1 file changed

+62
-63
lines changed
  • src/Symfony/Component/Filesystem

1 file changed

+62
-63
lines changed

src/Symfony/Component/Filesystem/Path.php

+62-63
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Filesystem;
1313

1414
use Symfony\Component\Filesystem\Exception\InvalidArgumentException;
15-
use Symfony\Component\Filesystem\ExceptionInvalidArgumentException;
1615

1716
/**
1817
* Contains utility methods for handling path strings.
@@ -83,14 +82,14 @@ public static function canonicalize(string $path): string
8382

8483
// Replace "~" with user's home directory.
8584
if ('~' === $path[0]) {
86-
$path = self::getHomeDirectory().\substr($path, 1);
85+
$path = self::getHomeDirectory().substr($path, 1);
8786
}
8887

89-
$path = \str_replace('\\', '/', $path);
88+
$path = str_replace('\\', '/', $path);
9089

9190
[$root, $pathWithoutRoot] = self::split($path);
9291

93-
$parts = \explode('/', $pathWithoutRoot);
92+
$parts = explode('/', $pathWithoutRoot);
9493
$canonicalParts = [];
9594

9695
// Collapse "." and "..", if possible
@@ -102,7 +101,7 @@ public static function canonicalize(string $path): string
102101
// Collapse ".." with the previous part, if one exists
103102
// Don't collapse ".." if the previous part is also ".."
104103
if ('..' === $part && \count($canonicalParts) > 0 && '..' !== $canonicalParts[\count($canonicalParts) - 1]) {
105-
\array_pop($canonicalParts);
104+
array_pop($canonicalParts);
106105

107106
continue;
108107
}
@@ -114,7 +113,7 @@ public static function canonicalize(string $path): string
114113
}
115114

116115
// Add the root directory again
117-
self::$buffer[$path] = $canonicalPath = $root.\implode('/', $canonicalParts);
116+
self::$buffer[$path] = $canonicalPath = $root.implode('/', $canonicalParts);
118117
++self::$bufferSize;
119118

120119
// Clean up regularly to prevent memory leaks
@@ -139,7 +138,7 @@ public static function canonicalize(string $path): string
139138
*/
140139
public static function normalize(string $path): string
141140
{
142-
return \str_replace('\\', '/', $path);
141+
return str_replace('\\', '/', $path);
143142
}
144143

145144
/**
@@ -174,9 +173,9 @@ public static function getDirectory(string $path): string
174173
$path = self::canonicalize($path);
175174

176175
// Maintain scheme
177-
if (false !== ($pos = \strpos($path, '://'))) {
178-
$scheme = \substr($path, 0, $pos + 3);
179-
$path = \substr($path, $pos + 3);
176+
if (false !== ($pos = strpos($path, '://'))) {
177+
$scheme = substr($path, 0, $pos + 3);
178+
$path = substr($path, $pos + 3);
180179
} else {
181180
$scheme = '';
182181
}
@@ -188,11 +187,11 @@ public static function getDirectory(string $path): string
188187
}
189188

190189
// Directory equals Windows root "C:/"
191-
if (2 === $pos && \ctype_alpha($path[0]) && ':' === $path[1]) {
192-
return $scheme.\substr($path, 0, 3);
190+
if (2 === $pos && ctype_alpha($path[0]) && ':' === $path[1]) {
191+
return $scheme.substr($path, 0, 3);
193192
}
194193

195-
return $scheme.\substr($path, 0, $pos);
194+
return $scheme.substr($path, 0, $pos);
196195
}
197196

198197
return '';
@@ -215,13 +214,13 @@ public static function getDirectory(string $path): string
215214
public static function getHomeDirectory(): string
216215
{
217216
// For UNIX support
218-
if (\getenv('HOME')) {
219-
return self::canonicalize(\getenv('HOME'));
217+
if (getenv('HOME')) {
218+
return self::canonicalize(getenv('HOME'));
220219
}
221220

222221
// For >= Windows8 support
223-
if (\getenv('HOMEDRIVE') && \getenv('HOMEPATH')) {
224-
return self::canonicalize(\getenv('HOMEDRIVE').\getenv('HOMEPATH'));
222+
if (getenv('HOMEDRIVE') && getenv('HOMEPATH')) {
223+
return self::canonicalize(getenv('HOMEDRIVE').getenv('HOMEPATH'));
225224
}
226225

227226
throw new \RuntimeException("Cannot find the home directory path: Your environment or operation system isn't supported");
@@ -242,9 +241,9 @@ public static function getRoot(string $path): string
242241
}
243242

244243
// Maintain scheme
245-
if (false !== ($pos = \strpos($path, '://'))) {
246-
$scheme = \substr($path, 0, $pos + 3);
247-
$path = \substr($path, $pos + 3);
244+
if (false !== ($pos = strpos($path, '://'))) {
245+
$scheme = substr($path, 0, $pos + 3);
246+
$path = substr($path, $pos + 3);
248247
} else {
249248
$scheme = '';
250249
}
@@ -257,7 +256,7 @@ public static function getRoot(string $path): string
257256
$length = \strlen($path);
258257

259258
// Windows root
260-
if ($length > 1 && \ctype_alpha($path[0]) && ':' === $path[1]) {
259+
if ($length > 1 && ctype_alpha($path[0]) && ':' === $path[1]) {
261260
// Special case: "C:"
262261
if (2 === $length) {
263262
return $scheme.$path.'/';
@@ -286,10 +285,10 @@ public static function getFilenameWithoutExtension(string $path, ?string $extens
286285

287286
if (null !== $extension) {
288287
// remove extension and trailing dot
289-
return \rtrim(\basename($path, $extension), '.');
288+
return rtrim(basename($path, $extension), '.');
290289
}
291290

292-
return \pathinfo($path, PATHINFO_FILENAME);
291+
return pathinfo($path, PATHINFO_FILENAME);
293292
}
294293

295294
/**
@@ -303,7 +302,7 @@ public static function getExtension(string $path, bool $forceLowerCase = false):
303302
return '';
304303
}
305304

306-
$extension = \pathinfo($path, PATHINFO_EXTENSION);
305+
$extension = pathinfo($path, PATHINFO_EXTENSION);
307306

308307
if ($forceLowerCase) {
309308
$extension = self::toLower($extension);
@@ -346,7 +345,7 @@ public static function hasExtension(string $path, $extensions = null, bool $igno
346345
}
347346

348347
// remove leading '.' in extensions array
349-
$extensions[$key] = \ltrim($extension, '.');
348+
$extensions[$key] = ltrim($extension, '.');
350349
}
351350

352351
return \in_array($actualExtension, $extensions, true);
@@ -367,19 +366,19 @@ public static function changeExtension(string $path, string $extension): string
367366
}
368367

369368
$actualExtension = self::getExtension($path);
370-
$extension = \ltrim($extension, '.');
369+
$extension = ltrim($extension, '.');
371370

372371
// No extension for paths
373-
if ('/' === \substr($path, -1)) {
372+
if ('/' === substr($path, -1)) {
374373
return $path;
375374
}
376375

377376
// No actual extension in path
378377
if (empty($actualExtension)) {
379-
return $path.('.' === \substr($path, -1) ? '' : '.').$extension;
378+
return $path.('.' === substr($path, -1) ? '' : '.').$extension;
380379
}
381380

382-
return \substr($path, 0, -\strlen($actualExtension)).$extension;
381+
return substr($path, 0, -\strlen($actualExtension)).$extension;
383382
}
384383

385384
public static function isAbsolute(string $path): bool
@@ -389,8 +388,8 @@ public static function isAbsolute(string $path): bool
389388
}
390389

391390
// Strip scheme
392-
if (false !== ($pos = \strpos($path, '://'))) {
393-
$path = \substr($path, $pos + 3);
391+
if (false !== ($pos = strpos($path, '://'))) {
392+
$path = substr($path, $pos + 3);
394393
}
395394

396395
// UNIX root "/" or "\" (Windows style)
@@ -399,7 +398,7 @@ public static function isAbsolute(string $path): bool
399398
}
400399

401400
// Windows root
402-
if (\strlen($path) > 1 && \ctype_alpha($path[0]) && ':' === $path[1]) {
401+
if (\strlen($path) > 1 && ctype_alpha($path[0]) && ':' === $path[1]) {
403402
// Special case: "C:"
404403
if (2 === \strlen($path)) {
405404
return true;
@@ -453,31 +452,31 @@ public static function isRelative(string $path): bool
453452
* @param string $basePath an absolute base path
454453
*
455454
* @throws InvalidArgumentException if the base path is not absolute or if
456-
* the given path is an absolute path with
457-
* a different root than the base path
455+
* the given path is an absolute path with
456+
* a different root than the base path
458457
*/
459458
public static function makeAbsolute(string $path, string $basePath): string
460459
{
461460
if ('' === $basePath) {
462-
throw new InvalidArgumentException(\sprintf('The base path must be a non-empty string. Got: "%s"', $basePath));
461+
throw new InvalidArgumentException(sprintf('The base path must be a non-empty string. Got: "%s"', $basePath));
463462
}
464463

465464
if (!self::isAbsolute($basePath)) {
466-
throw new InvalidArgumentException(\sprintf('The base path "%s" is not an absolute path.', $basePath));
465+
throw new InvalidArgumentException(sprintf('The base path "%s" is not an absolute path.', $basePath));
467466
}
468467

469468
if (self::isAbsolute($path)) {
470469
return self::canonicalize($path);
471470
}
472471

473-
if (false !== ($pos = \strpos($basePath, '://'))) {
474-
$scheme = \substr($basePath, 0, $pos + 3);
475-
$basePath = \substr($basePath, $pos + 3);
472+
if (false !== ($pos = strpos($basePath, '://'))) {
473+
$scheme = substr($basePath, 0, $pos + 3);
474+
$basePath = substr($basePath, $pos + 3);
476475
} else {
477476
$scheme = '';
478477
}
479478

480-
return $scheme.self::canonicalize(\rtrim($basePath, '/\\').'/'.$path);
479+
return $scheme.self::canonicalize(rtrim($basePath, '/\\').'/'.$path);
481480
}
482481

483482
/**
@@ -527,8 +526,8 @@ public static function makeAbsolute(string $path, string $basePath): string
527526
* The result is a canonical path.
528527
*
529528
* @throws InvalidArgumentException if the base path is not absolute or if
530-
* the given path has a different root
531-
* than the base path
529+
* the given path has a different root
530+
* than the base path
532531
*/
533532
public static function makeRelative(string $path, string $basePath): string
534533
{
@@ -544,7 +543,7 @@ public static function makeRelative(string $path, string $basePath): string
544543
if ('' === $root && '' !== $baseRoot) {
545544
// If base path is already in its root
546545
if ('' === $relativeBasePath) {
547-
$relativePath = \ltrim($relativePath, './\\');
546+
$relativePath = ltrim($relativePath, './\\');
548547
}
549548

550549
return $relativePath;
@@ -553,21 +552,21 @@ public static function makeRelative(string $path, string $basePath): string
553552
// If the passed path is absolute, but the base path is not, we
554553
// cannot generate a relative path
555554
if ('' !== $root && '' === $baseRoot) {
556-
throw new InvalidArgumentException(\sprintf('The absolute path "%s" cannot be made relative to the relative path "%s". You should provide an absolute base path instead.', $path, $basePath));
555+
throw new InvalidArgumentException(sprintf('The absolute path "%s" cannot be made relative to the relative path "%s". You should provide an absolute base path instead.', $path, $basePath));
557556
}
558557

559558
// Fail if the roots of the two paths are different
560559
if ($baseRoot && $root !== $baseRoot) {
561-
throw new InvalidArgumentException(\sprintf('The path "%s" cannot be made relative to "%s", because they have different roots ("%s" and "%s").', $path, $basePath, $root, $baseRoot));
560+
throw new InvalidArgumentException(sprintf('The path "%s" cannot be made relative to "%s", because they have different roots ("%s" and "%s").', $path, $basePath, $root, $baseRoot));
562561
}
563562

564563
if ('' === $relativeBasePath) {
565564
return $relativePath;
566565
}
567566

568567
// Build a "../../" prefix with as many "../" parts as necessary
569-
$parts = \explode('/', $relativePath);
570-
$baseParts = \explode('/', $relativeBasePath);
568+
$parts = explode('/', $relativePath);
569+
$baseParts = explode('/', $relativeBasePath);
571570
$dotDotPrefix = '';
572571

573572
// Once we found a non-matching part in the prefix, we need to add
@@ -585,15 +584,15 @@ public static function makeRelative(string $path, string $basePath): string
585584
$dotDotPrefix .= '../';
586585
}
587586

588-
return \rtrim($dotDotPrefix.\implode('/', $parts), '/');
587+
return rtrim($dotDotPrefix.implode('/', $parts), '/');
589588
}
590589

591590
/**
592591
* Returns whether the given path is on the local filesystem.
593592
*/
594593
public static function isLocal(string $path): bool
595594
{
596-
return '' !== $path && false === \strpos($path, '://');
595+
return '' !== $path && false === strpos($path, '://');
597596
}
598597

599598
/**
@@ -657,7 +656,7 @@ public static function getLongestCommonBasePath(string ...$paths): ?string
657656

658657
// Prevent false positives for common prefixes
659658
// see isBasePath()
660-
if (0 === \strpos($path.'/', $basePath.'/')) {
659+
if (0 === strpos($path.'/', $basePath.'/')) {
661660
// Next path
662661
continue 2;
663662
}
@@ -687,17 +686,17 @@ public static function join(string ...$paths): string
687686
if (null === $finalPath) {
688687
// For first part we keep slashes, like '/top', 'C:\' or 'phar://'
689688
$finalPath = $path;
690-
$wasScheme = (false !== \strpos($path, '://'));
689+
$wasScheme = (false !== strpos($path, '://'));
691690
continue;
692691
}
693692

694693
// Only add slash if previous part didn't end with '/' or '\'
695-
if (!\in_array(\substr($finalPath, -1), ['/', '\\'])) {
694+
if (!\in_array(substr($finalPath, -1), ['/', '\\'])) {
696695
$finalPath .= '/';
697696
}
698697

699698
// If first part included a scheme like 'phar://' we allow current part to start with '/', otherwise trim
700-
$finalPath .= $wasScheme ? $path : \ltrim($path, '/');
699+
$finalPath .= $wasScheme ? $path : ltrim($path, '/');
701700
$wasScheme = false;
702701
}
703702

@@ -738,7 +737,7 @@ public static function isBasePath(string $basePath, string $ofPath): bool
738737
// Don't append a slash for the root "/", because then that root
739738
// won't be discovered as common prefix ("//" is not a prefix of
740739
// "/foobar/").
741-
return 0 === \strpos($ofPath.'/', \rtrim($basePath, '/').'/');
740+
return 0 === strpos($ofPath.'/', rtrim($basePath, '/').'/');
742741
}
743742

744743
/**
@@ -766,28 +765,28 @@ private static function split(string $path): array
766765
}
767766

768767
// Remember scheme as part of the root, if any
769-
if (false !== ($pos = \strpos($path, '://'))) {
770-
$root = \substr($path, 0, $pos + 3);
771-
$path = \substr($path, $pos + 3);
768+
if (false !== ($pos = strpos($path, '://'))) {
769+
$root = substr($path, 0, $pos + 3);
770+
$path = substr($path, $pos + 3);
772771
} else {
773772
$root = '';
774773
}
775774

776775
$length = \strlen($path);
777776

778777
// Remove and remember root directory
779-
if (0 === \strpos($path, '/')) {
778+
if (0 === strpos($path, '/')) {
780779
$root .= '/';
781-
$path = $length > 1 ? \substr($path, 1) : '';
782-
} elseif ($length > 1 && \ctype_alpha($path[0]) && ':' === $path[1]) {
780+
$path = $length > 1 ? substr($path, 1) : '';
781+
} elseif ($length > 1 && ctype_alpha($path[0]) && ':' === $path[1]) {
783782
if (2 === $length) {
784783
// Windows special case: "C:"
785784
$root .= $path.'/';
786785
$path = '';
787786
} elseif ('/' === $path[2]) {
788787
// Windows normal case: "C:/"..
789-
$root .= \substr($path, 0, 3);
790-
$path = $length > 3 ? \substr($path, 3) : '';
788+
$root .= substr($path, 0, 3);
789+
$path = $length > 3 ? substr($path, 3) : '';
791790
}
792791
}
793792

@@ -796,7 +795,7 @@ private static function split(string $path): array
796795

797796
private static function toLower(string $str): string
798797
{
799-
return \mb_strtolower($str, mb_detect_encoding($str));
798+
return mb_strtolower($str, mb_detect_encoding($str));
800799
}
801800

802801
private function __construct()

0 commit comments

Comments
 (0)