Conditions | 8 |
Paths | 8 |
Total Lines | 65 |
Code Lines | 54 |
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 |
||
43 | public function handleAjax(Event $event) |
||
44 | { |
||
45 | if ($event->data !== 'plugin_prosemirror') { |
||
46 | return; |
||
47 | } |
||
48 | $event->preventDefault(); |
||
49 | $event->stopPropagation(); |
||
50 | |||
51 | global $INPUT, $ID; |
||
52 | $ID = $INPUT->str('id'); |
||
53 | $responseData = []; |
||
54 | foreach ($INPUT->arr('actions') as $action) { |
||
55 | switch ($action) { |
||
56 | case 'resolveInternalLink': |
||
57 | $inner = $INPUT->str('inner'); |
||
58 | $responseData[$action] = $this->resolveInternalLink($inner, $ID); |
||
59 | break; |
||
60 | case 'resolveInterWikiLink': |
||
61 | $inner = $INPUT->str('inner'); |
||
62 | [$shortcut, $reference] = explode('>', $inner); |
||
63 | $responseData[$action] = $this->resolveInterWikiLink($shortcut, $reference); |
||
64 | break; |
||
65 | case 'resolveMedia': |
||
66 | $attrs = $INPUT->arr('attrs'); |
||
67 | $responseData[$action] = [ |
||
68 | 'data-resolvedHtml' => ImageNode::resolveMedia( |
||
69 | $attrs['id'], |
||
70 | $attrs['title'], |
||
71 | $attrs['align'], |
||
72 | $attrs['width'], |
||
73 | $attrs['height'], |
||
74 | $attrs['cache'], |
||
75 | $attrs['linking'] |
||
76 | ) |
||
77 | ]; |
||
78 | break; |
||
79 | case 'resolveImageTitle': |
||
80 | $image = $INPUT->arr('image'); |
||
81 | $responseData[$action] = []; |
||
82 | $responseData[$action]['data-resolvedImage'] = LinkNode::resolveImageTitle( |
||
83 | $ID, |
||
84 | $image['id'], |
||
85 | $image['title'], |
||
86 | $image['align'], |
||
87 | $image['width'], |
||
88 | $image['height'], |
||
89 | $image['cache'] |
||
90 | ); |
||
91 | break; |
||
92 | case 'resolveRSS': |
||
93 | $attrs = json_decode($INPUT->str('attrs'), true); |
||
94 | $responseData[$action] = RSSNode::renderAttrsToHTML($attrs); |
||
95 | break; |
||
96 | default: |
||
97 | dokuwiki\Logger::getInstance(dokuwiki\Logger::LOG_DEBUG)->log( |
||
98 | __FILE__ . ': ' . __LINE__, |
||
99 | 'Unknown action: ' . $action |
||
100 | ); |
||
101 | http_status(400, 'unknown action'); |
||
102 | return; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | header('Content-Type: application/json'); |
||
107 | echo json_encode($responseData); |
||
108 | } |
||
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: