From ae3bedfe763113aa499899f577f08b4c438b1e63 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 5 Dec 2018 22:57:12 -0600 Subject: [PATCH] Update: remove $append parameters, simplifying functions The $append is a convenience variable that does not need to exist. If an array needs to be reset, one shoud call the function with NULL before adding anything new. --- common/base/classes/base_path.php | 40 ++++++------- .../database/classes/database_alter_aggregate.php | 48 ++++----------- .../classes/database_alter_default_privileges.php | 68 +++++----------------- common/database/traits/database_in_schema.php | 24 ++------ common/database/traits/database_options.php | 30 +++------- 5 files changed, 56 insertions(+), 154 deletions(-) diff --git a/common/base/classes/base_path.php b/common/base/classes/base_path.php index 19b2476..b3aa64c 100644 --- a/common/base/classes/base_path.php +++ b/common/base/classes/base_path.php @@ -610,28 +610,26 @@ class c_base_path extends c_base_return_string { /** * Assign an allowed http method. * - * @param int $method + * @param int|null $method * The id of the method to allow. - * @param bool $append - * (optional) When TRUE, the method id is appended. - * When FALSE, the array is re-created with $method as the only array value. + * When NULL, the allowed_methods array is reset to an empty array. * * @return c_base_return_status * TRUE on success, FALSE otherwise. * FALSE with error bit set is returned on error. */ - public function set_allowed_method($method, $append = TRUE) { - if (!is_int($method)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'method', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); + public function set_allowed_method($method) { + if (is_null($method)) { + $this->allowed_methods = []; + return new c_base_return_true(); } - if (!is_bool($append)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + if (!is_int($method)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'method', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); return c_base_return_error::s_false($error); } - if (!$append) { + if (!is_array($this->allowed_methods)) { $this->allowed_methods = []; } @@ -1488,28 +1486,26 @@ class c_base_path_executed extends c_base_array { /** * Assign cookies. * - * @param c_base_cookie $cookie + * @param c_base_cookie|null $cookie * The cookie to assign. - * @param bool $append - * (optional) When TRUE the $cookie is appended. - * When FALSE, the array is reset with $cookie as the only value in the array. + * When NULL, the cookies array is reset to an empty array. * * @return c_base_return_status * TRUE is returned on success. * FALSE with error bit set is returned on error. */ - public function set_cookies($cookie, $append = TRUE) { - if (!($cookie instanceof c_base_cookie)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'cookie', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); + public function set_cookies($cookie) { + if (is_null($cookie)) { + $this->cookies = []; + return new c_base_return_true(); } - if (!is_bool($append)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + if (!($cookie instanceof c_base_cookie)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'cookie', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); return c_base_return_error::s_false($error); } - if (!$append || !is_array($this->cookies)) { + if (!is_array($this->cookies)) { $this->cookies = []; } diff --git a/common/database/classes/database_alter_aggregate.php b/common/database/classes/database_alter_aggregate.php index c40fa31..9f10ee8 100644 --- a/common/database/classes/database_alter_aggregate.php +++ b/common/database/classes/database_alter_aggregate.php @@ -92,38 +92,24 @@ class c_database_alter_aggregate extends c_database_query { * @param c_database_argument_aggregate_signature|null $aggregate_signature * The aggregate signatures to use. * Set to NULL to disable. - * When NULL, this will remove all aggregate signatures regardless of the $append parameter. - * @param bool $append - * (optional) When TRUE, the aggregate signatures will be appended. - * When FALSE, any existing aggregate signatures will be cleared before appending the aggregate signature. + * When NULL, this will remove all values. * * @return c_base_return_status * TRUE on success, FALSE otherwise. * FALSE with error bit set is returned on error. */ - public function set_aggregate_signature($aggregate_signature, $append = TRUE) { + public function set_aggregate_signature($aggregate_signature) { if (is_null($aggregate_signature)) { $this->aggregate_signatures = NULL; return new c_base_return_true(); } - if (!is_bool($append)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - if ($aggregate_signature instanceof c_database_argument_aggregate_signature) { - if ($append) { - if (!is_array($this->aggregate_signatures)) { - $this->aggregate_signatures = []; - } - - $this->aggregate_signatures[] = $aggregate_signature; - } - else { - $this->aggregate_signatures = [$aggregate_signature]; + if (!is_array($this->aggregate_signatures)) { + $this->aggregate_signatures = []; } + $this->aggregate_signatures[] = $aggregate_signature; return new c_base_return_true(); } @@ -137,38 +123,24 @@ class c_database_alter_aggregate extends c_database_query { * @param c_database_argument_aggregate_signature_base|null $order_by_signature * The order by aggregate signature to use. * Set to NULL to disable. - * When NULL, this will remove all modes regardless of the $append parameter. - * @param bool $append - * (optional) When TRUE, the argument mode will be appended. - * When FALSE, any existing aggregate signatures will be cleared before appending the aggregate signature. + * When NULL, this will remove all values. * * @return c_base_return_status * TRUE on success, FALSE otherwise. * FALSE with error bit set is returned on error. */ - public function set_order_by_signature($order_by_signature, $append = TRUE) { + public function set_order_by_signature($order_by_signature) { if (is_null($order_by_signature)) { $this->order_by_signature = NULL; return new c_base_return_true(); } - if (!is_bool($append)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - if ($order_by_signature instanceof c_database_argument_aggregate_signature_base) { - if ($append) { - if (!is_array($this->order_by_signature)) { - $this->order_by_signatures = []; - } - - $this->order_by_signatures[] = $order_by_signature; - } - else { - $this->order_by_signatures = [$order_by_signature]; + if (!is_array($this->order_by_signature)) { + $this->order_by_signatures = []; } + $this->order_by_signatures[] = $order_by_signature; return new c_base_return_true(); } diff --git a/common/database/classes/database_alter_default_privileges.php b/common/database/classes/database_alter_default_privileges.php index 2978262..1d3996c 100644 --- a/common/database/classes/database_alter_default_privileges.php +++ b/common/database/classes/database_alter_default_privileges.php @@ -129,33 +129,25 @@ class c_database_alter_default_priveleges extends c_database_query { * The for role target to use. * Set to TRUE to use (only) the current role, $append is considered FALSE. * Set to NULL to disable. - * When NULL, this will remove all for role targets regardless of the $append parameter. - * @param bool $append - * (optional) When TRUE, the for role target will be appended. - * When FALSE, any existing for role targets will be cleared before appending the for role target. + * When NULL, this will remove all values. * * @return c_base_return_status * TRUE on success, FALSE otherwise. * FALSE with error bit set is returned on error. */ - public function set_for_role_targets($target, $append = TRUE) { + public function set_for_role_targets($target) { if (is_null($target)) { $this->target = NULL; return new c_base_return_true(); } if (is_string($target)) { - if ($append) { - if (!is_array($this->for_role_targets)) { - $this->for_role_targets = []; - } - - $this->for_role_targets[] = $target; - } - else { - $this->for_role_targets = [$target]; + if (!is_array($this->for_role_targets)) { + $this->for_role_targets = []; } + $this->for_role_targets[] = $target; + return new c_base_return_true(); } else if ($target === TRUE) { @@ -227,38 +219,24 @@ class c_database_alter_default_priveleges extends c_database_query { * @param int|null $privilege * Whether or not to use the ON operation in the query. * Set to NULL to disable. - * When NULL, this will remove all privileges regardless of the $append parameter. - * @param bool $append - * (optional) When TRUE, the aggregate signatures will be appended. - * When FALSE, any existing aggregate signatures will be cleared before appending the aggregate signature. + * When NULL, this will remove all values. * * @return c_base_return_status * TRUE on success, FALSE otherwise. * FALSE with error bit set is returned on error. */ - public function set_privilege($privilege, $append = TRUE) { + public function set_privilege($privilege) { if (is_null($privilege)) { $this->privileges = NULL; return new c_base_return_true(); } - if (!is_bool($append)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - if (is_int($privilege)) { - if ($append) { - if (!is_array($this->privilege)) { - $this->privileges = []; - } - - $this->privileges[] = $privilege; - } - else { - $this->privileges = [$privilege]; + if (!is_array($this->privilege)) { + $this->privileges = []; } + $this->privileges[] = $privilege; return new c_base_return_true(); } @@ -272,38 +250,24 @@ class c_database_alter_default_priveleges extends c_database_query { * @param c_database_argument_role_name|null $role_name * The role name to use. * Set to NULL to disable. - * When NULL, this will remove all role names regardless of the $append parameter. - * @param bool $append - * (optional) When TRUE, the role name will be appended. - * When FALSE, any existing role names will be cleared before appending the role name. + * When NULL, this will remove all values. * * @return c_base_return_status * TRUE on success, FALSE otherwise. * FALSE with error bit set is returned on error. */ - public function set_role_name($role_name, $append = TRUE) { + public function set_role_name($role_name) { if (is_null($role_name)) { $this->role_names = NULL; return new c_base_return_true(); } - if (!is_bool($append)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - if ($role_name instanceof c_database_argument_role_name) { - if ($append) { - if (!is_array($this->role_names)) { - $this->role_names = []; - } - - $this->role_names[] = $role_name; - } - else { - $this->role_names = [$role_name]; + if (!is_array($this->role_names)) { + $this->role_names = []; } + $this->role_names[] = $role_name; return new c_base_return_true(); } diff --git a/common/database/traits/database_in_schema.php b/common/database/traits/database_in_schema.php index f5927e5..56c8cbe 100644 --- a/common/database/traits/database_in_schema.php +++ b/common/database/traits/database_in_schema.php @@ -26,38 +26,24 @@ trait t_database_in_schema { * @param string|null $schema_name * The schema name to use. * Set to NULL to disable. - * When NULL, this will remove all schema names regardless of the $append parameter. - * @param bool $append - * (optional) When TRUE, the schema name will be appended. - * When FALSE, any existing schema names will be cleared before appending the schema name. + * When NULL, this will remove all values. * * @return c_base_return_status * TRUE on success, FALSE otherwise. * FALSE with error bit set is returned on error. */ - public function set_in_schema($schema_name, $append = TRUE) { + public function set_in_schema($schema_name) { if (is_null($schema_name)) { $this->in_schema = NULL; return new c_base_return_true(); } - if (!is_bool($append)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - if (is_string($schema_name)) { - if ($append) { - if (!is_array($this->in_schema)) { - $this->in_schema = []; - } - - $this->in_schema[] = $schema_name; - } - else { - $this->in_schema = [$schema_name]; + if (!is_array($this->in_schema)) { + $this->in_schema = []; } + $this->in_schema[] = $schema_name; return new c_base_return_true(); } diff --git a/common/database/traits/database_options.php b/common/database/traits/database_options.php index 00c2c74..389d00e 100644 --- a/common/database/traits/database_options.php +++ b/common/database/traits/database_options.php @@ -26,19 +26,16 @@ trait t_database_options { * @param string|null $options_type * The option type to use. * Set to NULL to disable. - * When NULL, this will remove all options regardless of the $append parameter. + * When NULL, this will remove all values. * @param string $value * The value to associate with the options_type. * When options_type is NULL, this is ignored. - * @param bool $append - * (optional) When TRUE, the schema name will be appended. - * When FALSE, any existing schema names will be cleared before appending the schema name. * * @return c_base_return_status * TRUE on success, FALSE otherwise. * FALSE with error bit set is returned on error. */ - public function set_options($options_type, $value, $append = TRUE) { + public function set_options($options_type, $value) { if (is_null($options_type)) { $this->options = NULL; return new c_base_return_true(); @@ -49,11 +46,6 @@ trait t_database_options { return c_base_return_error::s_false($error); } - if (!is_bool($append)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - switch ($options_type) { case e_database_options::ADD: case e_database_options::DROP: @@ -64,23 +56,15 @@ trait t_database_options { return c_base_return_error::s_false($error); }; - $options = [ + if (!is_array($this->options)) { + $this->options = []; + } + + $this->options[] = [ 'type' => $options_type, 'value' => $value, ]; - if ($append) { - if (!is_array($this->options)) { - $this->options = []; - } - - $this->options[] = $options; - } - else { - $this->options = [$options]; - } - unset($options); - return new c_base_return_true(); } -- 1.8.3.1