Complex classes like HtmlEditorConfig 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 HtmlEditorConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class HtmlEditorConfig { |
||
| 15 | |||
| 16 | private static $configs = array(); |
||
| 17 | |||
| 18 | private static $current = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Get the HtmlEditorConfig object for the given identifier. This is a correct way to get an HtmlEditorConfig |
||
| 22 | * instance - do not call 'new' |
||
| 23 | * |
||
| 24 | * @param $identifier string - the identifier for the config set |
||
| 25 | * @return HtmlEditorConfig - the configuration object. This will be created if it does not yet exist for that |
||
| 26 | * identifier |
||
| 27 | */ |
||
| 28 | public static function get($identifier = 'default') { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Set the currently active configuration object |
||
| 35 | * @param $identifier string - the identifier for the config set |
||
| 36 | * @return null |
||
| 37 | */ |
||
| 38 | public static function set_active($identifier = null) { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get the currently active configuration identifier |
||
| 44 | * @return String - the active configuration identifier |
||
| 45 | */ |
||
| 46 | public static function get_active_identifier() { |
||
| 50 | /** |
||
| 51 | * Get the currently active configuration object |
||
| 52 | * @return HtmlEditorConfig - the active configuration object |
||
| 53 | */ |
||
| 54 | public static function get_active() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Get the available configurations as a map of friendly_name to |
||
| 61 | * configuration name. |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | public static function get_available_configs_map() { |
||
| 65 | $configs = array(); |
||
| 66 | |||
| 67 | foreach(self::$configs as $identifier => $config) { |
||
| 68 | $configs[$identifier] = $config->getOption('friendly_name'); |
||
| 69 | } |
||
| 70 | |||
| 71 | return $configs; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Holder for all TinyMCE settings _except_ plugins and buttons |
||
| 76 | */ |
||
| 77 | protected $settings = array( |
||
| 78 | 'friendly_name' => '(Please set a friendly name for this config)', |
||
| 79 | 'priority' => 0, |
||
| 80 | 'mode' => "none", // initialized through HtmlEditorField.js redraw() logic |
||
| 81 | 'editor_selector' => "htmleditor", |
||
| 82 | 'width' => "100%", |
||
| 83 | 'auto_resize' => false, |
||
| 84 | 'update_interval' => 5000, // Ensure update of this data every 5 seconds to the underlying textarea |
||
| 85 | 'theme' => "advanced", |
||
| 86 | |||
| 87 | 'theme_advanced_layout_manager' => "SimpleLayout", |
||
| 88 | 'theme_advanced_toolbar_location' => "top", |
||
| 89 | 'theme_advanced_toolbar_align' => "left", |
||
| 90 | 'theme_advanced_toolbar_parent' => "right", |
||
| 91 | |||
| 92 | 'blockquote_clear_tag' => "p", |
||
| 93 | 'table_inline_editing' => true, |
||
| 94 | |||
| 95 | 'safari_warning' => false, |
||
| 96 | 'relative_urls' => true, |
||
| 97 | 'verify_html' => true, |
||
| 98 | 'browser_spellcheck' => true, |
||
| 99 | ); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Holder list of enabled plugins |
||
| 103 | */ |
||
| 104 | protected $plugins = array( |
||
| 105 | 'contextmenu' => null, |
||
| 106 | 'table' => null, |
||
| 107 | 'emotions' => null, |
||
| 108 | 'paste' => null, |
||
| 109 | ); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Holder list of buttons, organised by line |
||
| 113 | */ |
||
| 114 | protected $buttons = array( |
||
| 115 | 1 => array('bold','italic','underline','strikethrough','separator', |
||
| 116 | 'justifyleft','justifycenter','justifyright','justifyfull','formatselect','separator', |
||
| 117 | 'bullist','numlist','outdent','indent','blockquote','hr','charmap'), |
||
| 118 | 2 => array('undo','redo','separator','cut','copy','paste','pastetext','pasteword','separator', |
||
| 119 | 'advcode','search','replace','selectall','visualaid','separator','tablecontrols'), |
||
| 120 | 3 => array() |
||
| 121 | ); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get the current value of an option |
||
| 125 | * @param $k string - The key of the option to get |
||
| 126 | * @return mixed - The value of the specified option |
||
| 127 | */ |
||
| 128 | public function getOption($k) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Set the value of one option |
||
| 134 | * @param $k string - The key of the option to set |
||
| 135 | * @param $v mixed - The value of the option to set |
||
| 136 | * @return HtmlEditorConfig |
||
| 137 | */ |
||
| 138 | public function setOption($k,$v) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Set multiple options |
||
| 145 | * @param $a array - The options to set, as keys and values of the array |
||
| 146 | * @return HtmlEditorConfig |
||
| 147 | */ |
||
| 148 | public function setOptions($a) { |
||
| 149 | foreach ($a as $k=>$v) { |
||
| 150 | $this->settings[$k] = $v; |
||
| 151 | } |
||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Enable one or several plugins. Will maintain unique list if already |
||
| 157 | * enabled plugin is re-passed. If passed in as a map of plugin-name to path, |
||
| 158 | * the plugin will be loaded by tinymce.PluginManager.load() instead of through tinyMCE.init(). |
||
| 159 | * Keep in mind that these externals plugins require a dash-prefix in their name. |
||
| 160 | * |
||
| 161 | * @see http://wiki.moxiecode.com/index.php/TinyMCE:API/tinymce.PluginManager/load |
||
| 162 | * |
||
| 163 | * @param String [0..] a string, or several strings, or a single array of strings - The plugins to enable |
||
| 164 | * @return HtmlEditorConfig |
||
| 165 | */ |
||
| 166 | public function enablePlugins() { |
||
| 167 | $plugins = func_get_args(); |
||
| 168 | if (is_array(current($plugins))) $plugins = current($plugins); |
||
| 169 | foreach ($plugins as $plugin => $path) { |
||
| 170 | // if plugins are passed without a path |
||
| 171 | if(is_numeric($plugin)) { |
||
| 172 | $plugin = $path; |
||
| 173 | $path = null; |
||
| 174 | } |
||
| 175 | if (!array_key_exists($plugin, $this->plugins)) $this->plugins[$plugin] = $path; |
||
| 176 | } |
||
| 177 | return $this; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Enable one or several plugins. Will properly handle being passed a plugin that is already disabled |
||
| 182 | * @param String [0..] a string, or several strings, or a single array of strings - The plugins to disable |
||
| 183 | * @return HtmlEditorConfig |
||
| 184 | */ |
||
| 185 | public function disablePlugins() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return Array |
||
| 199 | */ |
||
| 200 | public function getPlugins() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Totally re-set the buttons on a given line |
||
| 206 | * |
||
| 207 | * @param integer from 1..3 - The line number to redefine |
||
| 208 | * @param string a string or several strings, or a single array of strings - The button names to make this line |
||
| 209 | * contain |
||
| 210 | * @return HtmlEditorConfig |
||
| 211 | */ |
||
| 212 | public function setButtonsForLine() { |
||
| 213 | if (func_num_args() == 2) { |
||
| 214 | list($line, $buttons) = func_get_args(); |
||
| 215 | } |
||
| 216 | else { |
||
| 217 | $buttons = func_get_args(); |
||
| 218 | $line = array_shift($buttons); |
||
| 219 | } |
||
| 220 | $this->buttons[$line] = is_array($buttons) ? $buttons : array($buttons); |
||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Add buttons to the end of a line |
||
| 226 | * @param integer from 1..3 |
||
| 227 | * @param string a string, or several strings, or a single array of strings - The button names to add to the end |
||
| 228 | * of this line |
||
| 229 | * @return HtmlEditorConfig |
||
| 230 | */ |
||
| 231 | public function addButtonsToLine() { |
||
| 232 | $inserts = func_get_args(); |
||
| 233 | $line = array_shift($inserts); |
||
| 234 | if (is_array($inserts[0])) $inserts = $inserts[0]; |
||
| 235 | |||
| 236 | foreach ($inserts as $button) { |
||
| 237 | $this->buttons[$line][] = $button; |
||
| 238 | } |
||
| 239 | return $this; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Internal function for adding and removing buttons related to another button |
||
| 244 | * @param $name string - the name of the button to modify |
||
| 245 | * @param $offset integer - the offset relative to that button to perform an array_splice at - 0 for before $name, |
||
| 246 | * 1 for after |
||
| 247 | * @param $del integer - the number of buttons to remove at the position given by index(string) + offset |
||
| 248 | * @param $add mixed - an array or single item to insert at the position given by index(string) + offset, |
||
| 249 | * or null for no insertion |
||
| 250 | * @return boolean - true if $name matched a button, false otherwise |
||
| 251 | */ |
||
| 252 | protected function modifyButtons($name, $offset, $del=0, $add=null) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Insert buttons before the first occurance of another button |
||
| 265 | * @param string - the name of the button to insert other buttons before |
||
| 266 | * @param string a string, or several strings, or a single array of strings - the button names to insert before |
||
| 267 | * that button |
||
| 268 | * @return boolean - true if insertion occured, false if it did not (because the given button name was not found) |
||
| 269 | */ |
||
| 270 | public function insertButtonsBefore() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Insert buttons after the first occurance of another button |
||
| 278 | * @param string - the name of the button to insert other buttons after |
||
| 279 | * @param string a string, or several strings, or a single array of strings - the button names to insert after |
||
| 280 | * that button |
||
| 281 | * @return boolean - true if insertion occured, false if it did not (because the given button name was not found) |
||
| 282 | */ |
||
| 283 | public function insertButtonsAfter() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Remove the first occurance of buttons |
||
| 291 | * @param string one or more strings - the name of the buttons to remove |
||
| 292 | * @return null |
||
| 293 | */ |
||
| 294 | public function removeButtons() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Generate the JavaScript that will set TinyMCE's configuration: |
||
| 303 | * - Parse all configurations into JSON objects to be used in JavaScript |
||
| 304 | * - Includes TinyMCE and configurations using the {@link Requirements} system |
||
| 305 | */ |
||
| 306 | public static function require_js() { |
||
| 379 | } |
||
| 380 |