Complex classes like THtmlArea4 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 THtmlArea4, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class THtmlArea4 extends TTextBox |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @var array list of locale => language file pairs. |
||
| 64 | */ |
||
| 65 | private static $_langs = array( |
||
| 66 | 'ar' => 'ar', |
||
| 67 | 'bg_BG' => 'bg_BG', |
||
| 68 | 'bs' => 'bs', |
||
| 69 | 'ca' => 'ca', |
||
| 70 | 'cs' => 'cs', |
||
| 71 | 'cy' => 'cy', |
||
| 72 | 'da' => 'da', |
||
| 73 | 'de' => 'de', |
||
| 74 | 'de_AT' => 'de_AT', |
||
| 75 | 'el' => 'el', |
||
| 76 | 'es' => 'es', |
||
| 77 | 'et' => 'et', |
||
| 78 | 'eu' => 'eu', |
||
| 79 | 'fa' => 'fa', |
||
| 80 | 'fi' => 'fi', |
||
| 81 | 'fo' => 'fo', |
||
| 82 | 'fr_FR' => 'fr_FR', |
||
| 83 | 'gl' => 'gl', |
||
| 84 | 'he_IL' => 'he_IL', |
||
| 85 | 'hr' => 'hr', |
||
| 86 | 'hu_HU' => 'hu_HU', |
||
| 87 | 'id' => 'id', |
||
| 88 | 'it' => 'it', |
||
| 89 | 'ja' => 'ja', |
||
| 90 | 'ka_GE' => 'ka_GE', |
||
| 91 | 'ko_KR' => 'ko_KR', |
||
| 92 | 'lb' => 'lb', |
||
| 93 | 'lt' => 'lt', |
||
| 94 | 'lv' => 'lv', |
||
| 95 | 'nb_NO' => 'nb_NO', |
||
| 96 | 'nl' => 'nl', |
||
| 97 | 'pl' => 'pl', |
||
| 98 | 'pt_BR' => 'pt_BR', |
||
| 99 | 'pt_PT' => 'pt_PT', |
||
| 100 | 'ro' => 'ro', |
||
| 101 | 'ru' => 'ru', |
||
| 102 | 'si_LK' => 'si_LK', |
||
| 103 | 'sk' => 'sk', |
||
| 104 | 'sl_SI' => 'sl_SI', |
||
| 105 | 'sr' => 'sr', |
||
| 106 | 'sv_SE' => 'sv_SE', |
||
| 107 | 'ta' => 'ta', |
||
| 108 | 'ta_IN' => 'ta_IN', |
||
| 109 | 'th_TH' => 'th_TH', |
||
| 110 | 'tr_TR' => 'tr_TR', |
||
| 111 | 'ug' => 'ug', |
||
| 112 | 'uk' => 'uk', |
||
| 113 | 'uk_UA' => 'uk_UA', |
||
| 114 | 'vi' => 'vi', |
||
| 115 | 'vi_VN' => 'vi_VN', |
||
| 116 | 'zh_CN' => 'zh_CN', |
||
| 117 | 'zh_TW' => 'zh_TW', |
||
| 118 | ); |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var array list of default plugins to load, override using getAvailablePlugins(); |
||
| 122 | */ |
||
| 123 | private static $_plugins = array( |
||
| 124 | 'advlist', |
||
| 125 | 'anchor', |
||
| 126 | 'autolink', |
||
| 127 | 'autoresize', |
||
| 128 | 'autosave', |
||
| 129 | 'bbcode', |
||
| 130 | 'charmap', |
||
| 131 | 'code', |
||
| 132 | 'contextmenu', |
||
| 133 | 'directionality', |
||
| 134 | 'emoticons', |
||
| 135 | 'fullpage', |
||
| 136 | 'fullscreen', |
||
| 137 | 'hr', |
||
| 138 | 'image', |
||
| 139 | 'importcss', |
||
| 140 | 'insertdatetime', |
||
| 141 | 'layer', |
||
| 142 | 'legacyoutput', |
||
| 143 | 'link', |
||
| 144 | 'lists', |
||
| 145 | 'media', |
||
| 146 | 'nonbreaking', |
||
| 147 | 'noneditable', |
||
| 148 | 'pagebreak', |
||
| 149 | 'paste', |
||
| 150 | 'preview', |
||
| 151 | 'print', |
||
| 152 | 'save', |
||
| 153 | 'searchreplace', |
||
| 154 | 'spellchecker', |
||
| 155 | 'tabfocus', |
||
| 156 | 'table', |
||
| 157 | 'template', |
||
| 158 | 'textcolor', |
||
| 159 | 'visualblocks', |
||
| 160 | 'visualchars', |
||
| 161 | 'wordcount', |
||
| 162 | ); |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var array default themes to load |
||
| 166 | */ |
||
| 167 | private static $_themes = array( |
||
| 168 | 'modern', |
||
| 169 | ); |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Constructor. |
||
| 173 | * Sets default width and height. |
||
| 174 | */ |
||
| 175 | public function __construct() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Overrides the parent implementation. |
||
| 183 | * TextMode for THtmlArea control is always 'MultiLine' |
||
| 184 | * @return string the behavior mode of the THtmlArea component. |
||
| 185 | */ |
||
| 186 | public function getTextMode() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Overrides the parent implementation. |
||
| 193 | * TextMode for THtmlArea is always 'MultiLine' and cannot be changed to others. |
||
| 194 | * @param string the text mode |
||
| 195 | */ |
||
| 196 | public function setTextMode($value) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return boolean whether change of the content should cause postback. Return false if EnableVisualEdit is true. |
||
| 203 | */ |
||
| 204 | public function getAutoPostBack() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return boolean whether to show WYSIWYG text editor. Defaults to true. |
||
| 211 | */ |
||
| 212 | public function getEnableVisualEdit() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Sets whether to show WYSIWYG text editor. |
||
| 219 | * @param boolean whether to show WYSIWYG text editor |
||
| 220 | */ |
||
| 221 | public function setEnableVisualEdit($value) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Gets the current culture. |
||
| 228 | * @return string current culture, e.g. de_AT. |
||
| 229 | */ |
||
| 230 | public function getCulture() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Sets the culture/language for the html area |
||
| 237 | * @param string a culture string, e.g. de_AT. |
||
| 238 | */ |
||
| 239 | public function setCulture($value) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Gets the list of options for the WYSIWYG (TinyMCE) editor |
||
| 246 | * @see http://www.tinymce.com/wiki.php/Configuration |
||
| 247 | * @return string options |
||
| 248 | */ |
||
| 249 | public function getOptions() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Sets the list of options for the WYSIWYG (TinyMCE) editor |
||
| 256 | * @see http://www.tinymce.com/wiki.php/Configuration |
||
| 257 | * @param string options |
||
| 258 | */ |
||
| 259 | public function setOptions($value) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string path to custom plugins to be copied. |
||
| 266 | */ |
||
| 267 | public function setCustomPluginPath($value) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return string path to custom plugins to be copied. |
||
| 274 | */ |
||
| 275 | public function getCustomPluginPath() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return boolean enable compression of the javascript files, default is true. |
||
| 282 | * @deprecated since 3.2.3: tinyMCE 4 doesn't support this anymore |
||
| 283 | */ |
||
| 284 | public function getEnableCompression() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param boolean enable compression of the javascript files, default is true. |
||
| 291 | * @deprecated since 3.2.3: tinyMCE 4 doesn't support this anymore |
||
| 292 | */ |
||
| 293 | public function setEnableCompression($value) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Adds attribute name-value pairs to renderer. |
||
| 300 | * This method overrides the parent implementation by registering |
||
| 301 | * additional javacript code. |
||
| 302 | * @param THtmlWriter the writer used for the rendering purpose |
||
| 303 | */ |
||
| 304 | protected function addAttributesToRender($writer) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Returns a list of plugins to be loaded. |
||
| 317 | * Override this method to customize. |
||
| 318 | * @return array list of plugins to be loaded |
||
| 319 | */ |
||
| 320 | public function getAvailablePlugins() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return array list of available themese |
||
| 327 | */ |
||
| 328 | public function getAvailableThemes() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @deprecated since 3.2.3. tinyMCE4 doesn's use this anymore |
||
| 335 | */ |
||
| 336 | protected function getCompressionOptions() |
||
| 340 | |||
| 341 | protected function loadJavascriptLibrary() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Registers the editor javascript file and code to initialize the editor. |
||
| 350 | */ |
||
| 351 | protected function registerEditorClientScript($writer) |
||
| 363 | |||
| 364 | protected function copyCustomPlugins() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Default editor options gives basic tool bar only. |
||
| 380 | * @return array editor initialization options. |
||
| 381 | */ |
||
| 382 | protected function getEditorOptions() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Parse additional options set in the Options property. |
||
| 409 | * @return array additional custom options |
||
| 410 | */ |
||
| 411 | protected function parseEditorOptions($string) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @return string localized editor interface language extension. |
||
| 434 | */ |
||
| 435 | protected function getLanguageSuffix($culture) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Gets the name of the javascript class responsible for performing postback for this control. |
||
| 455 | * This method overrides the parent implementation. |
||
| 456 | * @return string the javascript class name |
||
| 457 | */ |
||
| 458 | protected function getClientClassName() |
||
| 462 | } |
||
| 463 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.