| Conditions | 4 |
| Paths | 1 |
| Total Lines | 59 |
| 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 |
||
| 30 | public static function mutateAndGetPayload() |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Updates an entity. |
||
| 34 | * |
||
| 35 | * @param array $input The input for the mutation |
||
| 36 | * @param AppContext $context The AppContext passed down to all resolvers |
||
| 37 | * @param ResolveInfo $info The ResolveInfo passed down to all resolvers |
||
| 38 | * @return array |
||
| 39 | * @throws UserError |
||
| 40 | * @throws ReflectionException |
||
| 41 | * @throws InvalidArgumentException |
||
| 42 | * @throws InvalidInterfaceException |
||
| 43 | * @throws InvalidDataTypeException |
||
| 44 | * @throws EE_Error |
||
| 45 | */ |
||
| 46 | return static function ($input, AppContext $context, ResolveInfo $info) { |
||
| 47 | /** |
||
| 48 | * Stop now if a user isn't allowed to reorder. |
||
| 49 | */ |
||
| 50 | if (! current_user_can('ee_edit_events')) { |
||
| 51 | throw new UserError( |
||
| 52 | esc_html__('Sorry, you do not have the required permissions to reorder entities', 'event_espresso') |
||
| 53 | ); |
||
| 54 | } |
||
| 55 | |||
| 56 | $details = EntityReorder::prepareEntityDetailsFromInput($input); |
||
| 57 | |||
| 58 | $orderKey = $details['keyPrefix'] . '_order'; // e.g. "TKT_order" |
||
| 59 | |||
| 60 | $ok = false; |
||
| 61 | |||
| 62 | // We do not want to continue reorder if one fails. |
||
| 63 | // Thus wrap whole loop in try-catch |
||
| 64 | try { |
||
| 65 | foreach ($details['entityDbids'] as $order => $entityDbid) { |
||
| 66 | $args = [ |
||
| 67 | $orderKey => $order + 1, |
||
| 68 | ]; |
||
| 69 | $details['entities'][ $entityDbid ]->save($args); |
||
| 70 | } |
||
| 71 | $ok = true; |
||
| 72 | } catch (Exception $exception) { |
||
| 73 | new ExceptionStackTraceDisplay( |
||
| 74 | new RuntimeException( |
||
| 75 | sprintf( |
||
| 76 | esc_html__( |
||
| 77 | 'Failed to update order because of the following error(s): %1$s', |
||
| 78 | 'event_espresso' |
||
| 79 | ), |
||
| 80 | $exception->getMessage() |
||
| 81 | ) |
||
| 82 | ) |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | |||
| 86 | return compact('ok'); |
||
| 87 | }; |
||
| 88 | } |
||
| 89 | |||
| 170 |