summaryrefslogtreecommitdiffstats
path: root/lib/mysql.php
diff options
context:
space:
mode:
authorDavey Shafik <me@daveyshafik.com>2017-01-08 16:34:23 -0800
committerDavey Shafik <me@daveyshafik.com>2017-01-08 16:34:23 -0800
commit6e950e6eea5ff662483e9c90847aabc223dfeeb3 (patch)
tree0d495a965321c204a89f4cc9a012eecdfa7459b1 /lib/mysql.php
parent8fee9378cf84a89c19a39042d8ce06c5850caf84 (diff)
downloadphp7-mysql-shim-6e950e6eea5ff662483e9c90847aabc223dfeeb3.zip
php7-mysql-shim-6e950e6eea5ff662483e9c90847aabc223dfeeb3.tar.gz
php7-mysql-shim-6e950e6eea5ff662483e9c90847aabc223dfeeb3.tar.bz2
Code cleanup
Diffstat (limited to 'lib/mysql.php')
-rw-r--r--lib/mysql.php121
1 files changed, 61 insertions, 60 deletions
diff --git a/lib/mysql.php b/lib/mysql.php
index f2908a0..0207d1a 100644
--- a/lib/mysql.php
+++ b/lib/mysql.php
@@ -45,7 +45,7 @@ namespace {
$hash = sha1($hostname . $username . $flags);
/* persistent connections start with p: */
- if ($hostname{1} != ':' && isset(\Dshafik\MySQL::$connections[$hash])) {
+ if ($hostname{1} !== ':' && isset(\Dshafik\MySQL::$connections[$hash])) {
\Dshafik\MySQL::$last_connection = \Dshafik\MySQL::$connections[$hash]['conn'];
\Dshafik\MySQL::$connections[$hash]['refcount'] += 1;
return \Dshafik\MySQL::$connections[$hash]['conn'];
@@ -126,7 +126,7 @@ namespace {
}
$return = true;
- if (\Dshafik\MySQL::$connections[$link->hash]['refcount'] == 0) {
+ if (\Dshafik\MySQL::$connections[$link->hash]['refcount'] === 0) {
$return = mysqli_close($link);
unset(\Dshafik\MySQL::$connections[$link->hash]);
}
@@ -144,7 +144,7 @@ namespace {
return mysqli_query(
$link,
- "USE `" . mysqli_real_escape_string($link, $databaseName) . "`"
+ 'USE `' . mysqli_real_escape_string($link, $databaseName) . '`'
) !== false;
}
@@ -173,14 +173,14 @@ namespace {
function mysql_list_dbs(\mysqli $link = null)
{
- return mysql_query("SHOW DATABASES", $link);
+ return mysql_query('SHOW DATABASES', $link);
}
function mysql_list_tables($databaseName, \mysqli $link = null)
{
$link = \Dshafik\MySQL::getConnection($link);
$query = sprintf(
- "SHOW TABLES FROM `%s`",
+ 'SHOW TABLES FROM `%s`',
mysql_real_escape_string($databaseName, $link)
);
return mysql_query($query, $link);
@@ -191,7 +191,7 @@ namespace {
$link = \Dshafik\MySQL::getConnection($link);
$query = sprintf(
- "SHOW COLUMNS FROM `%s`.`%s`",
+ 'SHOW COLUMNS FROM `%s`.`%s`',
mysqli_real_escape_string($link, $databaseName),
mysqli_real_escape_string($link, $tableName)
);
@@ -203,7 +203,7 @@ namespace {
return $result;
}
- trigger_error("mysql_list_fields(): Unable to save MySQL query result", E_USER_WARNING);
+ trigger_error('mysql_list_fields(): Unable to save MySQL query result', E_USER_WARNING);
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -211,7 +211,7 @@ namespace {
function mysql_list_processes(\mysqli $link = null)
{
- return mysql_query("SHOW PROCESSLIST", $link);
+ return mysql_query('SHOW PROCESSLIST', $link);
}
function mysql_error(\mysqli $link = null)
@@ -236,7 +236,7 @@ namespace {
function mysql_result($result, $row, $field = 0)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -245,7 +245,7 @@ namespace {
if (!mysqli_data_seek($result, $row)) {
trigger_error(
sprintf(
- "mysql_result(): Unable to jump to row %d on MySQL result index %s",
+ 'mysql_result(): Unable to jump to row %d on MySQL result index %s',
$row,
spl_object_hash($result)
),
@@ -257,13 +257,13 @@ namespace {
}
$found = true;
- if (strpos($field, ".") !== false) {
- list($table, $name) = explode(".", $field);
+ if (strpos($field, '.') !== false) {
+ list($table, $name) = explode('.', $field);
$i = 0;
$found = false;
mysqli_field_seek($result, 0);
while ($column = mysqli_fetch_field($result)) {
- if ($column->table == $table && $column->name == $name) {
+ if ($column->table === $table && $column->name === $name) {
$field = $i;
$found = true;
break;
@@ -279,7 +279,7 @@ namespace {
trigger_error(
sprintf(
- "%s(): %s not found in MySQL result index %s",
+ '%s(): %s not found in MySQL result index %s',
__FUNCTION__,
$field,
spl_object_hash($result)
@@ -293,7 +293,7 @@ namespace {
function mysql_num_rows($result)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -308,7 +308,7 @@ namespace {
function mysql_num_fields($result)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -318,7 +318,7 @@ namespace {
function mysql_fetch_row($result)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -328,7 +328,7 @@ namespace {
function mysql_fetch_array($result, $resultType = MYSQL_BOTH)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -338,7 +338,7 @@ namespace {
function mysql_fetch_assoc($result) /* : array|null */
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -349,7 +349,7 @@ namespace {
function mysql_fetch_object($result, $class = null, array $params = array()) /* : object|null */
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -366,7 +366,7 @@ namespace {
function mysql_data_seek($result, $offset)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -376,7 +376,7 @@ namespace {
function mysql_fetch_lengths($result) /* : array|*/
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -386,7 +386,7 @@ namespace {
function mysql_fetch_field($result) /* : object|*/
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -396,7 +396,7 @@ namespace {
function mysql_field_seek($result, $field)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -406,7 +406,7 @@ namespace {
function mysql_free_result($result)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -416,7 +416,7 @@ namespace {
function mysql_field_name($result, $field)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -426,7 +426,7 @@ namespace {
function mysql_field_table($result, $field)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -436,7 +436,7 @@ namespace {
function mysql_field_len($result, $field)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -446,7 +446,7 @@ namespace {
function mysql_field_type($result, $field)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -456,7 +456,7 @@ namespace {
function mysql_field_flags($result, $field)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -469,7 +469,7 @@ namespace {
if (\Dshafik\MySQL::$last_connection === null) {
trigger_error(
sprintf(
- "%s() is insecure; use mysql_real_escape_string() instead!",
+ '%s() is insecure; use mysql_real_escape_string() instead!',
__FUNCTION__
),
E_USER_NOTICE
@@ -537,7 +537,7 @@ namespace {
function mysql_db_name($result, $row, $field = 0)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -549,7 +549,7 @@ namespace {
function mysql_tablename($result, $row)
{
- if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
+ if (!\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
@@ -647,9 +647,9 @@ namespace Dshafik {
}
if (static::$last_connection === null) {
- $err = "A link to the server could not be established";
+ $err = 'A link to the server could not be established';
if ($func !== null) {
- $err = $func . "(): no MySQL-Link resource supplied";
+ $err = $func . '(): no MySQL-Link resource supplied';
}
trigger_error($err, E_USER_WARNING);
return false;
@@ -665,7 +665,7 @@ namespace Dshafik {
} catch (\Exception $e) {
trigger_error(
sprintf(
- "mysql_field_%s(): Field %d is invalid for MySQL result index %s",
+ 'mysql_field_%s(): Field %d is invalid for MySQL result index %s',
$what,
$field,
spl_object_hash($result)
@@ -678,19 +678,19 @@ namespace Dshafik {
// @codeCoverageIgnoreEnd
}
- if ($what == 'name' || $what == 'table') {
+ if ($what === 'name' || $what === 'table') {
return $field->{$what};
}
- if ($what == 'len') {
+ if ($what === 'len') {
return $field->length;
}
- if ($what == 'type') {
+ if ($what === 'type') {
return static::getFieldType($field->type);
}
- if ($what == 'flags') {
+ if ($what === 'flags') {
return static::getFieldFlags($field->flags);
}
@@ -700,28 +700,29 @@ namespace Dshafik {
public static function checkValidResult($result, $function)
{
if (!($result instanceof \mysqli_result)) {
- if ($function != "mysql_fetch_object") {
+ 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, ' . strtolower(gettype($result)) . ' given',
E_USER_WARNING
);
}
- if ($function == "mysql_fetch_object") {
+ 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',
E_USER_WARNING
);
}
return false;
}
+ return true;
}
public static function escapeString($unescapedString)
{
- $escapedString = "";
- for ($i = 0; $i < strlen($unescapedString); $i++) {
+ $escapedString = '';
+ for ($i = 0, $max = strlen($unescapedString); $i < $max; $i++) {
$escapedString .= self::escapeChar($unescapedString{$i});
}
@@ -732,18 +733,18 @@ namespace Dshafik {
{
// Order of flags taken from http://lxr.php.net/xref/PHP_5_6/ext/mysql/php_mysql.c#2507
$flags = array(
- MYSQLI_NOT_NULL_FLAG => "not_null",
- MYSQLI_PRI_KEY_FLAG => "primary_key",
- MYSQLI_UNIQUE_KEY_FLAG => "unique_key",
- MYSQLI_MULTIPLE_KEY_FLAG => "multiple_key",
- MYSQLI_BLOB_FLAG => "blob",
- MYSQLI_UNSIGNED_FLAG => "unsigned",
- MYSQLI_ZEROFILL_FLAG => "zerofill",
- MYSQLI_BINARY_FLAG => "binary",
- MYSQLI_ENUM_FLAG => "enum",
- MYSQLI_SET_FLAG => "set",
- MYSQLI_AUTO_INCREMENT_FLAG => "auto_increment",
- MYSQLI_TIMESTAMP_FLAG => "timestamp",
+ MYSQLI_NOT_NULL_FLAG => 'not_null',
+ MYSQLI_PRI_KEY_FLAG => 'primary_key',
+ MYSQLI_UNIQUE_KEY_FLAG => 'unique_key',
+ MYSQLI_MULTIPLE_KEY_FLAG => 'multiple_key',
+ MYSQLI_BLOB_FLAG => 'blob',
+ MYSQLI_UNSIGNED_FLAG => 'unsigned',
+ MYSQLI_ZEROFILL_FLAG => 'zerofill',
+ MYSQLI_BINARY_FLAG => 'binary',
+ MYSQLI_ENUM_FLAG => 'enum',
+ MYSQLI_SET_FLAG => 'set',
+ MYSQLI_AUTO_INCREMENT_FLAG => 'auto_increment',
+ MYSQLI_TIMESTAMP_FLAG => 'timestamp',
);
$fieldFlags = array();
@@ -753,7 +754,7 @@ namespace Dshafik {
}
}
- return implode(" ", $fieldFlags);
+ return implode(' ', $fieldFlags);
}
protected static function getFieldType($what)
@@ -793,7 +794,7 @@ namespace Dshafik {
MYSQLI_TYPE_GEOMETRY => 'geometry',
);
- return isset($types[$what]) ? $types[$what] : "unknown";
+ return isset($types[$what]) ? $types[$what] : 'unknown';
}
protected static function escapeChar($char)