| Conditions | 27 |
| Paths | > 20000 |
| Total Lines | 96 |
| Lines | 20 |
| Ratio | 20.83 % |
| 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 |
||
| 37 | public static function prepareFields(array $input) |
||
| 38 | { |
||
| 39 | $args = []; |
||
| 40 | |||
| 41 | View Code Duplication | if (! empty($input['datetimes'])) { |
|
| 42 | $args['datetimes'] = array_map('sanitize_text_field', (array) $input['datetimes']); |
||
| 43 | } |
||
| 44 | |||
| 45 | if (! empty($input['description'])) { |
||
| 46 | $args['TKT_description'] = sanitize_text_field($input['description']); |
||
| 47 | } |
||
| 48 | |||
| 49 | View Code Duplication | if (! empty($input['endDate'])) { |
|
| 50 | $args['TKT_end_date'] = new DateTime(sanitize_text_field($input['endDate'])); |
||
| 51 | } |
||
| 52 | |||
| 53 | if (array_key_exists('isDefault', $input)) { |
||
| 54 | $args['TKT_is_default'] = (bool) $input['isDefault']; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (array_key_exists('isRequired', $input)) { |
||
| 58 | $args['TKT_required'] = (bool) $input['isRequired']; |
||
| 59 | } |
||
| 60 | |||
| 61 | if (array_key_exists('isTaxable', $input)) { |
||
| 62 | $args['TKT_taxable'] = (bool) $input['isTaxable']; |
||
| 63 | } |
||
| 64 | |||
| 65 | if (array_key_exists('isTrashed', $input)) { |
||
| 66 | $args['TKT_deleted'] = (bool) $input['isTrashed']; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (array_key_exists('max', $input)) { |
||
| 70 | $args['TKT_max'] = (int) $input['max']; |
||
| 71 | } |
||
| 72 | |||
| 73 | if (array_key_exists('min', $input)) { |
||
| 74 | $args['TKT_min'] = (int) $input['min']; |
||
| 75 | } |
||
| 76 | |||
| 77 | if (! empty($input['name'])) { |
||
| 78 | $args['TKT_name'] = sanitize_text_field($input['name']); |
||
| 79 | } |
||
| 80 | |||
| 81 | if (array_key_exists('order', $input)) { |
||
| 82 | $args['TKT_order'] = (int) $input['order']; |
||
| 83 | } |
||
| 84 | |||
| 85 | View Code Duplication | if (! empty($input['parent'])) { |
|
| 86 | $parts = Relay::fromGlobalId(sanitize_text_field($input['parent'])); |
||
| 87 | $args['TKT_parent'] = (! empty($parts['id']) && is_int($parts['id'])) ? $parts['id'] : null; |
||
| 88 | } |
||
| 89 | |||
| 90 | if (! empty($input['price'])) { |
||
| 91 | $args['TKT_price'] = (float) $input['price']; |
||
| 92 | } |
||
| 93 | |||
| 94 | View Code Duplication | if (! empty($input['prices'])) { |
|
| 95 | $args['prices'] = array_map('sanitize_text_field', (array) $input['prices']); |
||
| 96 | } |
||
| 97 | |||
| 98 | if (array_key_exists('quantity', $input)) { |
||
| 99 | $args['TKT_qty'] = (int) $input['quantity']; |
||
| 100 | } |
||
| 101 | |||
| 102 | if (array_key_exists('reserved', $input)) { |
||
| 103 | $args['TKT_reserved'] = (int) $input['reserved']; |
||
| 104 | } |
||
| 105 | |||
| 106 | if (array_key_exists('reverseCalculate', $input)) { |
||
| 107 | $args['TKT_reverse_calculate'] = (bool) $input['reverseCalculate']; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (array_key_exists('row', $input)) { |
||
| 111 | $args['TKT_row'] = (int) $input['row']; |
||
| 112 | } |
||
| 113 | |||
| 114 | if (array_key_exists('sold', $input)) { |
||
| 115 | $args['TKT_sold'] = (int) $input['sold']; |
||
| 116 | } |
||
| 117 | |||
| 118 | View Code Duplication | if (! empty($input['startDate'])) { |
|
| 119 | $args['TKT_start_date'] = new DateTime(sanitize_text_field($input['startDate'])); |
||
| 120 | } |
||
| 121 | |||
| 122 | if (array_key_exists('uses', $input)) { |
||
| 123 | $args['TKT_uses'] = (int) $input['uses']; |
||
| 124 | } |
||
| 125 | |||
| 126 | View Code Duplication | if (! empty($input['wpUser'])) { |
|
| 127 | $parts = Relay::fromGlobalId(sanitize_text_field($input['wpUser'])); |
||
| 128 | $args['TKT_wp_user'] = (! empty($parts['id']) && is_int($parts['id'])) ? $parts['id'] : null; |
||
| 129 | } |
||
| 130 | |||
| 131 | return $args; |
||
| 132 | } |
||
| 133 | |||
| 220 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.