From f19a0f0005c2b829f31e7630b0b88842dca59e2d Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 22 Jul 2021 18:37:51 -0400 Subject: [PATCH] REF - Use function rather than static variable to return SQLFunction params --- Civi/Api4/Query/SqlFunction.php | 11 ++--- Civi/Api4/Query/SqlFunctionABS.php | 14 +++--- Civi/Api4/Query/SqlFunctionAVG.php | 14 +++--- Civi/Api4/Query/SqlFunctionCOALESCE.php | 14 +++--- Civi/Api4/Query/SqlFunctionCONCAT.php | 16 ++++--- Civi/Api4/Query/SqlFunctionCOUNT.php | 18 ++++---- Civi/Api4/Query/SqlFunctionCURDATE.php | 4 ++ Civi/Api4/Query/SqlFunctionGREATEST.php | 14 +++--- Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php | 50 +++++++++++---------- Civi/Api4/Query/SqlFunctionIF.php | 16 ++++--- Civi/Api4/Query/SqlFunctionISNULL.php | 12 ++--- Civi/Api4/Query/SqlFunctionLEAST.php | 14 +++--- Civi/Api4/Query/SqlFunctionLOWER.php | 14 +++--- Civi/Api4/Query/SqlFunctionMAX.php | 14 +++--- Civi/Api4/Query/SqlFunctionMIN.php | 14 +++--- Civi/Api4/Query/SqlFunctionNULLIF.php | 16 ++++--- Civi/Api4/Query/SqlFunctionREPLACE.php | 30 +++++++------ Civi/Api4/Query/SqlFunctionROUND.php | 22 ++++----- Civi/Api4/Query/SqlFunctionSUM.php | 14 +++--- Civi/Api4/Query/SqlFunctionUPPER.php | 14 +++--- 20 files changed, 186 insertions(+), 149 deletions(-) diff --git a/Civi/Api4/Query/SqlFunction.php b/Civi/Api4/Query/SqlFunction.php index 44923c6a56..542c567919 100644 --- a/Civi/Api4/Query/SqlFunction.php +++ b/Civi/Api4/Query/SqlFunction.php @@ -18,11 +18,6 @@ namespace Civi\Api4\Query; */ abstract class SqlFunction extends SqlExpression { - /** - * @var array - */ - protected static $params = []; - /** * @var array[] */ @@ -224,9 +219,9 @@ abstract class SqlFunction extends SqlExpression { * Get the param metadata for this sql function. * @return array */ - public static function getParams(): array { + final public static function getParams(): array { $params = []; - foreach (static::$params as $param) { + foreach (static::params() as $param) { // Merge in defaults to ensure each param has these properties $params[] = $param + [ 'prefix' => [], @@ -242,6 +237,8 @@ abstract class SqlFunction extends SqlExpression { return $params; } + abstract protected static function params(): array; + /** * Get the arguments passed to this sql function instance. * @return array[] diff --git a/Civi/Api4/Query/SqlFunctionABS.php b/Civi/Api4/Query/SqlFunctionABS.php index a5909ec818..015906c4de 100644 --- a/Civi/Api4/Query/SqlFunctionABS.php +++ b/Civi/Api4/Query/SqlFunctionABS.php @@ -18,12 +18,14 @@ class SqlFunctionABS extends SqlFunction { protected static $category = self::CATEGORY_MATH; - protected static $params = [ - [ - 'optional' => FALSE, - 'must_be' => ['SqlField', 'SqlNumber'], - ], - ]; + protected static function params(): array { + return [ + [ + 'optional' => FALSE, + 'must_be' => ['SqlField', 'SqlNumber'], + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionAVG.php b/Civi/Api4/Query/SqlFunctionAVG.php index a9787d2e96..60aff454e8 100644 --- a/Civi/Api4/Query/SqlFunctionAVG.php +++ b/Civi/Api4/Query/SqlFunctionAVG.php @@ -18,12 +18,14 @@ class SqlFunctionAVG extends SqlFunction { protected static $category = self::CATEGORY_AGGREGATE; - protected static $params = [ - [ - 'prefix' => ['', 'DISTINCT', 'ALL'], - 'must_be' => ['SqlField'], - ], - ]; + protected static function params(): array { + return [ + [ + 'prefix' => ['', 'DISTINCT', 'ALL'], + 'must_be' => ['SqlField'], + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionCOALESCE.php b/Civi/Api4/Query/SqlFunctionCOALESCE.php index 324d62fecf..16f9ec8e67 100644 --- a/Civi/Api4/Query/SqlFunctionCOALESCE.php +++ b/Civi/Api4/Query/SqlFunctionCOALESCE.php @@ -18,12 +18,14 @@ class SqlFunctionCOALESCE extends SqlFunction { protected static $category = self::CATEGORY_COMPARISON; - protected static $params = [ - [ - 'max_expr' => 99, - 'optional' => FALSE, - ], - ]; + protected static function params(): array { + return [ + [ + 'max_expr' => 99, + 'optional' => FALSE, + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionCONCAT.php b/Civi/Api4/Query/SqlFunctionCONCAT.php index 94f90abdeb..6143277b66 100644 --- a/Civi/Api4/Query/SqlFunctionCONCAT.php +++ b/Civi/Api4/Query/SqlFunctionCONCAT.php @@ -18,13 +18,15 @@ class SqlFunctionCONCAT extends SqlFunction { protected static $category = self::CATEGORY_STRING; - protected static $params = [ - [ - 'max_expr' => 99, - 'optional' => FALSE, - 'must_be' => ['SqlField', 'SqlString'], - ], - ]; + protected static function params(): array { + return [ + [ + 'max_expr' => 99, + 'optional' => FALSE, + 'must_be' => ['SqlField', 'SqlString'], + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionCOUNT.php b/Civi/Api4/Query/SqlFunctionCOUNT.php index 8d52c9c733..6e933f011c 100644 --- a/Civi/Api4/Query/SqlFunctionCOUNT.php +++ b/Civi/Api4/Query/SqlFunctionCOUNT.php @@ -18,14 +18,16 @@ class SqlFunctionCOUNT extends SqlFunction { protected static $category = self::CATEGORY_AGGREGATE; - protected static $params = [ - [ - 'prefix' => ['', 'DISTINCT', 'ALL'], - 'max_expr' => 1, - 'must_be' => ['SqlField', 'SqlWild'], - 'cant_be' => [], - ], - ]; + protected static function params(): array { + return [ + [ + 'prefix' => ['', 'DISTINCT', 'ALL'], + 'max_expr' => 1, + 'must_be' => ['SqlField', 'SqlWild'], + 'cant_be' => [], + ], + ]; + } /** * Reformat result as integer diff --git a/Civi/Api4/Query/SqlFunctionCURDATE.php b/Civi/Api4/Query/SqlFunctionCURDATE.php index b7687d711e..f81993b536 100644 --- a/Civi/Api4/Query/SqlFunctionCURDATE.php +++ b/Civi/Api4/Query/SqlFunctionCURDATE.php @@ -18,6 +18,10 @@ class SqlFunctionCURDATE extends SqlFunction { protected static $category = self::CATEGORY_DATE; + protected static function params(): array { + return []; + } + /** * @return string */ diff --git a/Civi/Api4/Query/SqlFunctionGREATEST.php b/Civi/Api4/Query/SqlFunctionGREATEST.php index 47212da6bb..549e59a3de 100644 --- a/Civi/Api4/Query/SqlFunctionGREATEST.php +++ b/Civi/Api4/Query/SqlFunctionGREATEST.php @@ -20,12 +20,14 @@ class SqlFunctionGREATEST extends SqlFunction { protected static $category = self::CATEGORY_COMPARISON; - protected static $params = [ - [ - 'max_expr' => 99, - 'optional' => FALSE, - ], - ]; + protected static function params(): array { + return [ + [ + 'max_expr' => 99, + 'optional' => FALSE, + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php b/Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php index c4b9b923a1..6af0d62d48 100644 --- a/Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php +++ b/Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php @@ -20,31 +20,33 @@ class SqlFunctionGROUP_CONCAT extends SqlFunction { protected static $category = self::CATEGORY_AGGREGATE; - protected static $params = [ - [ - 'prefix' => ['', 'DISTINCT', 'ALL'], - 'max_expr' => 1, - 'must_be' => ['SqlField', 'SqlFunction'], - 'optional' => FALSE, - ], - [ - 'prefix' => ['ORDER BY'], - 'max_expr' => 1, - 'suffix' => ['', 'ASC', 'DESC'], - 'must_be' => ['SqlField'], - 'optional' => TRUE, - ], - [ - 'prefix' => ['SEPARATOR'], - 'max_expr' => 1, - 'must_be' => ['SqlString'], - 'optional' => TRUE, - // @see self::formatOutput() - 'api_default' => [ - 'expr' => ['"' . \CRM_Core_DAO::VALUE_SEPARATOR . '"'], + protected static function params(): array { + return [ + [ + 'prefix' => ['', 'DISTINCT', 'ALL'], + 'max_expr' => 1, + 'must_be' => ['SqlField', 'SqlFunction'], + 'optional' => FALSE, ], - ], - ]; + [ + 'prefix' => ['ORDER BY'], + 'max_expr' => 1, + 'suffix' => ['', 'ASC', 'DESC'], + 'must_be' => ['SqlField'], + 'optional' => TRUE, + ], + [ + 'prefix' => ['SEPARATOR'], + 'max_expr' => 1, + 'must_be' => ['SqlString'], + 'optional' => TRUE, + // @see self::formatOutput() + 'api_default' => [ + 'expr' => ['"' . \CRM_Core_DAO::VALUE_SEPARATOR . '"'], + ], + ], + ]; + } /** * Reformat result as array if using default separator diff --git a/Civi/Api4/Query/SqlFunctionIF.php b/Civi/Api4/Query/SqlFunctionIF.php index 4a1e244c7d..3e024a9217 100644 --- a/Civi/Api4/Query/SqlFunctionIF.php +++ b/Civi/Api4/Query/SqlFunctionIF.php @@ -18,13 +18,15 @@ class SqlFunctionIF extends SqlFunction { protected static $category = self::CATEGORY_COMPARISON; - protected static $params = [ - [ - 'min_expr' => 3, - 'max_expr' => 3, - 'optional' => FALSE, - ], - ]; + protected static function params(): array { + return [ + [ + 'min_expr' => 3, + 'max_expr' => 3, + 'optional' => FALSE, + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionISNULL.php b/Civi/Api4/Query/SqlFunctionISNULL.php index 7b614a5381..cfec8d82d8 100644 --- a/Civi/Api4/Query/SqlFunctionISNULL.php +++ b/Civi/Api4/Query/SqlFunctionISNULL.php @@ -18,11 +18,13 @@ class SqlFunctionISNULL extends SqlFunction { protected static $category = self::CATEGORY_COMPARISON; - protected static $params = [ - [ - 'optional' => FALSE, - ], - ]; + protected static function params(): array { + return [ + [ + 'optional' => FALSE, + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionLEAST.php b/Civi/Api4/Query/SqlFunctionLEAST.php index 89aa121e8c..6317647241 100644 --- a/Civi/Api4/Query/SqlFunctionLEAST.php +++ b/Civi/Api4/Query/SqlFunctionLEAST.php @@ -20,12 +20,14 @@ class SqlFunctionLEAST extends SqlFunction { protected static $category = self::CATEGORY_COMPARISON; - protected static $params = [ - [ - 'max_expr' => 99, - 'optional' => FALSE, - ], - ]; + protected static function params(): array { + return [ + [ + 'max_expr' => 99, + 'optional' => FALSE, + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionLOWER.php b/Civi/Api4/Query/SqlFunctionLOWER.php index 0dc052123f..47763c2bc4 100644 --- a/Civi/Api4/Query/SqlFunctionLOWER.php +++ b/Civi/Api4/Query/SqlFunctionLOWER.php @@ -18,12 +18,14 @@ class SqlFunctionLOWER extends SqlFunction { protected static $category = self::CATEGORY_STRING; - protected static $params = [ - [ - 'optional' => FALSE, - 'must_be' => ['SqlField', 'SqlString'], - ], - ]; + protected static function params(): array { + return [ + [ + 'optional' => FALSE, + 'must_be' => ['SqlField', 'SqlString'], + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionMAX.php b/Civi/Api4/Query/SqlFunctionMAX.php index f26a2c0aa5..71a3b1329f 100644 --- a/Civi/Api4/Query/SqlFunctionMAX.php +++ b/Civi/Api4/Query/SqlFunctionMAX.php @@ -20,12 +20,14 @@ class SqlFunctionMAX extends SqlFunction { protected static $category = self::CATEGORY_AGGREGATE; - protected static $params = [ - [ - 'prefix' => ['', 'DISTINCT', 'ALL'], - 'must_be' => ['SqlField'], - ], - ]; + protected static function params(): array { + return [ + [ + 'prefix' => ['', 'DISTINCT', 'ALL'], + 'must_be' => ['SqlField'], + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionMIN.php b/Civi/Api4/Query/SqlFunctionMIN.php index 756aeb0392..ca8d48a3f8 100644 --- a/Civi/Api4/Query/SqlFunctionMIN.php +++ b/Civi/Api4/Query/SqlFunctionMIN.php @@ -20,12 +20,14 @@ class SqlFunctionMIN extends SqlFunction { protected static $category = self::CATEGORY_AGGREGATE; - protected static $params = [ - [ - 'prefix' => ['', 'DISTINCT', 'ALL'], - 'must_be' => ['SqlField'], - ], - ]; + protected static function params(): array { + return [ + [ + 'prefix' => ['', 'DISTINCT', 'ALL'], + 'must_be' => ['SqlField'], + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionNULLIF.php b/Civi/Api4/Query/SqlFunctionNULLIF.php index f5602cd2cd..79e117a7fd 100644 --- a/Civi/Api4/Query/SqlFunctionNULLIF.php +++ b/Civi/Api4/Query/SqlFunctionNULLIF.php @@ -20,13 +20,15 @@ class SqlFunctionNULLIF extends SqlFunction { protected static $category = self::CATEGORY_COMPARISON; - protected static $params = [ - [ - 'min_expr' => 2, - 'max_expr' => 2, - 'optional' => FALSE, - ], - ]; + protected static function params(): array { + return [ + [ + 'min_expr' => 2, + 'max_expr' => 2, + 'optional' => FALSE, + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionREPLACE.php b/Civi/Api4/Query/SqlFunctionREPLACE.php index b43a7ab802..75350f1a87 100644 --- a/Civi/Api4/Query/SqlFunctionREPLACE.php +++ b/Civi/Api4/Query/SqlFunctionREPLACE.php @@ -18,20 +18,22 @@ class SqlFunctionREPLACE extends SqlFunction { protected static $category = self::CATEGORY_STRING; - protected static $params = [ - [ - 'optional' => FALSE, - 'must_be' => ['SqlField', 'SqlString'], - ], - [ - 'optional' => FALSE, - 'must_be' => ['SqlField', 'SqlString'], - ], - [ - 'optional' => FALSE, - 'must_be' => ['SqlField', 'SqlString'], - ], - ]; + protected static function params(): array { + return [ + [ + 'optional' => FALSE, + 'must_be' => ['SqlField', 'SqlString'], + ], + [ + 'optional' => FALSE, + 'must_be' => ['SqlField', 'SqlString'], + ], + [ + 'optional' => FALSE, + 'must_be' => ['SqlField', 'SqlString'], + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionROUND.php b/Civi/Api4/Query/SqlFunctionROUND.php index 28ee78caab..5a57c584b1 100644 --- a/Civi/Api4/Query/SqlFunctionROUND.php +++ b/Civi/Api4/Query/SqlFunctionROUND.php @@ -18,16 +18,18 @@ class SqlFunctionROUND extends SqlFunction { protected static $category = self::CATEGORY_MATH; - protected static $params = [ - [ - 'optional' => FALSE, - 'must_be' => ['SqlField', 'SqlNumber'], - ], - [ - 'optional' => TRUE, - 'must_be' => ['SqlNumber'], - ], - ]; + protected static function params(): array { + return [ + [ + 'optional' => FALSE, + 'must_be' => ['SqlField', 'SqlNumber'], + ], + [ + 'optional' => TRUE, + 'must_be' => ['SqlNumber'], + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionSUM.php b/Civi/Api4/Query/SqlFunctionSUM.php index 81b49005f6..0354036cd9 100644 --- a/Civi/Api4/Query/SqlFunctionSUM.php +++ b/Civi/Api4/Query/SqlFunctionSUM.php @@ -18,12 +18,14 @@ class SqlFunctionSUM extends SqlFunction { protected static $category = self::CATEGORY_AGGREGATE; - protected static $params = [ - [ - 'prefix' => ['', 'DISTINCT', 'ALL'], - 'must_be' => ['SqlField'], - ], - ]; + protected static function params(): array { + return [ + [ + 'prefix' => ['', 'DISTINCT', 'ALL'], + 'must_be' => ['SqlField'], + ], + ]; + } /** * @return string diff --git a/Civi/Api4/Query/SqlFunctionUPPER.php b/Civi/Api4/Query/SqlFunctionUPPER.php index ed4590e290..cc21cd1aaa 100644 --- a/Civi/Api4/Query/SqlFunctionUPPER.php +++ b/Civi/Api4/Query/SqlFunctionUPPER.php @@ -18,12 +18,14 @@ class SqlFunctionUPPER extends SqlFunction { protected static $category = self::CATEGORY_STRING; - protected static $params = [ - [ - 'optional' => FALSE, - 'must_be' => ['SqlField', 'SqlString'], - ], - ]; + protected static function params(): array { + return [ + [ + 'optional' => FALSE, + 'must_be' => ['SqlField', 'SqlString'], + ], + ]; + } /** * @return string -- 2.25.1