Skip to content

Commit 40729e9

Browse files
jrfnlgrogy
authored andcommitted
SyntaxError::getNormalizedMessage(): bug fix - "in file on line" doesn't always get stripped
In certain cases, PHP puts the full file name in the error message. In those cases the "in filename.php on line .." trailing part of the error message did not get stripped off. Also, in some cases, when Windows slashes are used in the file path, the "in filename.php on line .." trailing part of the error message may not get stripped off. This last case will be exceedingly rare as on Windows, those file paths are handled correctly and the chances of a non-Windows user passing a path using Windows slashes will be minuscule. Even so, I've fixed both cases by making the path handling in the function more robust. * When the initial stripping of the trailing part of the error message fails, it will be retried up to two times. * The first time, if Windows slashes would be found in the file path _after_ the `basename()` function has been applied, a "manual" basename extraction is done and the stripping of the trailing part is retried. * The second time, the full file path is used in a last attempt to strip the trailing part of the error message. Includes unit tests. Fixes 94
1 parent f4d3e1a commit 40729e9

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/Errors/SyntaxError.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class SyntaxError extends ParallelLintError
88
{
9+
const IN_ON_REGEX = '~ in %s on line [0-9]+$~';
10+
911
/** @var Blame */
1012
private $blame;
1113

@@ -29,8 +31,22 @@ public function getLine()
2931
*/
3032
public function getNormalizedMessage($translateTokens = false)
3133
{
32-
$message = preg_replace('~^(Parse|Fatal) error: (syntax error, )?~', '', $this->message);
33-
$message = preg_replace('~ in ' . preg_quote(basename($this->filePath), '~') . ' on line [0-9]+$~', '', $message);
34+
$message = preg_replace('~^(Parse|Fatal) error: (syntax error, )?~', '', $this->message);
35+
$baseName = basename($this->filePath);
36+
$regex = sprintf(self::IN_ON_REGEX, preg_quote($baseName, '~'));
37+
$message = preg_replace($regex, '', $message, -1, $count);
38+
39+
if ($count === 0 && strpos($baseName, '\\') !== false) {
40+
$baseName = ltrim(strrchr($this->filePath, '\\'), '\\');
41+
$regex = sprintf(self::IN_ON_REGEX, preg_quote($baseName, '~'));
42+
$message = preg_replace($regex, '', $message, -1, $count);
43+
}
44+
45+
if ($count === 0) {
46+
$regex = sprintf(self::IN_ON_REGEX, preg_quote($this->filePath, '~'));
47+
$message = preg_replace($regex, '', $message);
48+
}
49+
3450
$message = ucfirst($message);
3551

3652
if ($translateTokens) {

tests/Unit/Errors/SyntaxErrorGetNormalizeMessageTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ public function dataFilePathHandling()
7878
'filePath' => 'test~file.php',
7979
'fileName' => 'test~file.php',
8080
),
81+
'Full file path, linux slashes' => array(
82+
'filePath' => 'path/to/subdir/file.php',
83+
'fileName' => 'file.php',
84+
),
85+
'File path, windows slashes' => array(
86+
'filePath' => 'path\to\subdir\file.php',
87+
'fileName' => 'file.php',
88+
),
89+
'Absolute file path, windows slashes' => array(
90+
'filePath' => 'C:\path\to\subdir\file.php',
91+
'fileName' => 'C:\path\to\subdir\file.php',
92+
),
93+
'Relative file path, windows slashes' => array(
94+
'filePath' => '.\subdir\file.php',
95+
'fileName' => '.\subdir\file.php',
96+
),
97+
'Phar file name' => array(
98+
'filePath' => 'phar://031.phar.php/a.php',
99+
'fileName' => 'phar://031.phar.php/a.php',
100+
),
81101
);
82102
}
83103
}

0 commit comments

Comments
 (0)