* performance and optimisations fixes part 2, according to awesome PHPStorm Php Inspections plugin!

This commit is contained in:
slawkens
2018-05-29 21:16:48 +02:00
parent 857aa110c8
commit 68d74a490b
4 changed files with 78 additions and 61 deletions

View File

@@ -453,8 +453,8 @@ class POT
*
* @version 0.1.5
* @since 0.0.5
* @param string $ip IP to ban.
* @param string $mask Mask for ban (by default bans only given IP).
* @param mixed|string $ip IP to ban.
* @param mixed|string $mask Mask for ban (by default bans only given IP).
* @param int $time Time for time until expires (0 - forever).
* @throws PDOException On PDO operation error.
* @deprecated 0.1.5 Use OTS_IPBan class.
@@ -463,7 +463,7 @@ class POT
{
// long2ip( ip2long('255.255.255.255') ) != '255.255.255.255' -.-'
// it's because that PHP integer types are signed
if($ip == '255.255.255.255')
if($ip === '255.255.255.255')
{
$ip = 4294967295;
}
@@ -472,7 +472,7 @@ class POT
$ip = sprintf('%u', ip2long($ip) );
}
if($mask == '255.255.255.255')
if($mask === '255.255.255.255')
{
$mask = 4294967295;
}
@@ -500,7 +500,7 @@ class POT
*
* @version 0.1.5
* @since 0.0.5
* @param string $ip IP to ban.
* @param mixed|string $ip IP to ban.
* @param string $mask Mask for ban (by default 255.255.255.255) - not used thought.
* @throws PDOException On PDO operation error.
* @deprecated 0.1.5 Use OTS_IPBan class.
@@ -509,7 +509,7 @@ class POT
{
// long2ip( ip2long('255.255.255.255') ) != '255.255.255.255' -.-'
// it's because that PHP integer types are signed
if($ip == '255.255.255.255')
if($ip === '255.255.255.255')
{
$ip = 4294967295;
}
@@ -531,7 +531,7 @@ class POT
*
* @version 0.1.5
* @since 0.0.5
* @param string $ip IP to ban.
* @param mixed|string $ip IP to ban.
* @return bool True if IP number is banned, false otherwise.
* @throws PDOException On PDO operation error.
* @deprecated 0.1.5 Use OTS_IPBan class.
@@ -540,7 +540,7 @@ class POT
{
// long2ip( ip2long('255.255.255.255') ) != '255.255.255.255' -.-'
// it's because that PHP integer types are signed
if($ip == '255.255.255.255')
if($ip === '255.255.255.255')
{
$ip = 4294967295;
}
@@ -551,7 +551,7 @@ class POT
// finds ban entry
$ban = new OTS_IPBan();
$ban->find($ip, $mask);
$ban->find($ip);
return $ban->isLoaded() && $ban->isActive() && ( $ban->getExpires() == 0 || $ban->getExpires() > time() );
}

View File

@@ -55,10 +55,8 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
{
$ret = '';
$argc = func_num_args();
$argv = func_get_args();
for($i = 0; $i < $argc; $i++) {
$ret .= $this->fieldName($argv[$i]) . ',';
foreach(func_get_args() as $v) {
$ret .= $this->fieldName($v) . ',';
}
$ret[strlen($ret) - 1] = '';
@@ -77,7 +75,7 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
}
/**
* @param stirng $string String to be quoted.
* @param string $string String to be quoted.
* @return string Quoted string.
* @deprecated 0.0.5 Use PDO::quote().
* @version 0.0.7
@@ -113,9 +111,11 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
$fields = array_keys($data);
$values = array_values($data);
$query = 'SELECT * FROM ' . $this->tableName($table) . ' WHERE (';
for ($i = 0; $i < count($fields); $i++)
$count = count($fields);
for ($i = 0; $i < $count; $i++)
$query.= $this->fieldName($fields[$i]).' = '.$this->quote($values[$i]).' AND ';
$query = substr($query, 0, strlen($query)-4);
$query = substr($query, 0, -4);
$query.=');';
$query = $this->query($query);
@@ -128,20 +128,25 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
$fields = array_keys($data);
$values = array_values($data);
$query = 'INSERT INTO ' . $this->tableName($table) . ' (';
foreach ($fields as $field)
foreach ($fields as $field) {
$query.= $this->fieldName($field).',';
}
$query = substr($query, 0, strlen($query) - 1);
$query = substr($query, 0, -1);
$query .= ') VALUES (';
foreach ($values as $value)
if ($value === null)
foreach ($values as $value) {
if ($value === null) {
$query .= 'NULL,';
else
}
else {
$query .= $this->quote($value).',';
}
}
$query = substr($query, 0, strlen($query) - 1);
$query = substr($query, 0, -1);
$query .= ')';
$this->query($query);
$this->exec($query);
return true;
}
@@ -153,7 +158,7 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
foreach ($fields as $field)
$query.= $this->fieldName($field).',';
$query = substr($query, 0, strlen($query) - 1);
$query = substr($query, 0, -1);
$query.= ') VALUES (';
foreach ($values as $value)
if ($value === null)
@@ -161,7 +166,7 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
else
$query.= $this->quote($value).',';
$query = substr($query, 0, strlen($query) - 1);
$query = substr($query, 0, -1);
$query .= ')';
$this->query($query);
@@ -174,24 +179,27 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
$values = array_values($data);
$query = 'UPDATE '.$this->tableName($table).' SET ';
for ($i = 0; $i < count($fields); $i++)
$count = count($fields);
for ($i = 0; $i < $count; $i++)
$query.= $this->fieldName($fields[$i]).' = '.$this->quote($values[$i]).', ';
$query = substr($query, 0, strlen($query)-2);
$query = substr($query, 0, -2);
$query.=' WHERE (';
$fields = array_keys($where);
$values = array_values($where);
for ($i = 0; $i < count($fields); $i++)
$count = count($fields);
for ($i = 0; $i < $count; $i++)
$query.= $this->fieldName($fields[$i]).' = '.$this->quote($values[$i]).' AND ';
$query = substr($query, 0, strlen($query)-4);
$query = substr($query, 0, -4);
if (isset($limit))
$query .=') LIMIT '.$limit.';';
else
$query .=');';
$this->query($query);
$this->exec($query);
return true;
}
@@ -201,21 +209,26 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
$values = array_values($data);
$query = 'DELETE FROM ' . $this->tableName($table) . ' WHERE (';
for ($i = 0; $i < count($fields); $i++)
$count = count($fields);
for ($i = 0; $i < $count; $i++) {
$query .= $this->fieldName($fields[$i]) . ' = ' . $this->quote($values[$i]) . ' AND ';
}
$query = substr($query, 0, strlen($query) - 4);
if ($limit > 0)
$query = substr($query, 0, -4);
if ($limit > 0) {
$query.=') LIMIT '.$limit.';';
else
}
else {
$query.=');';
}
$this->query($query);
$this->exec($query);
return true;
}
/**
* LIMIT/OFFSET clause for queries.
*
*
* @param int|bool $limit Limit of rows to be affected by query (false if no limit).
* @param int|bool $offset Number of rows to be skipped before applying query effects (false if no offset).
* @return string LIMIT/OFFSET SQL clause for query.

View File

@@ -105,7 +105,7 @@ class OTS_DB_MySQL extends OTS_Base_DB
$need_revalidation = true;
if($cache->fetch('database_checksum', $tmp) && $tmp) {
$tmp = unserialize($tmp);
if(sha1($config['database_host'] . '.' . $config['database_name']) == $tmp) {
if(sha1($config['database_host'] . '.' . $config['database_name']) === $tmp) {
$need_revalidation = false;
}
}
@@ -185,7 +185,7 @@ class OTS_DB_MySQL extends OTS_Base_DB
private function hasTableInternal($name) {
global $config;
return ($this->has_table_cache[$name] = $this->query("SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = " . $this->quote($config['database_name']) . " AND `TABLE_NAME` = " . $this->quote($name) . " LIMIT 1;")->rowCount() > 0);
return ($this->has_table_cache[$name] = $this->query('SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = ' . $this->quote($config['database_name']) . ' AND `TABLE_NAME` = ' . $this->quote($name) . ' LIMIT 1;')->rowCount() > 0);
}
public function hasColumn($table, $column) {
@@ -197,7 +197,7 @@ class OTS_DB_MySQL extends OTS_Base_DB
}
private function hasColumnInternal($table, $column) {
return $this->hasTable($table) && ($this->has_column_cache[$table . '.' . $column] = count($this->query("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "'")->fetchAll()) > 0);
return $this->hasTable($table) && ($this->has_column_cache[$table . '.' . $column] = count($this->query('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $column . "'")->fetchAll()) > 0);
}
public function revalidateCache() {