Update code style issues, and fix deprecated dynamic method call

This commit is contained in:
Timothy Warren 2023-03-17 16:16:39 -04:00
parent 2d80d8ea15
commit 2fbccaedbd
13 changed files with 128 additions and 153 deletions

View File

@ -17,6 +17,8 @@
namespace Query; namespace Query;
use DomainException; use DomainException;
use PHPUnit\Framework\Attributes\CodeCoverageIgnore;
use Query\Exception\{BadDBDriverException, NonExistentConnectionException};
use stdClass; use stdClass;
/** /**
@ -37,8 +39,8 @@ final class ConnectionManager
/** /**
* Private constructor to prevent multiple instances * Private constructor to prevent multiple instances
* @codeCoverageIgnore
*/ */
#[CodeCoverageIgnore]
private function __construct() private function __construct()
{ {
} }
@ -108,7 +110,7 @@ final class ConnectionManager
} }
// You should actually connect before trying to get a connection... // You should actually connect before trying to get a connection...
throw new Exception\NonExistentConnectionException('The specified connection does not exist'); throw new NonExistentConnectionException('The specified connection does not exist');
} }
/** /**
@ -118,7 +120,7 @@ final class ConnectionManager
{ {
[$dsn, $dbType, $params, $options] = $this->parseParams($params); [$dsn, $dbType, $params, $options] = $this->parseParams($params);
$dbType = ucfirst($dbType); $dbType = ucfirst((string) $dbType);
$driver = "\\Query\\Drivers\\{$dbType}\\Driver"; $driver = "\\Query\\Drivers\\{$dbType}\\Driver";
// Create the database connection // Create the database connection
@ -156,14 +158,14 @@ final class ConnectionManager
public function parseParams(array|object $rawParams): array public function parseParams(array|object $rawParams): array
{ {
$params = (object) $rawParams; $params = (object) $rawParams;
$params->type = strtolower($params->type); $params->type = strtolower((string) $params->type);
$dbType = ($params->type === 'postgresql') ? 'pgsql' : $params->type; $dbType = ($params->type === 'postgresql') ? 'pgsql' : $params->type;
$dbType = ucfirst($dbType); $dbType = ucfirst($dbType);
// Make sure the class exists // Make sure the class exists
if ( ! class_exists("\\Query\\Drivers\\{$dbType}\\Driver")) if ( ! class_exists("\\Query\\Drivers\\{$dbType}\\Driver"))
{ {
throw new Exception\BadDBDriverException('Database driver does not exist, or is not supported'); throw new BadDBDriverException('Database driver does not exist, or is not supported');
} }
// Set additional PDO options // Set additional PDO options
@ -182,9 +184,8 @@ final class ConnectionManager
/** /**
* Create the dsn from the db type and params * Create the dsn from the db type and params
*
* @codeCoverageIgnore
*/ */
#[CodeCoverageIgnore]
private function createDsn(string $dbType, stdClass $params): string private function createDsn(string $dbType, stdClass $params): string
{ {
$pairs = []; $pairs = [];

View File

@ -19,6 +19,7 @@ namespace Query\Drivers;
use InvalidArgumentException; use InvalidArgumentException;
use PDO; use PDO;
use PDOStatement; use PDOStatement;
use PHPUnit\Framework\Attributes\CodeCoverageIgnore;
use function call_user_func_array; use function call_user_func_array;
use function dbFilter; use function dbFilter;
@ -87,9 +88,9 @@ abstract class AbstractDriver extends PDO implements DriverInterface
/** /**
* Allow invoke to work on table object * Allow invoke to work on table object
* *
* @codeCoverageIgnore
* @return mixed * @return mixed
*/ */
#[CodeCoverageIgnore]
public function __call(string $name, array $args = []) public function __call(string $name, array $args = [])
{ {
if ( if (
@ -109,7 +110,7 @@ abstract class AbstractDriver extends PDO implements DriverInterface
protected function _loadSubClasses(): void protected function _loadSubClasses(): void
{ {
// Load the sql and util class for the driver // Load the sql and util class for the driver
$thisClass = $this::class; $thisClass = static::class;
$nsArray = explode('\\', $thisClass); $nsArray = explode('\\', $thisClass);
array_pop($nsArray); array_pop($nsArray);
$driver = array_pop($nsArray); $driver = array_pop($nsArray);
@ -256,7 +257,7 @@ abstract class AbstractDriver extends PDO implements DriverInterface
{ {
if (is_array($identifier)) if (is_array($identifier))
{ {
return array_map([$this, __METHOD__], $identifier); return array_map(__METHOD__, $identifier);
} }
// Make all the string-handling methods happy // Make all the string-handling methods happy
@ -266,7 +267,7 @@ abstract class AbstractDriver extends PDO implements DriverInterface
if (str_contains($identifier, ',')) if (str_contains($identifier, ','))
{ {
$parts = array_map('mb_trim', explode(',', $identifier)); $parts = array_map('mb_trim', explode(',', $identifier));
$parts = array_map([$this, __METHOD__], $parts); $parts = array_map(__METHOD__, $parts);
$identifier = implode(',', $parts); $identifier = implode(',', $parts);
} }
@ -275,7 +276,7 @@ abstract class AbstractDriver extends PDO implements DriverInterface
$hiers = array_map('mb_trim', $hiers); $hiers = array_map('mb_trim', $hiers);
// Re-compile the string // Re-compile the string
$raw = implode('.', array_map([$this, '_quote'], $hiers)); $raw = implode('.', array_map($this->_quote(...), $hiers));
// Fix functions // Fix functions
$funcs = []; $funcs = [];

View File

@ -17,6 +17,7 @@
namespace Query\Drivers\Mysql; namespace Query\Drivers\Mysql;
use PDO; use PDO;
use PHPUnit\Framework\Attributes\CodeCoverageIgnore;
use Query\Drivers\AbstractDriver; use Query\Drivers\AbstractDriver;
use function defined; use function defined;
@ -37,9 +38,8 @@ class Driver extends AbstractDriver
/** /**
* Connect to MySQL Database * Connect to MySQL Database
*
* @codeCoverageIgnore
*/ */
#[CodeCoverageIgnore]
public function __construct(string $dsn, ?string $username=NULL, ?string $password=NULL, array $options=[]) public function __construct(string $dsn, ?string $username=NULL, ?string $password=NULL, array $options=[])
{ {
// Set the charset to UTF-8 // Set the charset to UTF-8

View File

@ -16,6 +16,7 @@
namespace Query\Drivers\Mysql; namespace Query\Drivers\Mysql;
use PHPUnit\Framework\Attributes\CodeCoverageIgnore;
use Query\Drivers\AbstractSQL; use Query\Drivers\AbstractSQL;
/** /**
@ -98,9 +99,8 @@ SQL;
/** /**
* Returns sql to list triggers * Returns sql to list triggers
*
* @codeCoverageIgnore
*/ */
#[CodeCoverageIgnore]
public function triggerList(): string public function triggerList(): string
{ {
return 'SHOW TRIGGERS'; return 'SHOW TRIGGERS';
@ -108,9 +108,8 @@ SQL;
/** /**
* Return sql to list functions * Return sql to list functions
*
* @codeCoverageIgnore
*/ */
#[CodeCoverageIgnore]
public function functionList(): string public function functionList(): string
{ {
return 'SHOW FUNCTION STATUS'; return 'SHOW FUNCTION STATUS';
@ -118,9 +117,8 @@ SQL;
/** /**
* Return sql to list stored procedures * Return sql to list stored procedures
*
* @codeCoverageIgnore
*/ */
#[CodeCoverageIgnore]
public function procedureList(): string public function procedureList(): string
{ {
return 'SHOW PROCEDURE STATUS'; return 'SHOW PROCEDURE STATUS';

View File

@ -108,7 +108,7 @@ class Util extends AbstractUtil
$row = array_map(static fn ($r) => is_string($r) ? $driver->quote($r) : $r, $row); $row = array_map(static fn ($r) => is_string($r) ? $driver->quote($r) : $r, $row);
$row = array_map('trim', $row); $row = array_map('trim', $row);
$rowString = 'INSERT INTO `' . trim($t) . '` (`' . implode('`,`', $columns) . '`) VALUES (' . implode(',', $row) . ');'; $rowString = 'INSERT INTO `' . trim((string) $t) . '` (`' . implode('`,`', $columns) . '`) VALUES (' . implode(',', $row) . ');';
$row = NULL; $row = NULL;

View File

@ -16,6 +16,7 @@
namespace Query\Drivers\Pgsql; namespace Query\Drivers\Pgsql;
use PHPUnit\Framework\Attributes\CodeCoverageIgnore;
use Query\Drivers\AbstractDriver; use Query\Drivers\AbstractDriver;
/** /**
@ -25,9 +26,8 @@ class Driver extends AbstractDriver
{ {
/** /**
* Connect to a PosgreSQL database * Connect to a PosgreSQL database
*
* @codeCoverageIgnore
*/ */
#[CodeCoverageIgnore]
public function __construct(string $dsn, ?string $username=NULL, ?string $password=NULL, array $options=[]) public function __construct(string $dsn, ?string $username=NULL, ?string $password=NULL, array $options=[])
{ {
if ( ! str_contains($dsn, 'pgsql')) if ( ! str_contains($dsn, 'pgsql'))

View File

@ -51,7 +51,7 @@ class Util extends AbstractUtil
// Get the data for each object // Get the data for each object
foreach ($tables as $t) foreach ($tables as $t)
{ {
$sql = 'SELECT * FROM "' . trim($t) . '"'; $sql = 'SELECT * FROM "' . trim((string) $t) . '"';
$res = $this->getDriver()->query($sql); $res = $this->getDriver()->query($sql);
$objRes = $res->fetchAll(PDO::FETCH_ASSOC); $objRes = $res->fetchAll(PDO::FETCH_ASSOC);
@ -77,7 +77,7 @@ class Util extends AbstractUtil
$row = array_map([$this->getDriver(), 'quote'], $row); $row = array_map([$this->getDriver(), 'quote'], $row);
$row = array_map('trim', $row); $row = array_map('trim', $row);
$rowString = 'INSERT INTO "' . trim($t) . '" ("' . implode('","', $columns) . '") VALUES (' . implode(',', $row) . ');'; $rowString = 'INSERT INTO "' . trim((string) $t) . '" ("' . implode('","', $columns) . '") VALUES (' . implode(',', $row) . ');';
$row = NULL; $row = NULL;

View File

@ -17,8 +17,9 @@
namespace Query\Drivers\Sqlite; namespace Query\Drivers\Sqlite;
use InvalidArgumentException; use InvalidArgumentException;
use PDO; use PDO;
use PHPUnit\Framework\Attributes\CodeCoverageIgnore;
use Query\Drivers\AbstractDriver; use Query\Drivers\AbstractDriver;
use function is_array; use function is_array;
@ -89,9 +90,9 @@ class Driver extends AbstractDriver
/** /**
* Create sql for batch insert * Create sql for batch insert
* *
* @codeCoverageIgnore
* @return array[]|null[]|string[] * @return array[]|null[]|string[]
*/ */
#[CodeCoverageIgnore]
public function insertBatch(string $table, array $data=[]): array public function insertBatch(string $table, array $data=[]): array
{ {
// If greater than version 3.7.11, supports the same syntax as // If greater than version 3.7.11, supports the same syntax as

View File

@ -70,10 +70,8 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface
/** /**
* Selects the maximum value of a field from a query * Selects the maximum value of a field from a query
*
* @param bool|string $as
*/ */
public function selectMax(string $field, $as=FALSE): self public function selectMax(string $field, bool|string $as=FALSE): self
{ {
// Create the select string // Create the select string
$this->state->appendSelectString(' MAX' . $this->_select($field, $as)); $this->state->appendSelectString(' MAX' . $this->_select($field, $as));
@ -83,10 +81,8 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface
/** /**
* Selects the minimum value of a field from a query * Selects the minimum value of a field from a query
*
* @param bool|string $as
*/ */
public function selectMin(string $field, $as=FALSE): self public function selectMin(string $field, bool|string $as=FALSE): self
{ {
// Create the select string // Create the select string
$this->state->appendSelectString(' MIN' . $this->_select($field, $as)); $this->state->appendSelectString(' MIN' . $this->_select($field, $as));
@ -96,10 +92,8 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface
/** /**
* Selects the average value of a field from a query * Selects the average value of a field from a query
*
* @param bool|string $as
*/ */
public function selectAvg(string $field, $as=FALSE): self public function selectAvg(string $field, bool|string $as=FALSE): self
{ {
// Create the select string // Create the select string
$this->state->appendSelectString(' AVG' . $this->_select($field, $as)); $this->state->appendSelectString(' AVG' . $this->_select($field, $as));
@ -109,10 +103,8 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface
/** /**
* Selects the sum of a field from a query * Selects the sum of a field from a query
*
* @param bool|string $as
*/ */
public function selectSum(string $field, $as=FALSE): self public function selectSum(string $field, bool|string $as=FALSE): self
{ {
// Create the select string // Create the select string
$this->state->appendSelectString(' SUM' . $this->_select($field, $as)); $this->state->appendSelectString(' SUM' . $this->_select($field, $as));
@ -391,7 +383,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface
// Flatten key/val pairs into an array of space-separated pairs // Flatten key/val pairs into an array of space-separated pairs
foreach ($this->state->getOrderArray() as $k => $v) foreach ($this->state->getOrderArray() as $k => $v)
{ {
$orderClauses[] = $k . ' ' . strtoupper($v); $orderClauses[] = $k . ' ' . strtoupper((string) $v);
} }
// Set the final string // Set the final string
@ -558,7 +550,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface
/** /**
* Creates and executes a batch insertion query * Creates and executes a batch insertion query
*/ */
public function insertBatch(string $table, $data=[]): ?PDOStatement public function insertBatch(string $table, mixed $data=[]): ?PDOStatement
{ {
// Get the generated values and sql string // Get the generated values and sql string
[$sql, $data] = $this->driver->insertBatch($table, $data); [$sql, $data] = $this->driver->insertBatch($table, $data);

View File

@ -17,9 +17,10 @@
namespace Query; namespace Query;
use BadMethodCallException; use BadMethodCallException;
use PDO; use PDO;
use PDOStatement; use PDOStatement;
use PHPUnit\Framework\Attributes\CodeCoverageIgnore;
use Query\Drivers\DriverInterface; use Query\Drivers\DriverInterface;
use function is_string; use function is_string;
use function regexInArray; use function regexInArray;
@ -90,19 +91,13 @@ class QueryBuilderBase
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Methods // ! Methods
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Constructor
*/
public function __construct(protected ?DriverInterface $driver, protected QueryParser $parser) public function __construct(protected ?DriverInterface $driver, protected QueryParser $parser)
{ {
// Create new State object // Create new State object
$this->state = new State(); $this->state = new State();
} }
/** #[CodeCoverageIgnore]
* Destructor
* @codeCoverageIgnore
*/
public function __destruct() public function __destruct()
{ {
$this->driver = NULL; $this->driver = NULL;
@ -370,7 +365,7 @@ class QueryBuilderBase
{ {
$v = (is_numeric($v)) $v = (is_numeric($v))
? $v ? $v
: htmlentities($this->driver->quote($v), ENT_NOQUOTES, 'utf-8'); : htmlentities((string) $this->driver->quote($v), ENT_NOQUOTES, 'utf-8');
} }
unset($v); unset($v);
@ -392,9 +387,8 @@ class QueryBuilderBase
/** /**
* Sub-method for generating sql strings * Sub-method for generating sql strings
*
* @codeCoverageIgnore
*/ */
#[CodeCoverageIgnore]
protected function _compileType(QueryType $type = QueryType::SELECT, string $table = ''): string protected function _compileType(QueryType $type = QueryType::SELECT, string $table = ''): string
{ {
$setArrayKeys = $this->state->getSetArrayKeys(); $setArrayKeys = $this->state->getSetArrayKeys();

View File

@ -70,31 +70,23 @@ interface QueryBuilderInterface
/** /**
* Selects the maximum value of a field from a query * Selects the maximum value of a field from a query
*
* @param bool|string $as
*/ */
public function selectMax(string $field, $as=FALSE): self; public function selectMax(string $field, bool|string $as=FALSE): self;
/** /**
* Selects the minimum value of a field from a query * Selects the minimum value of a field from a query
*
* @param bool|string $as
*/ */
public function selectMin(string $field, $as=FALSE): self; public function selectMin(string $field, bool|string $as=FALSE): self;
/** /**
* Selects the average value of a field from a query * Selects the average value of a field from a query
*
* @param bool|string $as
*/ */
public function selectAvg(string $field, $as=FALSE): self; public function selectAvg(string $field, bool|string $as=FALSE): self;
/** /**
* Selects the sum of a field from a query * Selects the sum of a field from a query
*
* @param bool|string $as
*/ */
public function selectSum(string $field, $as=FALSE): self; public function selectSum(string $field, bool|string $as=FALSE): self;
/** /**
* Adds the 'distinct' keyword to a query * Adds the 'distinct' keyword to a query
@ -196,8 +188,6 @@ interface QueryBuilderInterface
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets values for inserts / updates / deletes * Sets values for inserts / updates / deletes
*
* @param mixed $values
*/ */
public function set(mixed $key, mixed $values = NULL): self; public function set(mixed $key, mixed $values = NULL): self;

View File

@ -66,7 +66,7 @@ class QueryParser
preg_match_all($fullPattern, $sql, $this->matches['combined'], PREG_SET_ORDER); preg_match_all($fullPattern, $sql, $this->matches['combined'], PREG_SET_ORDER);
// Go through the matches, and get the most relevant matches // Go through the matches, and get the most relevant matches
$this->matches = array_map([$this, 'filterArray'], $this->matches); $this->matches = array_map($this->filterArray(...), $this->matches);
return $this->matches; return $this->matches;
} }

View File

@ -13,111 +13,109 @@
* @link https://git.timshomepage.net/aviat/Query * @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0 * @version 4.0.0
*/ */
namespace {
use Query\ConnectionManager; use Query\{ConnectionManager, QueryBuilderInterface};
use Query\QueryBuilderInterface;
/** /**
* Global functions that don't really fit anywhere else * Global functions that don't really fit anywhere else
*/ */
/** /**
* Multibyte-safe trim function * Multibyte-safe trim function
*/ */
function mb_trim(string $string): string function mb_trim(string $string): string
{
return preg_replace('/(^\s+)|(\s+$)/u', '', $string);
}
/**
* Filter out db rows into one array
*/
function dbFilter(array $array, mixed $index): array
{
$newArray = [];
foreach ($array as $a)
{ {
return preg_replace('/(^\s+)|(\s+$)/u', '', $string); $newArray[] = $a[$index];
} }
/** return $newArray;
* Filter out db rows into one array }
*/
function dbFilter(array $array, mixed $index): array /**
* Zip a set of arrays together on common keys
*
* The $zipperInput array is an array of arrays indexed by their place in the output
* array.
*/
function arrayZipper(array $zipperInput): array
{
$output = [];
foreach ($zipperInput as $appendKey => $values)
{ {
$newArray = []; foreach ($values as $index => $value)
foreach ($array as $a)
{ {
$newArray[] = $a[$index]; if ( ! isset($output[$index]))
}
return $newArray;
}
/**
* Zip a set of arrays together on common keys
*
* The $zipperInput array is an array of arrays indexed by their place in the output
* array.
*/
function arrayZipper(array $zipperInput): array
{
$output = [];
foreach ($zipperInput as $appendKey => $values)
{
foreach ($values as $index => $value)
{ {
if ( ! isset($output[$index])) $output[$index] = [];
{
$output[$index] = [];
}
$output[$index][$appendKey] = $value;
} }
$output[$index][$appendKey] = $value;
} }
return $output;
} }
/** return $output;
* Determine whether a value in the passed array matches the pattern }
* passed
*/ /**
function regexInArray(array $array, string $pattern): bool * Determine whether a value in the passed array matches the pattern
* passed
*/
function regexInArray(array $array, string $pattern): bool
{
if (empty($array))
{ {
if (empty($array))
{
return FALSE;
}
foreach ($array as $item)
{
if (is_scalar($item) && preg_match($pattern, (string) $item))
{
return TRUE;
}
}
return FALSE; return FALSE;
} }
/** foreach ($array as $item)
* Connection function
*
* Send an array or object as connection parameters to create a connection. If
* the array or object has an 'alias' parameter, passing that string to this
* function will return that connection. Passing no parameters returns the last
* connection created.
*/
function Query(string|object|array|null $params = ''): ?QueryBuilderInterface
{ {
if ($params === NULL) if (is_scalar($item) && preg_match($pattern, (string) $item))
{ {
return NULL; return TRUE;
} }
$manager = ConnectionManager::getInstance();
// If you are getting a previously created connection
if (is_string($params))
{
return $manager->getConnection($params);
}
$paramsObject = (object)$params;
// Otherwise, return a new connection
return $manager->connect($paramsObject);
} }
return FALSE;
} }
/**
* Connection function
*
* Send an array or object as connection parameters to create a connection. If
* the array or object has an 'alias' parameter, passing that string to this
* function will return that connection. Passing no parameters returns the last
* connection created.
*/
function Query(string|object|array|null $params = ''): ?QueryBuilderInterface
{
if ($params === NULL)
{
return NULL;
}
$manager = ConnectionManager::getInstance();
// If you are getting a previously created connection
if (is_string($params))
{
return $manager->getConnection($params);
}
$paramsObject = (object) $params;
// Otherwise, return a new connection
return $manager->connect($paramsObject);
}
// End of common.php // End of common.php