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 |
||
22 | public function sanitizeWhereArgs(array $where_args, array $arg_mapping, array $id_fields, array $options = []) |
||
23 | { |
||
24 | // if "include_all_args" is true, then the incoming $where_args array |
||
25 | // will be copied to the outgoing $where_params prior to sanitizing the fields. |
||
26 | // so ALL elements in the $where_args array will be present in the $where_params array |
||
27 | $include_all_args = isset($options['include_all_args']) |
||
28 | ? filter_var($options['include_all_args'], FILTER_VALIDATE_BOOLEAN) |
||
29 | : false; |
||
30 | // if "use_IN_operator" is true, then any ID args found in the $id_fields array |
||
31 | // will have their values converted to use an SQL "IN" clause format |
||
32 | // if the value returned from Relay::fromGlobalId() is an array of IDs |
||
33 | $use_IN_operator = isset($options['use_IN_operator']) |
||
34 | ? filter_var($options['use_IN_operator'], FILTER_VALIDATE_BOOLEAN) |
||
35 | : false; |
||
36 | $where_params = $include_all_args ? $where_args : []; |
||
37 | foreach ($where_args as $arg => $value) { |
||
38 | if (! array_key_exists($arg, $arg_mapping)) { |
||
39 | continue; |
||
40 | } |
||
41 | if (is_array($value) && ! empty($value)) { |
||
42 | $value = array_map( |
||
43 | static function ($value) { |
||
44 | if (is_string($value)) { |
||
45 | $value = sanitize_text_field($value); |
||
46 | } |
||
47 | return $value; |
||
48 | }, |
||
49 | $value |
||
50 | ); |
||
51 | } elseif (is_string($value)) { |
||
52 | $value = sanitize_text_field($value); |
||
53 | } |
||
54 | if (in_array($arg, $id_fields, true)) { |
||
55 | $ID = $this->convertGlobalId($value); |
||
56 | // Use the proper operator. |
||
57 | $value = $use_IN_operator && is_array($ID) ? ['IN', $ID] : $ID; |
||
58 | } |
||
59 | $where_params[ $arg_mapping[ $arg ] ] = $value; |
||
60 | } |
||
61 | return $where_params; |
||
62 | } |
||
63 | |||
80 |