Conditions | 12 |
Paths | 86 |
Total Lines | 111 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
27 | public function execute($json) { |
||
28 | /* If json_encode produces an empty string, wa-js shows an absolutely |
||
29 | * worthless "Invalid data received from the server" dialog. */ |
||
30 | $jsonflags = JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE; |
||
31 | |||
32 | try { |
||
33 | // decode JSON data |
||
34 | $data = json_decode_data($json, true); |
||
35 | |||
36 | // Reset the bus |
||
37 | $GLOBALS["bus"]->reset(); |
||
38 | |||
39 | // notify modules that wants to do something at the start of a request |
||
40 | $GLOBALS["bus"]->notify(REQUEST_ENTRYID, REQUEST_START); |
||
41 | |||
42 | // Check if the JSON is parsed correctly into an array |
||
43 | $data = $data["zarafa"] ?: false; |
||
44 | |||
45 | // @TODO throw exception if zarafa tag is not present |
||
46 | if (is_array($data)) { |
||
47 | // iterate over all module names |
||
48 | foreach ($data as $moduleName => $modules) { |
||
49 | // each module can contain multiple requests using different module ids |
||
50 | foreach ($modules as $moduleId => $moduleData) { |
||
51 | // Create the module via the Dispatcher |
||
52 | $moduleObj = $GLOBALS["dispatcher"]->loadModule($moduleName, $moduleId, $moduleData); |
||
53 | |||
54 | // Check if the module is loaded |
||
55 | if (is_object($moduleObj)) { |
||
56 | $moduleObj->loadSessionData(); |
||
57 | |||
58 | // Execute the actions in the module |
||
59 | $moduleObj->execute(); |
||
60 | |||
61 | $moduleObj->saveSessionData(); |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | |||
67 | // notify modules that wants to do something at the end of a request |
||
68 | $GLOBALS["bus"]->notify(REQUEST_ENTRYID, REQUEST_END); |
||
69 | |||
70 | // Build the JSON and return it |
||
71 | return json_encode(["zarafa" => $GLOBALS["bus"]->getData()], $jsonflags); |
||
72 | } |
||
73 | catch (ZarafaException $e) { |
||
74 | if (!$e->isHandled) { |
||
75 | $data = [ |
||
76 | "error" => [ |
||
77 | "type" => ERROR_ZARAFA, |
||
78 | "info" => [ |
||
79 | "file" => $e->getFileLine(), |
||
80 | "display_message" => $e->getDisplayMessage(), |
||
81 | "original_message" => $e->getMessage(), |
||
82 | ], |
||
83 | ], |
||
84 | ]; |
||
85 | |||
86 | return json_encode(["zarafa" => $data], $jsonflags); |
||
87 | } |
||
88 | } |
||
89 | catch (ZarafaErrorException $e) { |
||
90 | if (!$e->isHandled) { |
||
91 | $data = [ |
||
92 | "error" => [ |
||
93 | "type" => ERROR_GENERAL, |
||
94 | "info" => [ |
||
95 | "file" => $e->getFileLine(), |
||
96 | "display_message" => $e->getDisplayMessage(), |
||
97 | "original_message" => $e->getMessage(), |
||
98 | ], |
||
99 | ], |
||
100 | ]; |
||
101 | |||
102 | return json_encode(["zarafa" => $data], $jsonflags); |
||
103 | } |
||
104 | } |
||
105 | catch (Exception $e) { |
||
106 | // handle exceptions that are not handled by modules |
||
107 | dump($e); |
||
108 | |||
109 | $data = [ |
||
110 | "error" => [ |
||
111 | "type" => ERROR_GENERAL, |
||
112 | "info" => [ |
||
113 | "file" => basename($e->getFile()) . ':' . $e->getLine(), |
||
114 | "display_message" => _('An unexpected error has occurred'), |
||
115 | "original_message" => $e->getMessage(), |
||
116 | ], |
||
117 | ], |
||
118 | ]; |
||
119 | |||
120 | return json_encode(["zarafa" => $data], $jsonflags); |
||
121 | } |
||
122 | catch (Throwable $e) { |
||
123 | // Catch PHP Errors/TypeErrors as JSON to avoid HTTP 500s |
||
124 | dump($e); |
||
125 | |||
126 | $data = [ |
||
127 | "error" => [ |
||
128 | "type" => ERROR_GENERAL, |
||
129 | "info" => [ |
||
130 | "file" => basename($e->getFile()) . ':' . $e->getLine(), |
||
131 | "display_message" => _('An unexpected error has occurred'), |
||
132 | "original_message" => $e->getMessage(), |
||
133 | ], |
||
134 | ], |
||
135 | ]; |
||
136 | |||
137 | return json_encode(["zarafa" => $data], $jsonflags); |
||
138 | } |
||
141 |