From b0ecaa87e4a35e1515390937630d30bb51b93165 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 13 Jan 2023 13:14:28 -0500 Subject: [PATCH] Refactor fake Enums to 'real' ones --- .travis.yml | 5 +- src/ConnectionManager.php | 12 ++--- src/Drivers/AbstractDriver.php | 38 ++------------ src/Drivers/AbstractSQL.php | 2 - src/Drivers/AbstractUtil.php | 12 ++--- src/Drivers/DriverInterface.php | 37 +------------- src/Drivers/Mysql/SQL.php | 8 +-- src/Drivers/Mysql/Util.php | 7 --- src/Drivers/Pgsql/Driver.php | 4 -- src/Drivers/Pgsql/SQL.php | 2 - src/Drivers/SQLInterface.php | 8 --- src/Drivers/Sqlite/Driver.php | 5 +- src/JoinType.php | 14 +++--- src/LikeType.php | 8 +-- src/MapType.php | 14 +++--- src/QueryBuilder.php | 89 ++++++++------------------------- src/QueryBuilderBase.php | 35 ++++--------- src/QueryBuilderInterface.php | 43 ++++++++-------- src/QueryParser.php | 6 +-- src/QueryType.php | 14 +++--- src/State.php | 12 ++--- src/common.php | 4 +- 22 files changed, 104 insertions(+), 275 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6c0c9ed..ae2522c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,10 @@ services: php: - nightly - - '7.4' +# - '8.0' + - '8.1' + - '8.2' +# - '7.4' # - '8.0' diff --git a/src/ConnectionManager.php b/src/ConnectionManager.php index ed87979..9743fb3 100644 --- a/src/ConnectionManager.php +++ b/src/ConnectionManager.php @@ -91,10 +91,10 @@ final class ConnectionManager { /** * Returns the connection specified by the name given * - * @param string|array|object $name + * @param string $name * @throws Exception\NonExistentConnectionException */ - public function getConnection($name = ''): QueryBuilderInterface + public function getConnection(string $name = ''): QueryBuilderInterface { // If the parameter is a string, use it as an array index if (is_scalar($name) && isset($this->connections[$name])) @@ -115,9 +115,9 @@ final class ConnectionManager { * Parse the passed parameters and return a connection * * @param array|object $params - * @throws Exception\BadDBDriverException + * @return QueryBuilderInterface */ - public function connect($params): QueryBuilderInterface + public function connect(array|object $params): QueryBuilderInterface { [$dsn, $dbType, $params, $options] = $this->parseParams($params); @@ -157,9 +157,9 @@ final class ConnectionManager { * * @param array|object $rawParams * @throws Exception\BadDBDriverException - * @return mixed[] + * @return array */ - public function parseParams($rawParams): array + public function parseParams(array|object $rawParams): array { $params = (object) $rawParams; $params->type = strtolower($params->type); diff --git a/src/Drivers/AbstractDriver.php b/src/Drivers/AbstractDriver.php index 2534e33..c1f2d38 100644 --- a/src/Drivers/AbstractDriver.php +++ b/src/Drivers/AbstractDriver.php @@ -170,9 +170,6 @@ abstract class AbstractDriver // -------------------------------------------------------------------------- /** * Simplifies prepared statements for database queries - * - * @return PDOStatement | FALSE - * @throws InvalidArgumentException */ public function prepareQuery(string $sql, array $data): PDOStatement { @@ -259,7 +256,7 @@ abstract class AbstractDriver * * @param mixed $identifier */ - public function quoteIdent($identifier): string|array + public function quoteIdent(string|array $identifier): string|array { if (is_array($identifier)) { @@ -299,8 +296,6 @@ abstract class AbstractDriver /** * Return schemas for databases that list them - * - * @return array */ public function getSchemas(): ?array { @@ -310,8 +305,6 @@ abstract class AbstractDriver /** * Return list of tables for the current database - * - * @return array */ public function getTables(): ?array { @@ -322,8 +315,6 @@ abstract class AbstractDriver /** * Return list of dbs for the current connection, if possible - * - * @return array */ public function getDbs(): ?array { @@ -332,8 +323,6 @@ abstract class AbstractDriver /** * Return list of views for the current database - * - * @return array */ public function getViews(): ?array { @@ -344,8 +333,6 @@ abstract class AbstractDriver /** * Return list of sequences for the current database, if they exist - * - * @return array */ public function getSequences(): ?array { @@ -354,8 +341,6 @@ abstract class AbstractDriver /** * Return list of functions for the current database - * - * @return array */ public function getFunctions(): ?array { @@ -364,8 +349,6 @@ abstract class AbstractDriver /** * Return list of stored procedures for the current database - * - * @return array */ public function getProcedures(): ?array { @@ -374,8 +357,6 @@ abstract class AbstractDriver /** * Return list of triggers for the current database - * - * @return array */ public function getTriggers(): ?array { @@ -385,8 +366,6 @@ abstract class AbstractDriver /** * Retrieves an array of non-user-created tables for * the connection/database - * - * @return array */ public function getSystemTables(): ?array { @@ -395,8 +374,6 @@ abstract class AbstractDriver /** * Retrieve column information for the current database table - * - * @return array */ public function getColumns(string $table): ?array { @@ -405,8 +382,6 @@ abstract class AbstractDriver /** * Retrieve foreign keys for the table - * - * @return array */ public function getFks(string $table): ?array { @@ -415,8 +390,6 @@ abstract class AbstractDriver /** * Retrieve indexes for the table - * - * @return array */ public function getIndexes(string $table): ?array { @@ -425,8 +398,6 @@ abstract class AbstractDriver /** * Retrieve list of data types for the database - * - * @return array */ public function getTypes(): ?array { @@ -443,11 +414,8 @@ abstract class AbstractDriver /** * Method to simplify retrieving db results for meta-data queries - * - * @param string|array|null $query - * @param bool $filteredIndex */ - public function driverQuery($query, $filteredIndex=TRUE): ?array + public function driverQuery(string|array $query, bool $filteredIndex=TRUE): ?array { // Call the appropriate method, if it exists if (is_string($query) && method_exists($this->driverSQL, $query)) @@ -628,7 +596,7 @@ abstract class AbstractDriver * @param mixed $str * @return mixed */ - public function _quote($str) + public function _quote(mixed $str): mixed { // Check that the current value is a string, // and is not already quoted before quoting diff --git a/src/Drivers/AbstractSQL.php b/src/Drivers/AbstractSQL.php index 36fac35..6d4ea8f 100644 --- a/src/Drivers/AbstractSQL.php +++ b/src/Drivers/AbstractSQL.php @@ -22,8 +22,6 @@ abstract class AbstractSQL implements SQLInterface { /** * Limit clause - * - * @param int $offset */ public function limit(string $sql, int $limit, ?int $offset=NULL): string { diff --git a/src/Drivers/AbstractUtil.php b/src/Drivers/AbstractUtil.php index 1613380..1e78618 100644 --- a/src/Drivers/AbstractUtil.php +++ b/src/Drivers/AbstractUtil.php @@ -23,7 +23,7 @@ abstract class AbstractUtil { /** * Save a reference to the connection object for later use */ - public function __construct(private DriverInterface $connection) + public function __construct(private readonly DriverInterface $connection) { } @@ -37,12 +37,8 @@ abstract class AbstractUtil { /** * Convenience public function to generate sql for creating a db table - * - * @param string $name - * @param array $fields - * @param bool $ifNotExists */ - public function createTable($name, $fields, array $constraints=[], $ifNotExists=TRUE): string + public function createTable(string $name, array $fields, array $constraints=[], bool $ifNotExists=TRUE): string { $existsStr = $ifNotExists ? ' IF NOT EXISTS ' : ' '; @@ -78,10 +74,8 @@ abstract class AbstractUtil { /** * Drop the selected table - * - * @param string $name */ - public function deleteTable($name): string + public function deleteTable(string $name): string { return 'DROP TABLE IF EXISTS '.$this->getDriver()->quoteTable($name); } diff --git a/src/Drivers/DriverInterface.php b/src/Drivers/DriverInterface.php index f7c472d..b5876d8 100644 --- a/src/Drivers/DriverInterface.php +++ b/src/Drivers/DriverInterface.php @@ -45,102 +45,73 @@ interface DriverInterface /* extends the interface of PDO */ { /** * Simplifies prepared statements for database queries - * - * @return PDOStatement|null - * @throws InvalidArgumentException */ public function prepareQuery(string $sql, array $data): PDOStatement; /** * Retrieve column information for the current database table - * - * @return array */ public function getColumns(string $table): ?array; /** * Retrieve list of data types for the database - * - * @return array */ public function getTypes(): ?array; /** * Retrieve indexes for the table - * - * @return array */ public function getIndexes(string $table): ?array; /** * Retrieve foreign keys for the table - * - * @return array */ public function getFks(string $table): ?array; /** * Return list of tables for the current database - * - * @return array */ public function getTables(): ?array; /** * Retrieves an array of non-user-created tables for * the connection/database - * - * @return array */ public function getSystemTables(): ?array; /** * Return schemas for databases that list them. Returns * database list if schemas are databases for the current driver. - * - * @return array */ public function getSchemas(): ?array; /** * Return list of dbs for the current connection, if possible - * - * @return array */ public function getDbs(): ?array; /** * Return list of views for the current database - * - * @return array */ public function getViews(): ?array; /** * Return list of sequences for the current database, if they exist - * - * @return array */ public function getSequences(): ?array; /** * Return list of functions for the current database - * - * @return array */ public function getFunctions(): ?array; /** * Return list of stored procedures for the current database - * - * @return array */ public function getProcedures(): ?array; /** * Return list of triggers for the current database - * - * @return array */ public function getTriggers(): ?array; @@ -161,12 +132,8 @@ interface DriverInterface /* extends the interface of PDO */ { /** * Method to simplify retrieving db results for meta-data queries - * - * @param string|array|null $query - * @param bool $filteredIndex - * @return array */ - public function driverQuery($query, $filteredIndex=TRUE): ?array; + public function driverQuery(string|array $query, bool $filteredIndex=TRUE): ?array; /** * Returns number of rows affected by an INSERT, UPDATE, DELETE type query @@ -176,8 +143,6 @@ interface DriverInterface /* extends the interface of PDO */ { /** * Return the number of rows returned for a SELECT query * @see http://us3.php.net/manual/en/pdostatement.rowcount.php#87110 - * - * @return int */ public function numRows(): ?int; diff --git a/src/Drivers/Mysql/SQL.php b/src/Drivers/Mysql/SQL.php index 5651ef7..48c8354 100644 --- a/src/Drivers/Mysql/SQL.php +++ b/src/Drivers/Mysql/SQL.php @@ -24,8 +24,6 @@ class SQL extends AbstractSQL { /** * Limit clause - * - * @param int|boolean $offset */ public function limit(string $sql, int $limit, ?int $offset=NULL): string { @@ -66,10 +64,8 @@ SQL; /** * Returns sql to list tables - * - * @param string $database */ - public function tableList($database=''): string + public function tableList(string $database=''): string { // @codeCoverageIgnoreStart if ( ! empty($database)) @@ -126,8 +122,6 @@ SQL; /** * Return sql to list sequences - * - * @return string */ public function sequenceList(): ?string { diff --git a/src/Drivers/Mysql/Util.php b/src/Drivers/Mysql/Util.php index 35918e4..f3966de 100644 --- a/src/Drivers/Mysql/Util.php +++ b/src/Drivers/Mysql/Util.php @@ -103,13 +103,6 @@ class Util extends AbstractUtil { foreach($rows as $row) { $row = array_values($row); - - // Workaround for Quercus -// foreach($row as &$r) -// { -// $r = $driver->quote($r); -// } -// unset($r); $row = array_map('trim', $row); $rowString = 'INSERT INTO `'.trim($t).'` (`'.implode('`,`', $columns).'`) VALUES ('.implode(',', $row).');'; diff --git a/src/Drivers/Pgsql/Driver.php b/src/Drivers/Pgsql/Driver.php index 824c231..84f142c 100644 --- a/src/Drivers/Pgsql/Driver.php +++ b/src/Drivers/Pgsql/Driver.php @@ -39,8 +39,6 @@ class Driver extends AbstractDriver { /** * Get a list of schemas for the current connection - * - * @return array */ public function getSchemas(): ?array { @@ -55,8 +53,6 @@ SQL; /** * Retrieve foreign keys for the table - * - * @return mixed[]|null */ public function getFks(string $table): array { diff --git a/src/Drivers/Pgsql/SQL.php b/src/Drivers/Pgsql/SQL.php index a4aaedf..b8896e6 100644 --- a/src/Drivers/Pgsql/SQL.php +++ b/src/Drivers/Pgsql/SQL.php @@ -107,8 +107,6 @@ SQL; /** * Return sql to list functions - * - * @return string */ public function functionList(): ?string { diff --git a/src/Drivers/SQLInterface.php b/src/Drivers/SQLInterface.php index 11bb697..fa1a416 100644 --- a/src/Drivers/SQLInterface.php +++ b/src/Drivers/SQLInterface.php @@ -57,29 +57,21 @@ interface SQLInterface { /** * Returns sql to list triggers - * - * @return string */ public function triggerList(): ?string; /** * Return sql to list functions - * - * @return string */ public function functionList(): ?string; /** * Return sql to list stored procedures - * - * @return string */ public function procedureList(): ?string; /** * Return sql to list sequences - * - * @return string */ public function sequenceList(): ?string; diff --git a/src/Drivers/Sqlite/Driver.php b/src/Drivers/Sqlite/Driver.php index 5e4ac77..b8fc0b1 100644 --- a/src/Drivers/Sqlite/Driver.php +++ b/src/Drivers/Sqlite/Driver.php @@ -25,7 +25,6 @@ use Query\Drivers\AbstractDriver; * SQLite specific class */ class Driver extends AbstractDriver { - /** * SQLite has a truncate optimization, * but no support for the actual keyword @@ -55,8 +54,6 @@ class Driver extends AbstractDriver { /** * List tables for the current database - * - * @return mixed[] */ public function getTables(): array { @@ -92,7 +89,7 @@ class Driver extends AbstractDriver { * Create sql for batch insert * * @codeCoverageIgnore - * @return mixed[][]|string[]|null[]|string[]|null[] + * @return array[]|string[]|null[] */ public function insertBatch(string $table, array $data=[]): array { diff --git a/src/JoinType.php b/src/JoinType.php index 6b5a6bf..5118e2a 100644 --- a/src/JoinType.php +++ b/src/JoinType.php @@ -16,12 +16,12 @@ namespace Query; /** - * 'Enum' of join types + * Enum of join types */ -class JoinType { - public final const CROSS = 'cross'; - public final const INNER = 'inner'; - public final const OUTER = 'outer'; - public final const LEFT = 'left'; - public final const RIGHT = 'right'; +enum JoinType: string { + case CROSS = 'cross'; + case INNER = 'inner'; + case OUTER = 'outer'; + case LEFT = 'left'; + case RIGHT = 'right'; } \ No newline at end of file diff --git a/src/LikeType.php b/src/LikeType.php index 77f230f..748716c 100644 --- a/src/LikeType.php +++ b/src/LikeType.php @@ -18,8 +18,8 @@ namespace Query; /** * 'Enum' of join types */ -class LikeType { - public final const BEFORE = 'before'; - public final const AFTER = 'after'; - public final const BOTH = 'both'; +enum LikeType: string { + case BEFORE = 'before'; + case AFTER = 'after'; + case BOTH = 'both'; } \ No newline at end of file diff --git a/src/MapType.php b/src/MapType.php index 8dc1ad9..45a82c1 100644 --- a/src/MapType.php +++ b/src/MapType.php @@ -18,11 +18,11 @@ namespace Query; /** * 'Enum' of query map types */ -class MapType { - public final const GROUP_END = 'group_end'; - public final const GROUP_START = 'group_start'; - public final const JOIN = 'join'; - public final const LIKE = 'like'; - public final const WHERE = 'where'; - public final const WHERE_IN = 'where_in'; +enum MapType: string { + case GROUP_END = 'group_end'; + case GROUP_START = 'group_start'; + case JOIN = 'join'; + case LIKE = 'like'; + case WHERE = 'where'; + case WHERE_IN = 'where_in'; } \ No newline at end of file diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index b693df5..2634f93 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -187,40 +187,32 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { // -------------------------------------------------------------------------- /** * Creates a Like clause in the sql statement - * - * @param mixed $val */ - public function like(string $field, $val, string $pos=LikeType::BOTH): self + public function like(string $field, mixed $val, LikeType $pos=LikeType::BOTH): self { return $this->_like($field, $val, $pos); } /** * Generates an OR Like clause - * - * @param mixed $val */ - public function orLike(string $field, $val, string $pos=LikeType::BOTH): self + public function orLike(string $field, mixed $val, LikeType $pos=LikeType::BOTH): self { return $this->_like($field, $val, $pos, 'LIKE', 'OR'); } /** * Generates a NOT LIKE clause - * - * @param mixed $val */ - public function notLike(string $field, $val, string $pos=LikeType::BOTH): self + public function notLike(string $field, mixed $val, LikeType $pos=LikeType::BOTH): self { return $this->_like($field, $val, $pos, 'NOT LIKE'); } /** * Generates a OR NOT LIKE clause - * - * @param mixed $val */ - public function orNotLike(string $field, $val, string $pos=LikeType::BOTH): self + public function orNotLike(string $field, mixed $val, LikeType $pos=LikeType::BOTH): self { return $this->_like($field, $val, $pos, 'NOT LIKE', 'OR'); } @@ -230,22 +222,16 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { // -------------------------------------------------------------------------- /** * Generates a 'Having' clause - * - * @param mixed $key - * @param mixed $val */ - public function having($key, $val=[]): self + public function having(mixed $key, mixed $val=[]): self { return $this->_having($key, $val); } /** * Generates a 'Having' clause prefixed with 'OR' - * - * @param mixed $key - * @param mixed $val */ - public function orHaving($key, $val=[]): self + public function orHaving(mixed $key, mixed $val=[]): self { return $this->_having($key, $val, 'OR'); } @@ -257,67 +243,48 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { * Specify condition(s) in the where clause of a query * Note: this function works with key / value, or a * passed array with key / value pairs - * - * @param mixed $key - * @param mixed $val - * @param mixed $escape */ - public function where($key, $val=[], $escape=NULL): self + public function where(mixed $key, mixed $val=[]): self { return $this->_whereString($key, $val); } /** * Where clause prefixed with "OR" - * - * @param string $key - * @param mixed $val */ - public function orWhere($key, $val=[]): self + public function orWhere(mixed $key, mixed $val=[]): self { return $this->_whereString($key, $val, 'OR'); } /** * Where clause with 'IN' statement - * - * @param mixed $field - * @param mixed $val */ - public function whereIn($field, $val=[]): self + public function whereIn(string $field, mixed $val=[]): self { return $this->_whereIn($field, $val); } /** * Where in statement prefixed with "or" - * - * @param string $field - * @param mixed $val */ - public function orWhereIn($field, $val=[]): self + public function orWhereIn(string $field, mixed $val=[]): self { return $this->_whereIn($field, $val, 'IN', 'OR'); } /** * WHERE NOT IN (FOO) clause - * - * @param string $field - * @param mixed $val */ - public function whereNotIn($field, $val=[]): self + public function whereNotIn(string $field, mixed $val=[]): self { return $this->_whereIn($field, $val, 'NOT IN'); } /** * OR WHERE NOT IN (FOO) clause - * - * @param string $field - * @param mixed $val */ - public function orWhereNotIn($field, $val=[]): self + public function orWhereNotIn(string $field, mixed $val=[]): self { return $this->_whereIn($field, $val, 'NOT IN', 'OR'); } @@ -327,11 +294,8 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { // -------------------------------------------------------------------------- /** * Sets values for inserts / updates / deletes - * - * @param mixed $key - * @param mixed $val */ - public function set($key, $val = NULL): self + public function set(mixed $key, mixed $val = NULL): self { $pairs = is_scalar($key) ? [$key => $val] : $key; @@ -359,7 +323,7 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { /** * Creates a join phrase in a compiled query */ - public function join(string $table, string $condition, string $type=''): self + public function join(string $table, string $condition, JoinType $type=JoinType::INNER): self { // Prefix and quote table name $tableArr = explode(' ', mb_trim($table)); @@ -371,17 +335,15 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { $parsedCondition = $this->parser->compileJoin($condition); $condition = $table . ' ON ' . $parsedCondition; - $this->state->appendMap("\n" . strtoupper($type) . ' JOIN ', $condition, MapType::JOIN); + $this->state->appendMap("\n" . strtoupper($type->value) . ' JOIN ', $condition, MapType::JOIN); return $this; } /** * Group the results by the selected field(s) - * - * @param mixed $field */ - public function groupBy($field): self + public function groupBy(mixed $field): self { if ( ! is_scalar($field)) { @@ -575,10 +537,8 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { /** * Creates an insert clause, and executes it - * - * @param mixed $data */ - public function insert(string $table, $data=[]): PDOStatement + public function insert(string $table, mixed $data=[]): PDOStatement { if ( ! empty($data)) { @@ -590,9 +550,6 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { /** * Creates and executes a batch insertion query - * - * @param array $data - * @return PDOStatement */ public function insertBatch(string $table, $data=[]): ?PDOStatement { @@ -600,16 +557,14 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { [$sql, $data] = $this->driver->insertBatch($table, $data); return $sql !== NULL - ? $this->_run('', $table, $sql, $data) + ? $this->_run(QueryType::INSERT_BATCH, $table, $sql, $data) : NULL; } /** * Creates an update clause, and executes it - * - * @param mixed $data */ - public function update(string $table, $data=[]): PDOStatement + public function update(string $table, mixed $data=[]): PDOStatement { if ( ! empty($data)) { @@ -633,16 +588,14 @@ class QueryBuilder extends QueryBuilderBase implements QueryBuilderInterface { // Get the generated values and sql string [$sql, $data, $affectedRows] = $this->driver->updateBatch($table, $data, $where); - $this->_run('', $table, $sql, $data); + $this->_run(QueryType::UPDATE_BATCH, $table, $sql, $data); return $affectedRows; } /** * Deletes data from a table - * - * @param mixed $where */ - public function delete(string $table, $where=''): PDOStatement + public function delete(string $table, mixed $where=''): PDOStatement { // Set the where clause if ( ! empty($where)) diff --git a/src/QueryBuilderBase.php b/src/QueryBuilderBase.php index 2b1e70a..cdfcca0 100644 --- a/src/QueryBuilderBase.php +++ b/src/QueryBuilderBase.php @@ -138,10 +138,8 @@ class QueryBuilderBase { /** * Method to simplify select_ methods - * - * @param string|bool $as */ - protected function _select(string $field, $as = FALSE): string + protected function _select(string $field, bool|string $as = FALSE): string { // Escape the identifiers $field = $this->driver->quoteIdent($field); @@ -160,7 +158,7 @@ class QueryBuilderBase { /** * Helper function for returning sql strings */ - protected function _getCompile(string $type, string $table, bool $reset): string + protected function _getCompile(QueryType $type, string $table, bool $reset): string { $sql = $this->_compile($type, $table); @@ -175,10 +173,8 @@ class QueryBuilderBase { /** * Simplify 'like' methods - * - * @param mixed $val */ - protected function _like(string $field, $val, string $pos, string $like = 'LIKE', string $conj = 'AND'): self + protected function _like(string $field, mixed $val, LikeType $pos, string $like = 'LIKE', string $conj = 'AND'): self { $field = $this->driver->quoteIdent($field); @@ -209,11 +205,8 @@ class QueryBuilderBase { /** * Simplify building having clauses - * - * @param mixed $key - * @param mixed $values */ - protected function _having($key, $values = [], string $conj = 'AND'): self + protected function _having(mixed $key, mixed $values = [], string $conj = 'AND'): self { $where = $this->_where($key, $values); @@ -243,11 +236,8 @@ class QueryBuilderBase { /** * Do all the redundant stuff for where/having type methods - * - * @param mixed $key - * @param mixed $val */ - protected function _where($key, $val = []): array + protected function _where(mixed $key, mixed $val = []): array { $where = []; $pairs = []; @@ -271,11 +261,8 @@ class QueryBuilderBase { /** * Simplify generating where string - * - * @param mixed $key - * @param mixed $values */ - protected function _whereString($key, $values = [], string $defaultConj = 'AND'): self + protected function _whereString(mixed $key, mixed $values = [], string $defaultConj = 'AND'): self { // Create key/value placeholders foreach ($this->_where($key, $values) as $f => $val) @@ -319,7 +306,7 @@ class QueryBuilderBase { * @param string $in - The (not) in fragment * @param string $conj - The where in conjunction */ - protected function _whereIn($key, $val = [], string $in = 'IN', string $conj = 'AND'): self + protected function _whereIn(mixed $key, mixed $val = [], string $in = 'IN', string $conj = 'AND'): self { $key = $this->driver->quoteIdent($key); $params = array_fill(0, is_countable($val) ? count($val) : 0, '?'); @@ -338,7 +325,7 @@ class QueryBuilderBase { * * @param array|null $vals */ - protected function _run(string $type, string $table, string $sql = NULL, array $vals = NULL, bool $reset = TRUE): PDOStatement + protected function _run(QueryType $type, string $table, string $sql = NULL, array $vals = NULL, bool $reset = TRUE): PDOStatement { if ($sql === NULL) { @@ -409,7 +396,7 @@ class QueryBuilderBase { * * @codeCoverageIgnore */ - protected function _compileType(string $type = QueryType::SELECT, string $table = ''): string + protected function _compileType(QueryType $type = QueryType::SELECT, string $table = ''): string { $setArrayKeys = $this->state->getSetArrayKeys(); switch ($type) @@ -453,7 +440,7 @@ class QueryBuilderBase { /** * String together the sql statements for sending to the db */ - protected function _compile(string $type = '', string $table = ''): string + protected function _compile(QueryType $type, string $table = ''): string { // Get the base clause for the query $sql = $this->_compileType($type, $this->driver->quoteTable($table)); @@ -505,7 +492,7 @@ class QueryBuilderBase { /** * Generate returning clause of query */ - protected function _compileReturning(string $sql, string $type): string + protected function _compileReturning(string $sql, QueryType $type): string { if ($this->returning === FALSE) { diff --git a/src/QueryBuilderInterface.php b/src/QueryBuilderInterface.php index 0aabbca..9823829 100644 --- a/src/QueryBuilderInterface.php +++ b/src/QueryBuilderInterface.php @@ -127,28 +127,28 @@ interface QueryBuilderInterface { * * @param mixed $values */ - public function like(string $field, $values, string $pos=LikeType::BOTH): self; + public function like(string $field, mixed $values, LikeType $pos=LikeType::BOTH): self; /** * Generates an OR Like clause * * @param mixed $values */ - public function orLike(string $field, $values, string $pos=LikeType::BOTH): self; + public function orLike(string $field, mixed $values, LikeType $pos=LikeType::BOTH): self; /** * Generates a NOT LIKE clause * * @param mixed $values */ - public function notLike(string $field, $values, string $pos=LikeType::BOTH): self; + public function notLike(string $field, mixed $values, LikeType $pos=LikeType::BOTH): self; /** * Generates a OR NOT LIKE clause * * @param mixed $values */ - public function orNotLike(string $field, $values, string $pos=LikeType::BOTH): self; + public function orNotLike(string $field, mixed $values, LikeType $pos=LikeType::BOTH): self; // -------------------------------------------------------------------------- // ! Having methods @@ -159,7 +159,7 @@ interface QueryBuilderInterface { * @param mixed $key * @param mixed $values */ - public function having($key, $values=[]): self; + public function having(mixed $key, mixed $values=[]): self; /** * Generates a 'Having' clause prefixed with 'OR' @@ -167,7 +167,7 @@ interface QueryBuilderInterface { * @param mixed $key * @param mixed $values */ - public function orHaving($key, $values=[]): self; + public function orHaving(mixed $key, mixed $values=[]): self; // -------------------------------------------------------------------------- // ! 'Where' methods @@ -179,9 +179,8 @@ interface QueryBuilderInterface { * * @param mixed $key * @param mixed $values - * @param bool $escape */ - public function where($key, $values=[], $escape = NULL): self; + public function where(mixed $key, mixed $values=[]): self; /** * Where clause prefixed with "OR" @@ -189,15 +188,15 @@ interface QueryBuilderInterface { * @param string $key * @param mixed $values */ - public function orWhere($key, $values=[]): self; + public function orWhere(mixed $key, mixed $values=[]): self; /** * Where clause with 'IN' statement * - * @param mixed $field + * @param string $field * @param mixed $values */ - public function whereIn($field, $values=[]): self; + public function whereIn(string $field, mixed $values=[]): self; /** * Where in statement prefixed with "or" @@ -205,7 +204,7 @@ interface QueryBuilderInterface { * @param string $field * @param mixed $values */ - public function orWhereIn($field, $values=[]): self; + public function orWhereIn(string $field, mixed $values=[]): self; /** * WHERE NOT IN (FOO) clause @@ -213,7 +212,7 @@ interface QueryBuilderInterface { * @param string $field * @param mixed $values */ - public function whereNotIn($field, $values=[]): self; + public function whereNotIn(string $field, mixed $values=[]): self; /** * OR WHERE NOT IN (FOO) clause @@ -221,7 +220,7 @@ interface QueryBuilderInterface { * @param string $field * @param mixed $values */ - public function orWhereNotIn($field, $values=[]): self; + public function orWhereNotIn(string $field, mixed $values=[]): self; // -------------------------------------------------------------------------- // ! Other Query Modifier methods @@ -232,19 +231,19 @@ interface QueryBuilderInterface { * @param mixed $key * @param mixed $values */ - public function set($key, $values = NULL): self; + public function set(mixed $key, mixed $values = NULL): self; /** * Creates a join phrase in a compiled query */ - public function join(string $table, string $condition, string $type=JoinType::INNER): self; + public function join(string $table, string $condition, JoinType $type=JoinType::INNER): self; /** * Group the results by the selected field(s) * * @param mixed $field */ - public function groupBy($field): self; + public function groupBy(mixed $field): self; /** * Order the results by the selected field(s) @@ -301,7 +300,7 @@ interface QueryBuilderInterface { * * @param array $where */ - public function getWhere(string $table, $where=[], ?int $limit=NULL, ?int $offset=NULL): PDOStatement; + public function getWhere(string $table, array $where=[], ?int $limit=NULL, ?int $offset=NULL): PDOStatement; /** * Retrieve the number of rows in the selected table @@ -321,21 +320,21 @@ interface QueryBuilderInterface { * * @param mixed $data */ - public function insert(string $table, $data=[]): PDOStatement; + public function insert(string $table, mixed $data=[]): PDOStatement; /** * Creates and executes a batch insertion query * * @param array $data */ - public function insertBatch(string $table, $data=[]): ?PDOStatement; + public function insertBatch(string $table, mixed $data=[]): ?PDOStatement; /** * Creates an update clause, and executes it * * @param mixed $data */ - public function update(string $table, $data=[]): PDOStatement; + public function update(string $table, mixed $data=[]): PDOStatement; /** * Creates a batch update, and executes it. @@ -352,7 +351,7 @@ interface QueryBuilderInterface { * * @param mixed $where */ - public function delete(string $table, $where=''): PDOStatement; + public function delete(string $table, mixed $where=''): PDOStatement; // -------------------------------------------------------------------------- // ! SQL Returning Methods diff --git a/src/QueryParser.php b/src/QueryParser.php index dd5235a..d35567c 100644 --- a/src/QueryParser.php +++ b/src/QueryParser.php @@ -44,14 +44,14 @@ class QueryParser { /** * Constructor/entry point into parser */ - public function __construct(private DriverInterface $db) + public function __construct(private readonly DriverInterface $db) { } /** * Parser method for setting the parse string * - * @return mixed[][] + * @return array[] */ public function parseJoin(string $sql): array { @@ -93,7 +93,7 @@ class QueryParser { /** * Returns a more useful match array * - * @return mixed[] + * @return array */ protected function filterArray(array $array): array { diff --git a/src/QueryType.php b/src/QueryType.php index b62cd38..29caa96 100644 --- a/src/QueryType.php +++ b/src/QueryType.php @@ -18,11 +18,11 @@ namespace Query; /** * 'Enum' of query types */ -class QueryType { - public final const SELECT = 'select'; - public final const INSERT = 'insert'; - public final const INSERT_BATCH = 'insert_batch'; - public final const UPDATE = 'update'; - public final const UPDATE_BATCH = 'update_batch'; - public final const DELETE = 'delete'; +enum QueryType: string { + case SELECT = 'select'; + case INSERT = 'insert'; + case INSERT_BATCH = 'insert_batch'; + case UPDATE = 'update'; + case UPDATE_BATCH = 'update_batch'; + case DELETE = 'delete'; } \ No newline at end of file diff --git a/src/State.php b/src/State.php index db87b4e..464ad7a 100644 --- a/src/State.php +++ b/src/State.php @@ -169,10 +169,7 @@ class State { return $this; } - /** - * @param mixed $orderArray - */ - public function setOrderArray(string $key, $orderArray): self + public function setOrderArray(string $key, mixed $orderArray): self { $this->orderArray[$key] = $orderArray; return $this; @@ -190,10 +187,7 @@ class State { return $this; } - /** - * @param mixed $val - */ - public function appendWhereValues($val): self + public function appendWhereValues(mixed $val): self { if (is_array($val)) { @@ -212,7 +206,7 @@ class State { /** * Add an additional set of mapping pairs to a internal map */ - public function appendMap(string $conjunction = '', string $string = '', string $type = ''): self + public function appendMap(string $conjunction = '', string $string = '', MapType $type = MapType::WHERE): self { $this->queryMap[] = [ 'type' => $type, diff --git a/src/common.php b/src/common.php index ca4d7bc..ff39782 100644 --- a/src/common.php +++ b/src/common.php @@ -31,10 +31,8 @@ namespace { /** * Filter out db rows into one array - * - * @param mixed $index */ - function dbFilter(array $array, $index): array + function dbFilter(array $array, mixed $index): array { $newArray = [];