Conditions | 5 |
Paths | 1 |
Total Lines | 53 |
Lines | 53 |
Ratio | 100 % |
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(EEM_Datetime $model, Datetime $type) |
||
31 | { |
||
32 | /** |
||
33 | * Creates 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) use ($model, $type) { |
||
47 | |||
48 | /** |
||
49 | * Stop now if a user isn't allowed to create a datetime. |
||
50 | */ |
||
51 | if (! current_user_can('ee_edit_events')) { |
||
52 | // translators: the %1$s is the name of the object being mutated |
||
53 | throw new UserError( |
||
54 | sprintf(__('Sorry, you are not allowed to create %1$s', 'event_espresso'), $type->name()) |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | $tickets = []; |
||
59 | |||
60 | $args = DatetimeMutation::prepareFields($input); |
||
61 | |||
62 | if (isset($args['tickets'])) { |
||
63 | $tickets = $args['tickets']; |
||
64 | unset($args['tickets']); |
||
65 | } |
||
66 | |||
67 | $entity = EE_Datetime::new_instance($args); |
||
68 | $id = $entity->save(); |
||
69 | |||
70 | if (empty($id)) { |
||
71 | throw new UserError(__('The object failed to create but no error was provided', 'event_espresso')); |
||
72 | } |
||
73 | |||
74 | if (! empty($tickets)) { |
||
75 | DatetimeMutation::setRelatedTickets($entity, $tickets); |
||
76 | } |
||
77 | |||
78 | return [ |
||
79 | 'id' => $id, |
||
80 | ]; |
||
81 | }; |
||
82 | } |
||
83 | } |
||
84 |