Skip to content

Commit 49519d9

Browse files
committed
Prefix all sprintf() calls
1 parent 802e870 commit 49519d9

5 files changed

+42
-42
lines changed

Exception/FileNotFoundException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(?string $message = null, int $code = 0, ?\Throwable
2525
if (null === $path) {
2626
$message = 'File could not be found.';
2727
} else {
28-
$message = sprintf('File "%s" could not be found.', $path);
28+
$message = \sprintf('File "%s" could not be found.', $path);
2929
}
3030
}
3131

Filesystem.php

+33-33
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function copy(string $originFile, string $targetFile, bool $overwriteNewe
3838
{
3939
$originIsLocal = stream_is_local($originFile) || 0 === stripos($originFile, 'file://');
4040
if ($originIsLocal && !is_file($originFile)) {
41-
throw new FileNotFoundException(sprintf('Failed to copy "%s" because file does not exist.', $originFile), 0, null, $originFile);
41+
throw new FileNotFoundException(\sprintf('Failed to copy "%s" because file does not exist.', $originFile), 0, null, $originFile);
4242
}
4343

4444
$this->mkdir(\dirname($targetFile));
@@ -51,12 +51,12 @@ public function copy(string $originFile, string $targetFile, bool $overwriteNewe
5151
if ($doCopy) {
5252
// https://bugs.php.net/64634
5353
if (!$source = self::box('fopen', $originFile, 'r')) {
54-
throw new IOException(sprintf('Failed to copy "%s" to "%s" because source file could not be opened for reading: ', $originFile, $targetFile).self::$lastError, 0, null, $originFile);
54+
throw new IOException(\sprintf('Failed to copy "%s" to "%s" because source file could not be opened for reading: ', $originFile, $targetFile).self::$lastError, 0, null, $originFile);
5555
}
5656

5757
// Stream context created to allow files overwrite when using FTP stream wrapper - disabled by default
5858
if (!$target = self::box('fopen', $targetFile, 'w', false, stream_context_create(['ftp' => ['overwrite' => true]]))) {
59-
throw new IOException(sprintf('Failed to copy "%s" to "%s" because target file could not be opened for writing: ', $originFile, $targetFile).self::$lastError, 0, null, $originFile);
59+
throw new IOException(\sprintf('Failed to copy "%s" to "%s" because target file could not be opened for writing: ', $originFile, $targetFile).self::$lastError, 0, null, $originFile);
6060
}
6161

6262
$bytesCopied = stream_copy_to_stream($source, $target);
@@ -65,7 +65,7 @@ public function copy(string $originFile, string $targetFile, bool $overwriteNewe
6565
unset($source, $target);
6666

6767
if (!is_file($targetFile)) {
68-
throw new IOException(sprintf('Failed to copy "%s" to "%s".', $originFile, $targetFile), 0, null, $originFile);
68+
throw new IOException(\sprintf('Failed to copy "%s" to "%s".', $originFile, $targetFile), 0, null, $originFile);
6969
}
7070

7171
if ($originIsLocal) {
@@ -76,7 +76,7 @@ public function copy(string $originFile, string $targetFile, bool $overwriteNewe
7676
self::box('touch', $targetFile, filemtime($originFile));
7777

7878
if ($bytesCopied !== $bytesOrigin = filesize($originFile)) {
79-
throw new IOException(sprintf('Failed to copy the whole content of "%s" to "%s" (%g of %g bytes copied).', $originFile, $targetFile, $bytesCopied, $bytesOrigin), 0, null, $originFile);
79+
throw new IOException(\sprintf('Failed to copy the whole content of "%s" to "%s" (%g of %g bytes copied).', $originFile, $targetFile, $bytesCopied, $bytesOrigin), 0, null, $originFile);
8080
}
8181
}
8282
}
@@ -95,7 +95,7 @@ public function mkdir(string|iterable $dirs, int $mode = 0777): void
9595
}
9696

9797
if (!self::box('mkdir', $dir, $mode, true) && !is_dir($dir)) {
98-
throw new IOException(sprintf('Failed to create "%s": ', $dir).self::$lastError, 0, null, $dir);
98+
throw new IOException(\sprintf('Failed to create "%s": ', $dir).self::$lastError, 0, null, $dir);
9999
}
100100
}
101101
}
@@ -109,7 +109,7 @@ public function exists(string|iterable $files): bool
109109

110110
foreach ($this->toIterable($files) as $file) {
111111
if (\strlen($file) > $maxPathLength) {
112-
throw new IOException(sprintf('Could not check if file exist because path length exceeds %d characters.', $maxPathLength), 0, null, $file);
112+
throw new IOException(\sprintf('Could not check if file exist because path length exceeds %d characters.', $maxPathLength), 0, null, $file);
113113
}
114114

115115
if (!file_exists($file)) {
@@ -132,7 +132,7 @@ public function touch(string|iterable $files, ?int $time = null, ?int $atime = n
132132
{
133133
foreach ($this->toIterable($files) as $file) {
134134
if (!($time ? self::box('touch', $file, $time, $atime) : self::box('touch', $file))) {
135-
throw new IOException(sprintf('Failed to touch "%s": ', $file).self::$lastError, 0, null, $file);
135+
throw new IOException(\sprintf('Failed to touch "%s": ', $file).self::$lastError, 0, null, $file);
136136
}
137137
}
138138
}
@@ -160,7 +160,7 @@ private static function doRemove(array $files, bool $isRecursive): void
160160
if (is_link($file)) {
161161
// See https://bugs.php.net/52176
162162
if (!(self::box('unlink', $file) || '\\' !== \DIRECTORY_SEPARATOR || self::box('rmdir', $file)) && file_exists($file)) {
163-
throw new IOException(sprintf('Failed to remove symlink "%s": ', $file).self::$lastError);
163+
throw new IOException(\sprintf('Failed to remove symlink "%s": ', $file).self::$lastError);
164164
}
165165
} elseif (is_dir($file)) {
166166
if (!$isRecursive) {
@@ -191,10 +191,10 @@ private static function doRemove(array $files, bool $isRecursive): void
191191
$file = $origFile;
192192
}
193193

194-
throw new IOException(sprintf('Failed to remove directory "%s": ', $file).$lastError);
194+
throw new IOException(\sprintf('Failed to remove directory "%s": ', $file).$lastError);
195195
}
196196
} elseif (!self::box('unlink', $file) && ((self::$lastError && str_contains(self::$lastError, 'Permission denied')) || file_exists($file))) {
197-
throw new IOException(sprintf('Failed to remove file "%s": ', $file).self::$lastError);
197+
throw new IOException(\sprintf('Failed to remove file "%s": ', $file).self::$lastError);
198198
}
199199
}
200200
}
@@ -212,7 +212,7 @@ public function chmod(string|iterable $files, int $mode, int $umask = 0000, bool
212212
{
213213
foreach ($this->toIterable($files) as $file) {
214214
if (!self::box('chmod', $file, $mode & ~$umask)) {
215-
throw new IOException(sprintf('Failed to chmod file "%s": ', $file).self::$lastError, 0, null, $file);
215+
throw new IOException(\sprintf('Failed to chmod file "%s": ', $file).self::$lastError, 0, null, $file);
216216
}
217217
if ($recursive && is_dir($file) && !is_link($file)) {
218218
$this->chmod(new \FilesystemIterator($file), $mode, $umask, true);
@@ -236,11 +236,11 @@ public function chown(string|iterable $files, string|int $user, bool $recursive
236236
}
237237
if (is_link($file) && \function_exists('lchown')) {
238238
if (!self::box('lchown', $file, $user)) {
239-
throw new IOException(sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, $file);
239+
throw new IOException(\sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, $file);
240240
}
241241
} else {
242242
if (!self::box('chown', $file, $user)) {
243-
throw new IOException(sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, $file);
243+
throw new IOException(\sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, $file);
244244
}
245245
}
246246
}
@@ -262,11 +262,11 @@ public function chgrp(string|iterable $files, string|int $group, bool $recursive
262262
}
263263
if (is_link($file) && \function_exists('lchgrp')) {
264264
if (!self::box('lchgrp', $file, $group)) {
265-
throw new IOException(sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, $file);
265+
throw new IOException(\sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, $file);
266266
}
267267
} else {
268268
if (!self::box('chgrp', $file, $group)) {
269-
throw new IOException(sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, $file);
269+
throw new IOException(\sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, $file);
270270
}
271271
}
272272
}
@@ -282,7 +282,7 @@ public function rename(string $origin, string $target, bool $overwrite = false):
282282
{
283283
// we check that target does not exist
284284
if (!$overwrite && $this->isReadable($target)) {
285-
throw new IOException(sprintf('Cannot rename because the target "%s" already exists.', $target), 0, null, $target);
285+
throw new IOException(\sprintf('Cannot rename because the target "%s" already exists.', $target), 0, null, $target);
286286
}
287287

288288
if (!self::box('rename', $origin, $target)) {
@@ -293,7 +293,7 @@ public function rename(string $origin, string $target, bool $overwrite = false):
293293

294294
return;
295295
}
296-
throw new IOException(sprintf('Cannot rename "%s" to "%s": ', $origin, $target).self::$lastError, 0, null, $target);
296+
throw new IOException(\sprintf('Cannot rename "%s" to "%s": ', $origin, $target).self::$lastError, 0, null, $target);
297297
}
298298
}
299299

@@ -307,7 +307,7 @@ private function isReadable(string $filename): bool
307307
$maxPathLength = \PHP_MAXPATHLEN - 2;
308308

309309
if (\strlen($filename) > $maxPathLength) {
310-
throw new IOException(sprintf('Could not check if file is readable because path length exceeds %d characters.', $maxPathLength), 0, null, $filename);
310+
throw new IOException(\sprintf('Could not check if file is readable because path length exceeds %d characters.', $maxPathLength), 0, null, $filename);
311311
}
312312

313313
return is_readable($filename);
@@ -364,7 +364,7 @@ public function hardlink(string $originFile, string|iterable $targetFiles): void
364364
}
365365

366366
if (!is_file($originFile)) {
367-
throw new FileNotFoundException(sprintf('Origin file "%s" is not a file.', $originFile));
367+
throw new FileNotFoundException(\sprintf('Origin file "%s" is not a file.', $originFile));
368368
}
369369

370370
foreach ($this->toIterable($targetFiles) as $targetFile) {
@@ -388,10 +388,10 @@ private function linkException(string $origin, string $target, string $linkType)
388388
{
389389
if (self::$lastError) {
390390
if ('\\' === \DIRECTORY_SEPARATOR && str_contains(self::$lastError, 'error code(1314)')) {
391-
throw new IOException(sprintf('Unable to create "%s" link due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?', $linkType), 0, null, $target);
391+
throw new IOException(\sprintf('Unable to create "%s" link due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?', $linkType), 0, null, $target);
392392
}
393393
}
394-
throw new IOException(sprintf('Failed to create "%s" link from "%s" to "%s": ', $linkType, $origin, $target).self::$lastError, 0, null, $target);
394+
throw new IOException(\sprintf('Failed to create "%s" link from "%s" to "%s": ', $linkType, $origin, $target).self::$lastError, 0, null, $target);
395395
}
396396

397397
/**
@@ -428,11 +428,11 @@ public function readlink(string $path, bool $canonicalize = false): ?string
428428
public function makePathRelative(string $endPath, string $startPath): string
429429
{
430430
if (!$this->isAbsolutePath($startPath)) {
431-
throw new InvalidArgumentException(sprintf('The start path "%s" is not absolute.', $startPath));
431+
throw new InvalidArgumentException(\sprintf('The start path "%s" is not absolute.', $startPath));
432432
}
433433

434434
if (!$this->isAbsolutePath($endPath)) {
435-
throw new InvalidArgumentException(sprintf('The end path "%s" is not absolute.', $endPath));
435+
throw new InvalidArgumentException(\sprintf('The end path "%s" is not absolute.', $endPath));
436436
}
437437

438438
// Normalize separators on Windows
@@ -518,7 +518,7 @@ public function mirror(string $originDir, string $targetDir, ?\Traversable $iter
518518
$originDirLen = \strlen($originDir);
519519

520520
if (!$this->exists($originDir)) {
521-
throw new IOException(sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir);
521+
throw new IOException(\sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir);
522522
}
523523

524524
// Iterate in destination folder to remove obsolete entries
@@ -562,7 +562,7 @@ public function mirror(string $originDir, string $targetDir, ?\Traversable $iter
562562
} elseif (is_file($file)) {
563563
$this->copy($file, $target, $options['override'] ?? false);
564564
} else {
565-
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
565+
throw new IOException(\sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
566566
}
567567
}
568568
}
@@ -638,7 +638,7 @@ public function tempnam(string $dir, string $prefix, string $suffix = ''): strin
638638
public function dumpFile(string $filename, $content): void
639639
{
640640
if (\is_array($content)) {
641-
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
641+
throw new \TypeError(\sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
642642
}
643643

644644
$dir = \dirname($filename);
@@ -659,7 +659,7 @@ public function dumpFile(string $filename, $content): void
659659

660660
try {
661661
if (false === self::box('file_put_contents', $tmpFile, $content)) {
662-
throw new IOException(sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
662+
throw new IOException(\sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
663663
}
664664

665665
self::box('chmod', $tmpFile, self::box('fileperms', $filename) ?: 0666 & ~umask());
@@ -683,7 +683,7 @@ public function dumpFile(string $filename, $content): void
683683
public function appendToFile(string $filename, $content, bool $lock = false): void
684684
{
685685
if (\is_array($content)) {
686-
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
686+
throw new \TypeError(\sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
687687
}
688688

689689
$dir = \dirname($filename);
@@ -693,7 +693,7 @@ public function appendToFile(string $filename, $content, bool $lock = false): vo
693693
}
694694

695695
if (false === self::box('file_put_contents', $filename, $content, \FILE_APPEND | ($lock ? \LOCK_EX : 0))) {
696-
throw new IOException(sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
696+
throw new IOException(\sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
697697
}
698698
}
699699

@@ -705,12 +705,12 @@ public function appendToFile(string $filename, $content, bool $lock = false): vo
705705
public function readFile(string $filename): string
706706
{
707707
if (is_dir($filename)) {
708-
throw new IOException(sprintf('Failed to read file "%s": File is a directory.', $filename));
708+
throw new IOException(\sprintf('Failed to read file "%s": File is a directory.', $filename));
709709
}
710710

711711
$content = self::box('file_get_contents', $filename);
712712
if (false === $content) {
713-
throw new IOException(sprintf('Failed to read file "%s": ', $filename).self::$lastError, 0, null, $filename);
713+
throw new IOException(\sprintf('Failed to read file "%s": ', $filename).self::$lastError, 0, null, $filename);
714714
}
715715

716716
return $content;
@@ -734,7 +734,7 @@ private function getSchemeAndHierarchy(string $filename): array
734734
private static function assertFunctionExists(string $func): void
735735
{
736736
if (!\function_exists($func)) {
737-
throw new IOException(sprintf('Unable to perform filesystem operation because the "%s()" function has been disabled.', $func));
737+
throw new IOException(\sprintf('Unable to perform filesystem operation because the "%s()" function has been disabled.', $func));
738738
}
739739
}
740740

Path.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,11 @@ public static function isRelative(string $path): bool
437437
public static function makeAbsolute(string $path, string $basePath): string
438438
{
439439
if ('' === $basePath) {
440-
throw new InvalidArgumentException(sprintf('The base path must be a non-empty string. Got: "%s".', $basePath));
440+
throw new InvalidArgumentException(\sprintf('The base path must be a non-empty string. Got: "%s".', $basePath));
441441
}
442442

443443
if (!self::isAbsolute($basePath)) {
444-
throw new InvalidArgumentException(sprintf('The base path "%s" is not an absolute path.', $basePath));
444+
throw new InvalidArgumentException(\sprintf('The base path "%s" is not an absolute path.', $basePath));
445445
}
446446

447447
if (self::isAbsolute($path)) {
@@ -531,12 +531,12 @@ public static function makeRelative(string $path, string $basePath): string
531531
// If the passed path is absolute, but the base path is not, we
532532
// cannot generate a relative path
533533
if ('' !== $root && '' === $baseRoot) {
534-
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));
534+
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));
535535
}
536536

537537
// Fail if the roots of the two paths are different
538538
if ($baseRoot && $root !== $baseRoot) {
539-
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));
539+
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));
540540
}
541541

542542
if ('' === $relativeBasePath) {

Tests/FilesystemTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1823,15 +1823,15 @@ public function testReadFile()
18231823
public function testReadNonExistentFile()
18241824
{
18251825
$this->expectException(IOException::class);
1826-
$this->expectExceptionMessageMatches(sprintf('#^Failed to read file ".+%1$sTests/invalid"\\: file_get_contents\\(.+%1$sTests/invalid\\)\\: Failed to open stream\\: No such file or directory$#', preg_quote(\DIRECTORY_SEPARATOR)));
1826+
$this->expectExceptionMessageMatches(\sprintf('#^Failed to read file ".+%1$sTests/invalid"\\: file_get_contents\\(.+%1$sTests/invalid\\)\\: Failed to open stream\\: No such file or directory$#', preg_quote(\DIRECTORY_SEPARATOR)));
18271827

18281828
$this->filesystem->readFile(__DIR__.'/invalid');
18291829
}
18301830

18311831
public function testReadDirectory()
18321832
{
18331833
$this->expectException(IOException::class);
1834-
$this->expectExceptionMessageMatches(sprintf('#^Failed to read file ".+%sTests"\\: File is a directory\\.$#', preg_quote(\DIRECTORY_SEPARATOR)));
1834+
$this->expectExceptionMessageMatches(\sprintf('#^Failed to read file ".+%sTests"\\: File is a directory\\.$#', preg_quote(\DIRECTORY_SEPARATOR)));
18351835

18361836
$this->filesystem->readFile(__DIR__);
18371837
}

0 commit comments

Comments
 (0)