Complex classes like admin_plugin_config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use admin_plugin_config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class admin_plugin_config extends DokuWiki_Admin_Plugin { |
||
| 20 | |||
| 21 | const METADATA = __DIR__ . 'settings/config.metadata.php'; |
||
| 22 | const IMGDIR = DOKU_BASE . 'lib/plugins/config/images/'; |
||
| 23 | |||
| 24 | protected $_localised_prompts = false; |
||
| 25 | |||
| 26 | /** @var Configuration */ |
||
| 27 | protected $configuration; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * admin_plugin_config constructor. |
||
| 31 | */ |
||
| 32 | public function __construct() { |
||
| 33 | $this->configuration = new Configuration(); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * handle user request |
||
| 38 | */ |
||
| 39 | public function handle() { |
||
| 40 | global $ID, $INPUT; |
||
| 41 | |||
| 42 | if(!$INPUT->bool('save') || !checkSecurityToken()) { |
||
| 43 | return; |
||
| 44 | } |
||
| 45 | |||
| 46 | // don't go any further if the configuration is locked |
||
| 47 | if($this->configuration->isLocked()) return; |
||
| 48 | |||
| 49 | // update settings and redirect of successful |
||
| 50 | $ok = $this->configuration->updateSettings($INPUT->arr('config')); |
||
| 51 | if($ok) { // no errors |
||
| 52 | try { |
||
| 53 | if($this->configuration->hasChanged()) { |
||
| 54 | $this->configuration->save(); |
||
| 55 | } else { |
||
| 56 | $this->configuration->touch(); |
||
| 57 | } |
||
| 58 | msg($this->getLang('updated'), -1); |
||
| 59 | } catch(Exception $e) { |
||
| 60 | msg($this->getLang('error'), -1); |
||
| 61 | } |
||
| 62 | send_redirect(wl($ID, array('do' => 'admin', 'page' => 'config'), true, '&')); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * output appropriate html |
||
| 68 | */ |
||
| 69 | public function html() { |
||
| 70 | $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. |
||
| 71 | global $lang; |
||
| 72 | global $ID; |
||
| 73 | |||
| 74 | $this->setupLocale(true); |
||
| 75 | |||
| 76 | print $this->locale_xhtml('intro'); |
||
| 77 | |||
| 78 | ptln('<div id="config__manager">'); |
||
| 79 | |||
| 80 | if($this->configuration->isLocked()) { |
||
| 81 | ptln('<div class="info">' . $this->getLang('locked') . '</div>'); |
||
| 82 | } |
||
| 83 | |||
| 84 | // POST to script() instead of wl($ID) so config manager still works if |
||
| 85 | // rewrite config is broken. Add $ID as hidden field to remember |
||
| 86 | // current ID in most cases. |
||
| 87 | ptln('<form action="' . script() . '" method="post">'); |
||
| 88 | ptln('<div class="no"><input type="hidden" name="id" value="' . $ID . '" /></div>'); |
||
| 89 | formSecurityToken(); |
||
| 90 | $this->_print_h1('dokuwiki_settings', $this->getLang('_header_dokuwiki')); |
||
| 91 | |||
| 92 | $in_fieldset = false; |
||
| 93 | $first_plugin_fieldset = true; |
||
| 94 | $first_template_fieldset = true; |
||
| 95 | foreach($this->configuration->getSettings() as $setting) { |
||
| 96 | if(is_a($setting, SettingHidden::class)) { |
||
| 97 | continue; |
||
| 98 | } else if(is_a($setting, settingFieldset::class)) { |
||
| 99 | // config setting group |
||
| 100 | if($in_fieldset) { |
||
| 101 | ptln(' </table>'); |
||
| 102 | ptln(' </div>'); |
||
| 103 | ptln(' </fieldset>'); |
||
| 104 | } else { |
||
| 105 | $in_fieldset = true; |
||
| 106 | } |
||
| 107 | // fixme this should probably be a function in setting: |
||
| 108 | if($first_plugin_fieldset && substr($setting->getKey(), 0, 10) == 'plugin' . Configuration::KEYMARKER) { |
||
| 109 | $this->_print_h1('plugin_settings', $this->getLang('_header_plugin')); |
||
| 110 | $first_plugin_fieldset = false; |
||
| 111 | } else if($first_template_fieldset && substr($setting->getKey(), 0, 7) == 'tpl' . Configuration::KEYMARKER) { |
||
| 112 | $this->_print_h1('template_settings', $this->getLang('_header_template')); |
||
| 113 | $first_template_fieldset = false; |
||
| 114 | } |
||
| 115 | ptln(' <fieldset id="' . $setting->getKey() . '">'); |
||
| 116 | ptln(' <legend>' . $setting->prompt($this) . '</legend>'); |
||
| 117 | ptln(' <div class="table">'); |
||
| 118 | ptln(' <table class="inline">'); |
||
| 119 | } else { |
||
| 120 | // config settings |
||
| 121 | list($label, $input) = $setting->html($this, $this->_error); |
||
|
|
|||
| 122 | |||
| 123 | $class = $setting->is_default() |
||
| 124 | ? ' class="default"' |
||
| 125 | : ($setting->is_protected() ? ' class="protected"' : ''); |
||
| 126 | $error = $setting->error() |
||
| 127 | ? ' class="value error"' |
||
| 128 | : ' class="value"'; |
||
| 129 | $icon = $setting->caution() |
||
| 130 | ? '<img src="' . self::IMGDIR . $setting->caution() . '.png" ' . |
||
| 131 | 'alt="' . $setting->caution() . '" title="' . $this->getLang($setting->caution()) . '" />' |
||
| 132 | : ''; |
||
| 133 | |||
| 134 | ptln(' <tr' . $class . '>'); |
||
| 135 | ptln(' <td class="label">'); |
||
| 136 | ptln(' <span class="outkey">' . $setting->_out_key(true, true) . '</span>'); |
||
| 137 | ptln(' ' . $icon . $label); |
||
| 138 | ptln(' </td>'); |
||
| 139 | ptln(' <td' . $error . '>' . $input . '</td>'); |
||
| 140 | ptln(' </tr>'); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | ptln(' </table>'); |
||
| 145 | ptln(' </div>'); |
||
| 146 | if($in_fieldset) { |
||
| 147 | ptln(' </fieldset>'); |
||
| 148 | } |
||
| 149 | |||
| 150 | // show undefined settings list |
||
| 151 | $undefined_settings = $this->configuration->getUndefined(); |
||
| 152 | if($allow_debug && !empty($undefined_settings)) { |
||
| 153 | /** |
||
| 154 | * Callback for sorting settings |
||
| 155 | * |
||
| 156 | * @param Setting $a |
||
| 157 | * @param Setting $b |
||
| 158 | * @return int if $a is lower/equal/higher than $b |
||
| 159 | */ |
||
| 160 | function _setting_natural_comparison($a, $b) { |
||
| 163 | |||
| 164 | usort($undefined_settings, '_setting_natural_comparison'); |
||
| 165 | $this->_print_h1('undefined_settings', $this->getLang('_header_undefined')); |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param bool $prompts |
||
| 214 | */ |
||
| 215 | public function setupLocale($prompts = false) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return bool |
||
| 227 | * @fixme this should be done by the loader |
||
| 228 | */ |
||
| 229 | protected function _setup_localised_plugin_prompts() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Generates a two-level table of contents for the config plugin. |
||
| 281 | * |
||
| 282 | * @author Ben Coburn <[email protected]> |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function getTOC() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param string $id |
||
| 340 | * @param string $text |
||
| 341 | */ |
||
| 342 | protected function _print_h1($id, $text) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Adds a translation to this plugin's language array |
||
| 348 | * |
||
| 349 | * @param string $key |
||
| 350 | * @param string $value |
||
| 351 | */ |
||
| 352 | public function addLang($key, $value) { |
||
| 356 | } |
||
| 357 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: