Conditions | 7 |
Paths | 7 |
Total Lines | 64 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
138 | public function switchEditors(Event $event) |
||
139 | { |
||
140 | if ($event->data !== 'plugin_prosemirror_switch_editors') { |
||
141 | return; |
||
142 | } |
||
143 | $event->preventDefault(); |
||
144 | $event->stopPropagation(); |
||
145 | |||
146 | global $INPUT, $ID; |
||
147 | $ID = $INPUT->str('id'); |
||
148 | |||
149 | if ($INPUT->bool('getJSON')) { |
||
150 | $text = $INPUT->str('data'); |
||
151 | $instructions = p_get_instructions($text); |
||
152 | try { |
||
153 | $prosemirrorJSON = p_render('prosemirror', $instructions, $info); |
||
154 | } catch (Throwable $e) { |
||
155 | $errorMsg = 'Rendering the page\'s syntax for the WYSIWYG editor failed: '; |
||
156 | $errorMsg .= $e->getMessage(); |
||
157 | |||
158 | /** @var \helper_plugin_prosemirror $helper */ |
||
159 | $helper = plugin_load('helper', 'prosemirror'); |
||
160 | if ($helper->tryToLogErrorToSentry($e, ['text' => $text])) { |
||
161 | $errorMsg .= ' -- The error has been logged to Sentry.'; |
||
162 | } else { |
||
163 | $errorMsg .= '<code>' . $e->getFile() . ':' . $e->getLine() . '</code>'; |
||
164 | $errorMsg .= '<pre>' . $e->getTraceAsString() . '</pre>'; |
||
165 | } |
||
166 | |||
167 | http_status(500); |
||
168 | header('Content-Type: application/json'); |
||
169 | echo json_encode(['error' => $errorMsg]); |
||
170 | return; |
||
171 | } |
||
172 | $responseData = [ |
||
173 | 'json' => $prosemirrorJSON, |
||
174 | ]; |
||
175 | } else { |
||
176 | /** @var \helper_plugin_prosemirror $helper */ |
||
177 | $helper = plugin_load('helper', 'prosemirror'); |
||
178 | $json = $INPUT->str('data'); |
||
179 | try { |
||
180 | $syntax = $helper->getSyntaxFromProsemirrorData($json); |
||
181 | } catch (Throwable $e) { |
||
182 | $errorMsg = 'Parsing the data generated by Prosemirror failed with message: "'; |
||
183 | $errorMsg .= $e->getMessage(); |
||
184 | $errorMsg .= '"'; |
||
185 | |||
186 | if ($helper->tryToLogErrorToSentry($e, ['json' => $json])) { |
||
187 | $errorMsg .= ' -- The error has been logged to Sentry.'; |
||
188 | } |
||
189 | |||
190 | http_status(500); |
||
191 | header('Content-Type: application/json'); |
||
192 | echo json_encode(['error' => $errorMsg]); |
||
193 | return; |
||
194 | } |
||
195 | $responseData = [ |
||
196 | 'text' => $syntax, |
||
197 | ]; |
||
198 | } |
||
199 | |||
200 | header('Content-Type: application/json'); |
||
201 | echo json_encode($responseData); |
||
202 | } |
||
204 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: