Skip to content

Commit a3e0453

Browse files
committed
Fixed an issue that could occurr when checking files on network drives (ref #2965)
1 parent 9ff2b30 commit a3e0453

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

package.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
3838
-- Thanks to Vincent Langlet for the patch
3939
- The PSR2.Methods.FunctionCallSignature.SpaceBeforeCloseBracket error message is now reported on the closing parenthesis token
4040
-- Previously, the error was being reported on the function keyword, leading to confusing line numbers in the error report
41+
- Fixed an issue that could occurr when checking files on network drives, such as with WSL2 on Windows 10
42+
-- This works around a long-standing PHP bug with is_readable()
43+
-- Thanks to Michael S for the patch
4144
</notes>
4245
<contents>
4346
<dir name="/">

src/Files/LocalFile.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHP_CodeSniffer\Ruleset;
1313
use PHP_CodeSniffer\Config;
1414
use PHP_CodeSniffer\Util\Cache;
15+
use PHP_CodeSniffer\Util\Common;
1516

1617
class LocalFile extends File
1718
{
@@ -29,7 +30,7 @@ class LocalFile extends File
2930
public function __construct($path, Ruleset $ruleset, Config $config)
3031
{
3132
$this->path = trim($path);
32-
if (is_readable($this->path) === false) {
33+
if (Common::isReadable($this->path) === false) {
3334
parent::__construct($this->path, $ruleset, $config);
3435
$error = 'Error opening file; file no longer exists or you do not have access to read the file';
3536
$this->addMessage(true, $error, 1, 1, 'Internal.LocalFile', [], 5, false);

src/Util/Common.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ public static function isPharFile($path)
4848
}//end isPharFile()
4949

5050

51+
/**
52+
* Checks if a file is readable.
53+
*
54+
* Addresses PHP bug related to reading files from network drives on Windows.
55+
* e.g. when using WSL2.
56+
*
57+
* @param string $path The path to the file.
58+
*
59+
* @return boolean
60+
*/
61+
public static function isReadable($path)
62+
{
63+
if (is_readable($path) === true) {
64+
return true;
65+
}
66+
67+
if (file_exists($path) === true && is_file($path) === true) {
68+
$f = @fopen($path, 'rb');
69+
if (fclose($f) === true) {
70+
return true;
71+
}
72+
}
73+
74+
return false;
75+
76+
}//end isReadable()
77+
78+
5179
/**
5280
* CodeSniffer alternative for realpath.
5381
*

0 commit comments

Comments
 (0)