Complex classes like TinyMCEConfig 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 TinyMCEConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class TinyMCEConfig extends HtmlEditorConfig { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Location of module relative to BASE_DIR. This must contain the following dirs |
||
| 10 | * - plugins |
||
| 11 | * - themes |
||
| 12 | * - skins |
||
| 13 | * |
||
| 14 | * @config |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private static $base_dir = 'framework/thirdparty/tinymce'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * TinyMCE JS settings |
||
| 21 | * |
||
| 22 | * @link https://www.tinymce.com/docs/configure/ |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $settings = array( |
||
| 27 | 'fix_list_elements' => true, // https://www.tinymce.com/docs/configure/content-filtering/#fix_list_elements |
||
| 28 | 'friendly_name' => '(Please set a friendly name for this config)', |
||
| 29 | 'priority' => 0, // used for Per-member config override |
||
| 30 | 'browser_spellcheck' => true, |
||
| 31 | 'body_class' => 'typography', |
||
| 32 | 'elementpath' => false, // https://www.tinymce.com/docs/configure/editor-appearance/#elementpath |
||
| 33 | 'relative_urls' => true, |
||
| 34 | 'remove_script_host' => true, |
||
| 35 | 'convert_urls' => false, // Prevent site-root images being rewritten to base relative |
||
| 36 | 'menubar' => false, |
||
| 37 | ); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Holder list of enabled plugins |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $plugins = array( |
||
| 45 | 'table' => null, |
||
| 46 | 'emoticons' => null, |
||
| 47 | 'paste' => null, |
||
| 48 | 'code' => null, |
||
| 49 | 'link' => null, |
||
| 50 | 'importcss' => null, |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Theme name |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $theme = 'modern'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get the theme |
||
| 62 | * |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public function getTheme() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Set the theme name |
||
| 71 | * |
||
| 72 | * @param string $theme |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | public function setTheme($theme) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Holder list of buttons, organised by line. This array is 1-based indexed array |
||
| 82 | * |
||
| 83 | * {@link https://www.tinymce.com/docs/advanced/editor-control-identifiers/#toolbarcontrols} |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $buttons = array( |
||
| 88 | 1 => array( |
||
| 89 | 'bold', 'italic', 'underline', 'removeformat', '|', |
||
| 90 | 'alignleft', 'aligncenter', 'alignright', 'alignjustify', '|', |
||
| 91 | 'bullist', 'numlist', 'outdent', 'indent', |
||
| 92 | ), |
||
| 93 | 2 => array( |
||
| 94 | 'formatselect', '|', |
||
| 95 | 'paste', 'pastetext', '|', |
||
| 96 | 'table', 'ssmedia', 'sslink', 'unlink', '|', |
||
| 97 | 'code' |
||
| 98 | ), |
||
| 99 | 3 => array() |
||
| 100 | ); |
||
| 101 | |||
| 102 | public function getOption($key) { |
||
| 108 | |||
| 109 | public function setOption($key,$value) { |
||
| 113 | |||
| 114 | public function setOptions($options) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get all settings |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | protected function getSettings() { |
||
| 129 | |||
| 130 | public function getAttributes() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Enable one or several plugins. Will maintain unique list if already |
||
| 139 | * enabled plugin is re-passed. If passed in as a map of plugin-name to path, |
||
| 140 | * the plugin will be loaded by tinymce.PluginManager.load() instead of through tinyMCE.init(). |
||
| 141 | * Keep in mind that these externals plugins require a dash-prefix in their name. |
||
| 142 | * |
||
| 143 | * @see http://wiki.moxiecode.com/index.php/TinyMCE:API/tinymce.PluginManager/load |
||
| 144 | * |
||
| 145 | * If passing in a non-associative array, the plugin name should be located in the standard tinymce |
||
| 146 | * plugins folder. |
||
| 147 | * |
||
| 148 | * If passing in an associative array, the key of each item should be the plugin name. |
||
| 149 | * The value of each item is one of: |
||
| 150 | * - null - Will be treated as a stardard plugin in the standard location |
||
| 151 | * - relative path - Will be treated as a relative url |
||
| 152 | * - absolute url - Some url to an external plugin |
||
| 153 | * |
||
| 154 | * @param string $plugin,... a string, or several strings, or a single array of strings - The plugins to enable |
||
|
|
|||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function enablePlugins($plugin) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Enable one or several plugins. Will properly handle being passed a plugin that is already disabled |
||
| 177 | * @param string $plugin,... a string, or several strings, or a single array of strings - The plugins to enable |
||
| 178 | * @return $this |
||
| 179 | */ |
||
| 180 | public function disablePlugins($plugin) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Gets the list of all enabled plugins as an associative array. |
||
| 193 | * Array keys are the plugin names, and values are potentially the plugin location |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public function getPlugins() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get all button rows, skipping empty rows |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public function getButtons() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Totally re-set the buttons on a given line |
||
| 212 | * |
||
| 213 | * @param int $line The line number to redefine, from 1 to 3 |
||
| 214 | * @param string $buttons,... A string or several strings, or a single array of strings. |
||
| 215 | * The button names to assign to this line. |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | public function setButtonsForLine($line, $buttons) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Add buttons to the end of a line |
||
| 229 | * @param int $line The line number to redefine, from 1 to 3 |
||
| 230 | * @param string $buttons,... A string or several strings, or a single array of strings. |
||
| 231 | * The button names to add to this line |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | public function addButtonsToLine($line, $buttons) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Internal function for adding and removing buttons related to another button |
||
| 250 | * @param string $name The name of the button to modify |
||
| 251 | * @param int $offset The offset relative to that button to perform an array_splice at. |
||
| 252 | * 0 for before $name, 1 for after. |
||
| 253 | * @param int $del The number of buttons to remove at the position given by index(string) + offset |
||
| 254 | * @param mixed $add An array or single item to insert at the position given by index(string) + offset, |
||
| 255 | * or null for no insertion |
||
| 256 | * @return bool True if $name matched a button, false otherwise |
||
| 257 | */ |
||
| 258 | protected function modifyButtons($name, $offset, $del=0, $add=null) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Insert buttons before the first occurance of another button |
||
| 274 | * @param string $before the name of the button to insert other buttons before |
||
| 275 | * @param string $buttons,... a string, or several strings, or a single array of strings. |
||
| 276 | * The button names to insert before that button |
||
| 277 | * @return bool True if insertion occured, false if it did not (because the given button name was not found) |
||
| 278 | */ |
||
| 279 | public function insertButtonsBefore($before, $buttons) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Insert buttons after the first occurance of another button |
||
| 292 | * @param string $after the name of the button to insert other buttons before |
||
| 293 | * @param string $buttons,... a string, or several strings, or a single array of strings. |
||
| 294 | * The button names to insert after that button |
||
| 295 | * @return bool True if insertion occured, false if it did not (because the given button name was not found) |
||
| 296 | */ |
||
| 297 | public function insertButtonsAfter($after, $buttons) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Remove the first occurance of buttons |
||
| 310 | * @param string $buttons,... one or more strings - the name of the buttons to remove |
||
| 311 | * @return null |
||
| 312 | */ |
||
| 313 | public function removeButtons($buttons) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Generate the JavaScript that will set TinyMCE's configuration: |
||
| 327 | * - Parse all configurations into JSON objects to be used in JavaScript |
||
| 328 | * - Includes TinyMCE and configurations using the {@link Requirements} system |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | protected function getConfig() { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get location of all editor.css files |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | protected function getEditorCSS() { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Generate gzipped TinyMCE configuration including plugins and languages. |
||
| 417 | * This ends up "pre-loading" TinyMCE bundled with the required plugins |
||
| 418 | * so that multiple HTTP requests on the client don't need to be made. |
||
| 419 | */ |
||
| 420 | public function requireJS() { |
||
| 445 | |||
| 446 | public function init() { |
||
| 459 | } |
||
| 460 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.