From cfaff59a054fa13897c892ff1db1a49e3a568b43 Mon Sep 17 00:00:00 2001 From: David Rans Date: Tue, 20 Aug 2024 11:58:03 -0700 Subject: [PATCH] Add property/param types --- QueryGenerator.php | 45 +++++++++++++++++++++++++++++++++------------ psalm-baseline.xml | 23 ++++++----------------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/QueryGenerator.php b/QueryGenerator.php index 4776866..59ad380 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -40,7 +40,13 @@ class QueryGenerator { * statements. These correspond to methods that can be called on this class. * The values are the syntax rules for collapsing the corresponding clauses. * - * @var array + * @var non-empty-array */ private static array $methods = [ 'select' => [ @@ -186,7 +192,7 @@ class QueryGenerator { * MySQL query. Each primary clause has a set of valid sub-clauses that can * be present in a completed query of that type. * - * @var array> + * @var non-empty-array> */ private static array $possibleClauses = [ 'select' => ['from', 'join', 'where', 'group', 'having', 'order', 'limit', 'offset', 'forupdate'], @@ -250,7 +256,7 @@ public function __construct( * Append the given clause components and parameters to their existing * counterparts for the specified clause. */ - public function __call($method, $args) { + public function __call(string $method, array $args) { $method = strtolower($method); if (!isset(self::$methods[$method])) { @@ -300,13 +306,15 @@ public function __call($method, $args) { * (one of MissingPrimaryClauseException or MissingRequiredClauseException) * unless `skipValidation` has been called. * - * @param $skipClauses : Exclude the 'clause' part (WHERE, SELECT, FROM, + * @param bool $skipClauses : Exclude the 'clause' part (WHERE, SELECT, FROM, * ...) of each sub-expression. See constructClause * for more info. This is mostly for internal usage. * * Returns an array containing the query and paramter list, respectively. + * + * @return array{0: string, 1: array} */ - public function build($skipClauses = false) { + public function build(bool $skipClauses = false): array { if ($this->validateQuery) { $this->assertCompleteQuery(); } @@ -351,8 +359,11 @@ public function useOr(): self { /** * Assert the completeness of this QueryGenerator instance by verifying * that all required clauses have been set. + * + * @throws MissingPrimaryClauseException + * @throws MissingRequiredClauseException */ - private function assertCompleteQuery() { + private function assertCompleteQuery(): void { $primaryMethod = $this->getPrimaryMethod(); if (!$primaryMethod) { @@ -382,13 +393,16 @@ private function assertCompleteQuery() { }, $minimumClauses); $requiredClauseStr = '{' . implode('}, {', $requiredClauseOptions) . '}'; throw new MissingRequiredClauseException( - "Missing required clauses. One of $requiredClauseStr needed."); + "Missing required clauses. One of $requiredClauseStr needed." + ); } /** * Return the list of primary query clauses. + * + * @return non-empty-list */ - private static function getPrimaryClauses() { + private static function getPrimaryClauses(): array { return array_keys(self::$possibleClauses); } @@ -396,15 +410,20 @@ private static function getPrimaryClauses() { * Return the primary clause in this QueryGenerator instance. * If multiple primary clauses have been set, all but the first set clause * will be ignored. + * + * @return string|false */ - private function getPrimaryMethod() { + private function getPrimaryMethod(): string|bool { $primaryClauses = self::getPrimaryClauses(); $setMethods = $this->getSetMethods(); $setPrimaryClauses = array_intersect($primaryClauses, $setMethods); return reset($setPrimaryClauses); } - private function getSetMethods() { + /** + * @return array + */ + private function getSetMethods(): array { $methods = array_keys(array_filter($this->clauses)); return array_combine($methods, $methods); } @@ -418,7 +437,7 @@ private function getSetMethods() { * constructClause('where') => 'WHERE (foo = ?) AND (bar != ?)' * constructClause('where', false) => '(foo = ?) AND (bar != ?)' */ - private function constructClause($method, $skipClause = false) { + private function constructClause(string $method, bool $skipClause = false): string { $clauseInfo = self::$methods[$method]; $prefix = $clauseInfo['prefix']; $clause = $clauseInfo['clause']; @@ -443,8 +462,10 @@ private function constructClause($method, $skipClause = false) { /** * return the appropriate glue string for the given clause, taking into * account $this->useOr + * + * @return string|false */ - private function getGlue($method) { + private function getGlue(string $method): string|bool { if ($method !== 'where' || !$this->useOr) { return self::$methods[$method]['glue']; } else { diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 0d967d5..516e89f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,26 +1,15 @@ - - - - - - - - - - - - - - - - - + + + + + +