diff options
author | Davey Shafik <me@daveyshafik.com> | 2020-10-25 17:30:14 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-25 17:30:14 -0700 |
commit | 033ec6b1e3c07276ad801396e39c1a783fd9105b (patch) | |
tree | fe1e960b3ff08394151b268618a2aab4c9d0c0c0 | |
parent | 697e0eb466aa474ff054d7e6908ab873d77dcdcf (diff) | |
parent | 709444274c9e87383c3728ee347607e4ae6c7906 (diff) | |
download | php7-mysql-shim-033ec6b1e3c07276ad801396e39c1a783fd9105b.zip php7-mysql-shim-033ec6b1e3c07276ad801396e39c1a783fd9105b.tar.gz php7-mysql-shim-033ec6b1e3c07276ad801396e39c1a783fd9105b.tar.bz2 |
Merge pull request #54 from SWsistemas/master
Adds backtrace for illegal argument supplied warning
-rw-r--r-- | lib/mysql.php | 22 | ||||
-rw-r--r-- | tests/MySqlShimTest.php | 9 |
2 files changed, 29 insertions, 2 deletions
diff --git a/lib/mysql.php b/lib/mysql.php index a0ebb5a..d05d7a8 100644 --- a/lib/mysql.php +++ b/lib/mysql.php @@ -734,16 +734,34 @@ namespace Dshafik { public static function checkValidResult($result, $function) { if (!($result instanceof \mysqli_result)) { + $type = strtolower(gettype($result)); + $file = ""; + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $backtraceIndex = 0; + + /** + * Iterate through backtrace until finding a backtrace with an origin + * Some methods may not leave file and line metadata like call_user_func_array and __call + */ + do { + $currentBacktrace = $backtrace[$backtraceIndex]; + $callerHasFileAndLine = isset($currentBacktrace['file'], $currentBacktrace['line']); + + if ($callerHasFileAndLine && $currentBacktrace['file'] != __FILE__) { + $file = $currentBacktrace['file'] . ':' . $currentBacktrace['line']; + } + } while ($backtraceIndex++ < count($backtrace) && $file == ""); + if ($function !== 'mysql_fetch_object') { trigger_error( - $function . '() expects parameter 1 to be resource, ' . strtolower(gettype($result)) . ' given', + "$function() expects parameter 1 to be resource, $type given on $file", E_USER_WARNING ); } if ($function === 'mysql_fetch_object') { trigger_error( - $function . '(): supplied argument is not a valid MySQL result resource', + "$function(): supplied argument is not a valid MySQL result resource on $file", E_USER_WARNING ); } diff --git a/tests/MySqlShimTest.php b/tests/MySqlShimTest.php index ea9b2c3..1a63aad 100644 --- a/tests/MySqlShimTest.php +++ b/tests/MySqlShimTest.php @@ -688,11 +688,20 @@ class MySqlShimTest extends \PHPUnit\Framework\TestCase if ($args !== array()) { array_unshift($args, null); + $this->expectWarningMessageMatches('@' . __FILE__ . ':' . (__LINE__ + 1) . '@'); call_user_func_array($function, $args); } + $this->expectWarningMessageMatches('@' . __FILE__ . ':' . (__LINE__ + 1) . '@'); call_user_func($function, null); } + public function test_mysql_check_valid_result_backtraces_here() + { + $this->expectWarning(); + $this->expectWarningMessage(__FUNCTION__ . "() expects parameter 1 to be resource, boolean given on " . __FILE__ . ':' . (__LINE__ + 1)); + \Dshafik\MySQL::checkValidResult(false, __FUNCTION__); + } + /** * @dataProvider mysql_fetch_DataProvider */ |