| Conditions | 22 |
| Paths | 720 |
| Total Lines | 106 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 38 | public static function buildSettingsForm(OnlyofficeAppsettings $settingsManager) |
||
| 39 | { |
||
| 40 | $plugin = $settingsManager->plugin; |
||
| 41 | |||
| 42 | $plugin_info = $plugin->get_info(); |
||
| 43 | $form = $plugin_info['settings_form']; |
||
| 44 | |||
| 45 | $demoData = $settingsManager->getDemoData(); |
||
| 46 | $demoAvailable = is_array($demoData) && array_key_exists('available', $demoData) |
||
| 47 | ? (bool) $demoData['available'] |
||
| 48 | : false; |
||
| 49 | |||
| 50 | $connectDemoCheckbox = $form->createElement( |
||
| 51 | 'checkbox', |
||
| 52 | 'connect_demo', |
||
| 53 | '', |
||
| 54 | $plugin->get_lang('connect_demo') ?: 'Connect to demo' |
||
| 55 | ); |
||
| 56 | |||
| 57 | if (!$demoAvailable) { |
||
| 58 | $message = $plugin->get_lang('demoPeriodIsOver') ?: 'Demo trial period is over.'; |
||
| 59 | $connectDemoCheckbox->setAttribute('disabled', 'disabled'); |
||
| 60 | } else { |
||
| 61 | if ($settingsManager->useDemo()) { |
||
| 62 | $message = $plugin->get_lang('demoUsingMessage') ?: 'Demo is currently active.'; |
||
| 63 | $connectDemoCheckbox->setChecked(true); |
||
| 64 | } else { |
||
| 65 | $message = $plugin->get_lang('demoPrevMessage') ?: 'You can enable a 30-day demo.'; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | $demoServerMessageHtml = Display::return_message($message, 'info'); |
||
| 69 | $demoServerMessage = $form->createElement('html', $demoServerMessageHtml); |
||
| 70 | |||
| 71 | $bannerTemplate = self::buildTemplate('get_docs_cloud_banner', [ |
||
| 72 | 'docs_cloud_link' => $settingsManager->getLinkToDocs(), |
||
| 73 | 'banner_title' => $plugin->get_lang('DocsCloudBannerTitle') ?: 'ONLYOFFICE Docs Cloud', |
||
| 74 | 'banner_main_text' => $plugin->get_lang('DocsCloudBannerMain') ?: 'Try Docs Cloud for production use.', |
||
| 75 | 'banner_button_text' => $plugin->get_lang('DocsCloudBannerButton') ?: 'Learn more', |
||
| 76 | ]); |
||
| 77 | $banner = $form->createElement('html', $bannerTemplate); |
||
| 78 | |||
| 79 | $testBtn = $form->createElement('submit', 'test_connection', $plugin->get_lang('TestConnection') ?: 'Test connection'); |
||
| 80 | |||
| 81 | $docUrl = $settingsManager->getDocumentServerUrl(); |
||
| 82 | $docAvailable = $settingsManager->useDemo() || !empty($docUrl); |
||
| 83 | if ($docAvailable) { |
||
| 84 | $healthUrl = $settingsManager->getDocumentServerHealthcheckUrl(); |
||
| 85 | $apiUrl = $settingsManager->getDocumentServerApiUrl(); |
||
| 86 | $preloaderUrl = $settingsManager->getDocumentServerPreloaderUrl(); |
||
| 87 | |||
| 88 | $quickLinksHtml = ' |
||
| 89 | <div class="panel panel-default" style="margin-top:8px;"> |
||
| 90 | <div class="panel-heading"><strong>Quick checks</strong></div> |
||
| 91 | <div class="panel-body"> |
||
| 92 | <ul style="margin-bottom:8px;"> |
||
| 93 | <li><a href="'.Security::remove_XSS($healthUrl).'" target="_blank" rel="noopener">Healthcheck</a></li> |
||
| 94 | <li><a href="'.Security::remove_XSS($apiUrl).'" target="_blank" rel="noopener">API (api.js)</a></li> |
||
| 95 | <li><a href="'.Security::remove_XSS($preloaderUrl).'" target="_blank" rel="noopener">Preloader</a></li> |
||
| 96 | </ul> |
||
| 97 | <p style="margin:0;"> |
||
| 98 | <a href="#" onclick="var b=document.querySelector(\'button[name=test_connection],input[name=test_connection]\'); if(b){b.click();} return false;"> |
||
| 99 | Run server-side test now |
||
| 100 | </a> |
||
| 101 | </p> |
||
| 102 | </div> |
||
| 103 | </div>'; |
||
| 104 | } else { |
||
| 105 | $quickLinksHtml = ' |
||
| 106 | <div class="panel panel-default" style="margin-top:8px;"> |
||
| 107 | <div class="panel-heading"><strong>Quick checks</strong></div> |
||
| 108 | <div class="panel-body"> |
||
| 109 | <p>No Document Server URL configured and demo is disabled. Enable <em>Connect to demo</em> or set a server URL.</p> |
||
| 110 | </div> |
||
| 111 | </div>'; |
||
| 112 | } |
||
| 113 | $quickLinksBlock = $form->createElement('html', $quickLinksHtml); |
||
| 114 | |||
| 115 | $anchorNames = ['submit_button', 'submit', 'save', 'save_settings']; |
||
| 116 | $anchor = null; |
||
| 117 | foreach ($anchorNames as $name) { |
||
| 118 | if (method_exists($form, 'getElement') && $form->getElement($name)) { |
||
| 119 | $anchor = $name; |
||
| 120 | break; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($anchor && method_exists($form, 'insertElementBefore')) { |
||
| 125 | $form->insertElementBefore($banner, $anchor); |
||
| 126 | $form->insertElementBefore($demoServerMessage, $anchor); |
||
| 127 | $form->insertElementBefore($connectDemoCheckbox, $anchor); |
||
| 128 | $form->insertElementBefore($testBtn, $anchor); |
||
| 129 | $form->insertElementBefore($quickLinksBlock, $anchor); |
||
| 130 | } else { |
||
| 131 | if (method_exists($form, 'addElement')) { |
||
| 132 | $form->addElement('html', $banner->toHtml()); |
||
| 133 | $form->addElement('html', $demoServerMessage->toHtml()); |
||
| 134 | $form->addElement($connectDemoCheckbox); |
||
| 135 | $form->addElement($testBtn); |
||
| 136 | $form->addElement('html', $quickLinksBlock->toHtml()); |
||
| 137 | } |
||
| 138 | if (function_exists('error_log')) { |
||
| 139 | error_log('[OnlyOffice] settings_form: submit anchor not found; appended elements at end'); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | return $form; |
||
| 144 | } |
||
| 305 |