| Conditions | 13 |
| Paths | 136 |
| Total Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 28 | public function sanitizeWhereArgs(array $where_args, array $arg_mapping, array $id_fields, array $options = []) |
||
| 29 | { |
||
| 30 | // if "include_all_args" is true, then the incoming $where_args array |
||
| 31 | // will be copied to the outgoing $where_params prior to sanitizing the fields. |
||
| 32 | // so ALL elements in the $where_args array will be present in the $where_params array |
||
| 33 | $include_all_args = isset($options['include_all_args']) |
||
| 34 | ? filter_var($options['include_all_args'], FILTER_VALIDATE_BOOLEAN) |
||
| 35 | : false; |
||
| 36 | // if "use_IN_operator" is true, then any ID args found in the $id_fields array |
||
| 37 | // will have their values converted to use an SQL "IN" clause format |
||
| 38 | // if the value returned from Relay::fromGlobalId() is an array of IDs |
||
| 39 | $use_IN_operator = isset($options['use_IN_operator']) |
||
| 40 | ? filter_var($options['use_IN_operator'], FILTER_VALIDATE_BOOLEAN) |
||
| 41 | : false; |
||
| 42 | $where_params = $include_all_args ? $where_args : []; |
||
| 43 | foreach ($where_args as $arg => $value) { |
||
| 44 | if (! array_key_exists($arg, $arg_mapping)) { |
||
| 45 | continue; |
||
| 46 | } |
||
| 47 | if (is_array($value) && ! empty($value)) { |
||
| 48 | $value = array_map( |
||
| 49 | static function ($value) { |
||
| 50 | if (is_string($value)) { |
||
| 51 | $value = sanitize_text_field($value); |
||
| 52 | } |
||
| 53 | return $value; |
||
| 54 | }, |
||
| 55 | $value |
||
| 56 | ); |
||
| 57 | } elseif (is_string($value)) { |
||
| 58 | $value = sanitize_text_field($value); |
||
| 59 | } |
||
| 60 | if (in_array($arg, $id_fields, true)) { |
||
| 61 | $ID = $this->convertFromGlobalId($value); |
||
| 62 | // Use the proper operator. |
||
| 63 | $value = $use_IN_operator && is_array($ID) ? ['IN', $ID] : $ID; |
||
| 64 | } |
||
| 65 | $where_params[ $arg_mapping[ $arg ] ] = $value; |
||
| 66 | } |
||
| 67 | return $where_params; |
||
| 68 | } |
||
| 69 | |||
| 108 |