Skip to content

Commit cd86544

Browse files
author
Joseph Lenton
committed
added minor semi-colon detection
Instead of an 'unexpected }', you now get a semi-colon error, if it's obvious there should be one. It doesn't try to find every case, just the obvious ones.
1 parent b78b9a7 commit cd86544

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

src/php_error.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ class ErrorHandler
241241

242242
const REGEX_VARIABLE = '/\b[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/';
243243

244+
const REGEX_MISSING_SEMI_COLON_FOLLOWING_LINE = '/^ *(return|}|if|while|foreach|for|switch)/';
245+
244246
/**
245247
* The number of lines to take from the file,
246248
* where the error is reported. This is the number
@@ -1784,29 +1786,48 @@ private function improveErrorMessage( $ex, $code, $message, $errLine, $errFile,
17841786
if ( $message === "syntax error, unexpected T_ENCAPSED_AND_WHITESPACE" ) {
17851787
$message = "syntax error, string is not closed";
17861788
} else {
1787-
$matches = array();
1788-
$num = preg_match( '/\bunexpected ([A-Z_]+|\\$end)\b/', $message, $matches );
1789-
1790-
if ( $num > 0 ) {
1791-
$match = $matches[0];
1792-
$newSymbol = ErrorHandler::phpSymbolToDescription( str_replace('unexpected ', '', $match) );
1793-
1794-
$message = str_replace( $match, "unexpected $newSymbol", $message );
1789+
$semiColonError = false;
1790+
if ( strpos($message, 'syntax error,') === 0 && $errLine > 2 ) {
1791+
$lines = ErrorHandler::getFileContents( $errFile );
1792+
1793+
$line = $lines[$errLine-1];
1794+
if ( preg_match( ErrorHandler::REGEX_MISSING_SEMI_COLON_FOLLOWING_LINE, $line ) !== 0 ) {
1795+
$content = rtrim( join( "\n", array_slice($lines, 0, $errLine-1) ) );
1796+
1797+
if ( strrpos($content, ';') !== strlen($content)-1 ) {
1798+
$message = "Missing semi-colon";
1799+
$errLine--;
1800+
$srcErrLine = $errLine;
1801+
$semiColonError = true;
1802+
}
1803+
}
17951804
}
17961805

1797-
$matches = array();
1798-
$num = preg_match( '/, expecting ([A-Z_]+|\\$end)( or ([A-Z_]+|\\$end))*/', $message, $matches );
1806+
if ( $semiColonError ) {
1807+
$matches = array();
1808+
$num = preg_match( '/\bunexpected ([A-Z_]+|\\$end)\b/', $message, $matches );
1809+
1810+
if ( $num > 0 ) {
1811+
$match = $matches[0];
1812+
$newSymbol = ErrorHandler::phpSymbolToDescription( str_replace('unexpected ', '', $match) );
17991813

1800-
if ( $num > 0 ) {
1801-
$match = $matches[0];
1802-
$newMatch = str_replace( ", expecting ", '', $match );
1803-
$symbols = explode( ' or ', $newMatch );
1804-
foreach ( $symbols as $i => $sym ) {
1805-
$symbols[$i] = ErrorHandler::phpSymbolToDescription( $sym );
1814+
$message = str_replace( $match, "unexpected $newSymbol", $message );
18061815
}
1807-
$newMatch = join( ', or ', $symbols );
18081816

1809-
$message = str_replace( $match, ", expecting $newMatch", $message );
1817+
$matches = array();
1818+
$num = preg_match( '/, expecting ([A-Z_]+|\\$end)( or ([A-Z_]+|\\$end))*/', $message, $matches );
1819+
1820+
if ( $num > 0 ) {
1821+
$match = $matches[0];
1822+
$newMatch = str_replace( ", expecting ", '', $match );
1823+
$symbols = explode( ' or ', $newMatch );
1824+
foreach ( $symbols as $i => $sym ) {
1825+
$symbols[$i] = ErrorHandler::phpSymbolToDescription( $sym );
1826+
}
1827+
$newMatch = join( ', or ', $symbols );
1828+
1829+
$message = str_replace( $match, ", expecting $newMatch", $message );
1830+
}
18101831
}
18111832
}
18121833
/**

0 commit comments

Comments
 (0)