Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | 'language' => 'en', |
||
| 38 | ); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Holder list of enabled plugins |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $plugins = array( |
||
| 46 | 'table' => null, |
||
| 47 | 'emoticons' => null, |
||
| 48 | 'paste' => null, |
||
| 49 | 'code' => null, |
||
| 50 | 'link' => null, |
||
| 51 | 'importcss' => null, |
||
| 52 | ); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Theme name |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $theme = 'modern'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get the theme |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function getTheme() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Set the theme name |
||
| 72 | * |
||
| 73 | * @param string $theme |
||
| 74 | * @return $this |
||
| 75 | */ |
||
| 76 | public function setTheme($theme) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Holder list of buttons, organised by line. This array is 1-based indexed array |
||
| 83 | * |
||
| 84 | * {@link https://www.tinymce.com/docs/advanced/editor-control-identifiers/#toolbarcontrols} |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $buttons = array( |
||
| 89 | 1 => array( |
||
| 90 | 'bold', 'italic', 'underline', 'removeformat', '|', |
||
| 91 | 'alignleft', 'aligncenter', 'alignright', 'alignjustify', '|', |
||
| 92 | 'bullist', 'numlist', 'outdent', 'indent', |
||
| 93 | ), |
||
| 94 | 2 => array( |
||
| 95 | 'formatselect', '|', |
||
| 96 | 'paste', 'pastetext', '|', |
||
| 97 | 'table', 'ssmedia', 'sslink', 'unlink', '|', |
||
| 98 | 'code' |
||
| 99 | ), |
||
| 100 | 3 => array() |
||
| 101 | ); |
||
| 102 | |||
| 103 | public function getOption($key) { |
||
| 109 | |||
| 110 | public function setOption($key,$value) { |
||
| 114 | |||
| 115 | public function setOptions($options) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get all settings |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | protected function getSettings() { |
||
| 130 | |||
| 131 | public function getAttributes() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Enable one or several plugins. Will maintain unique list if already |
||
| 140 | * enabled plugin is re-passed. If passed in as a map of plugin-name to path, |
||
| 141 | * the plugin will be loaded by tinymce.PluginManager.load() instead of through tinyMCE.init(). |
||
| 142 | * Keep in mind that these externals plugins require a dash-prefix in their name. |
||
| 143 | * |
||
| 144 | * @see http://wiki.moxiecode.com/index.php/TinyMCE:API/tinymce.PluginManager/load |
||
| 145 | * |
||
| 146 | * If passing in a non-associative array, the plugin name should be located in the standard tinymce |
||
| 147 | * plugins folder. |
||
| 148 | * |
||
| 149 | * If passing in an associative array, the key of each item should be the plugin name. |
||
| 150 | * The value of each item is one of: |
||
| 151 | * - null - Will be treated as a stardard plugin in the standard location |
||
| 152 | * - relative path - Will be treated as a relative url |
||
| 153 | * - absolute url - Some url to an external plugin |
||
| 154 | * |
||
| 155 | * @param string $plugin,... a string, or several strings, or a single array of strings - The plugins to enable |
||
|
|
|||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function enablePlugins($plugin) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Enable one or several plugins. Will properly handle being passed a plugin that is already disabled |
||
| 178 | * @param string $plugin,... a string, or several strings, or a single array of strings - The plugins to enable |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function disablePlugins($plugin) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Gets the list of all enabled plugins as an associative array. |
||
| 194 | * Array keys are the plugin names, and values are potentially the plugin location |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | public function getPlugins() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get list of plugins without custom locations, which is the set of |
||
| 204 | * plugins which can be loaded via the standard plugin path, and could |
||
| 205 | * potentially be minified |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public function getInternalPlugins() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get all button rows, skipping empty rows |
||
| 222 | * |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | public function getButtons() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Totally re-set the buttons on a given line |
||
| 231 | * |
||
| 232 | * @param int $line The line number to redefine, from 1 to 3 |
||
| 233 | * @param string $buttons,... A string or several strings, or a single array of strings. |
||
| 234 | * The button names to assign to this line. |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | public function setButtonsForLine($line, $buttons) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Add buttons to the end of a line |
||
| 248 | * @param int $line The line number to redefine, from 1 to 3 |
||
| 249 | * @param string $buttons,... A string or several strings, or a single array of strings. |
||
| 250 | * The button names to add to this line |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | public function addButtonsToLine($line, $buttons) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Internal function for adding and removing buttons related to another button |
||
| 269 | * @param string $name The name of the button to modify |
||
| 270 | * @param int $offset The offset relative to that button to perform an array_splice at. |
||
| 271 | * 0 for before $name, 1 for after. |
||
| 272 | * @param int $del The number of buttons to remove at the position given by index(string) + offset |
||
| 273 | * @param mixed $add An array or single item to insert at the position given by index(string) + offset, |
||
| 274 | * or null for no insertion |
||
| 275 | * @return bool True if $name matched a button, false otherwise |
||
| 276 | */ |
||
| 277 | protected function modifyButtons($name, $offset, $del=0, $add=null) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Insert buttons before the first occurance of another button |
||
| 293 | * @param string $before the name of the button to insert other buttons before |
||
| 294 | * @param string $buttons,... a string, or several strings, or a single array of strings. |
||
| 295 | * The button names to insert before that button |
||
| 296 | * @return bool True if insertion occured, false if it did not (because the given button name was not found) |
||
| 297 | */ |
||
| 298 | View Code Duplication | public function insertButtonsBefore($before, $buttons) { |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Insert buttons after the first occurance of another button |
||
| 311 | * @param string $after the name of the button to insert other buttons before |
||
| 312 | * @param string $buttons,... a string, or several strings, or a single array of strings. |
||
| 313 | * The button names to insert after that button |
||
| 314 | * @return bool True if insertion occured, false if it did not (because the given button name was not found) |
||
| 315 | */ |
||
| 316 | View Code Duplication | public function insertButtonsAfter($after, $buttons) { |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Remove the first occurance of buttons |
||
| 329 | * @param string $buttons,... one or more strings - the name of the buttons to remove |
||
| 330 | * @return null |
||
| 331 | */ |
||
| 332 | View Code Duplication | public function removeButtons($buttons) { |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Generate the JavaScript that will set TinyMCE's configuration: |
||
| 346 | * - Parse all configurations into JSON objects to be used in JavaScript |
||
| 347 | * - Includes TinyMCE and configurations using the {@link Requirements} system |
||
| 348 | * |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | protected function getConfig() { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Get location of all editor.css files |
||
| 413 | * |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | protected function getEditorCSS() { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Generate gzipped TinyMCE configuration including plugins and languages. |
||
| 436 | * This ends up "pre-loading" TinyMCE bundled with the required plugins |
||
| 437 | * so that multiple HTTP requests on the client don't need to be made. |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getScriptURL() { |
||
| 459 | |||
| 460 | public function init() { |
||
| 473 | } |
||
| 474 |
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.