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 TinyMCE 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 TinyMCE, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class TinyMCE |
||
| 27 | { |
||
| 28 | var $rootpath; |
||
|
|
|||
| 29 | var $config = array(); |
||
| 30 | var $setting = array(); |
||
| 31 | var $element = ''; |
||
| 32 | |||
| 33 | // PHP 5 Constructor |
||
| 34 | function __construct($config) |
||
| 35 | { |
||
| 36 | $this->setConfig($config); |
||
| 37 | $this->rootpath = $this->config["rootpath"] . "/tiny_mce"; |
||
| 38 | $this->xoopsPlugins = $this->get_xoopsPlugins(); |
||
| 39 | $this->element = $config['elements']; |
||
| 40 | } |
||
| 41 | |||
| 42 | // PHP 4 Contructor |
||
| 43 | function TinyMCE($config) |
||
| 46 | } |
||
| 47 | |||
| 48 | function &instance( $config ) |
||
| 49 | { |
||
| 50 | static $instance; |
||
| 51 | if (!isset($instance)) { |
||
| 52 | $instance = new TinyMCE($config); |
||
| 53 | } else { |
||
| 54 | $instance->setConfig($config); |
||
| 55 | } |
||
| 56 | return $instance; |
||
| 57 | } |
||
| 58 | |||
| 59 | function setConfig( $config ) |
||
| 60 | { |
||
| 61 | foreach ($config as $key => $val) { |
||
| 62 | $this->config[$key] = $val; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | function init() |
||
| 67 | { |
||
| 68 | $xoops = Xoops::getInstance(); |
||
| 69 | // list of configured options |
||
| 70 | $configured = array(); |
||
| 71 | |||
| 72 | $xoops_root_path = \XoopsBaseConfig::get('root-path'); |
||
| 73 | |||
| 74 | // Load default settings |
||
| 75 | if ( ! ($this->setting = @include( $xoops->path( "var/configs/tinymce.php" ) ) ) ) { |
||
| 76 | $this->setting = include __DIR__ . "/settings.php"; |
||
| 77 | } |
||
| 78 | |||
| 79 | // get editor language (from ...) |
||
| 80 | if (is_readable($xoops_root_path . $this->rootpath . '/langs/' . $this->config["language"] . '.js')) { |
||
| 81 | $this->setting["language"] = $this->config["language"]; |
||
| 82 | $configured[] = "language"; |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->setting["content_css"] = implode( ",", $this->loadCss()) . ',' . $xoops->url('xoops.css'); |
||
| 86 | $configured[] = "content_css"; |
||
| 87 | |||
| 88 | if ( !empty($this->config["theme"]) && is_dir($xoops_root_path . $this->rootpath . "/themes/" . $this->config["theme"]) ) { |
||
| 89 | $this->setting["theme"] = $this->config["theme"]; |
||
| 90 | $configured[] = "theme"; |
||
| 91 | } |
||
| 92 | |||
| 93 | if (!empty($this->config["mode"])) { |
||
| 94 | $this->setting["mode"] = $this->config["mode"]; |
||
| 95 | $configured[] = "mode"; |
||
| 96 | } |
||
| 97 | |||
| 98 | // load all plugins except the plugins in setting["exclude_plugins"] |
||
| 99 | $this->setting["plugins"] = implode(",", $this->loadPlugins()); |
||
| 100 | $configured[] = "plugins"; |
||
| 101 | |||
| 102 | if ( $this->setting["theme"] !== "simple" ) { |
||
| 103 | if (empty($this->config["buttons"])) { |
||
| 104 | $this->config["buttons"][] = array( |
||
| 105 | "before" => "", |
||
| 106 | "add" => "", |
||
| 107 | ); |
||
| 108 | $this->config["buttons"][] = array( |
||
| 109 | "before" => "", |
||
| 110 | "add" => "", |
||
| 111 | ); |
||
| 112 | $this->config["buttons"][] = array( |
||
| 113 | "before" => "", |
||
| 114 | "add" => "", |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | $i = 0; |
||
| 118 | foreach ($this->config["buttons"] as $button) { |
||
| 119 | $i++; |
||
| 120 | if (isset($button["before"])) { |
||
| 121 | $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add_before"] = $button["before"]; |
||
| 122 | } |
||
| 123 | if (isset($button["add"])) { |
||
| 124 | $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add"] = $button["add"]; |
||
| 125 | } |
||
| 126 | if (isset($button[""])) { |
||
| 127 | $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = $button[""]; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | $configured[] = "buttons"; |
||
| 131 | |||
| 132 | if (isset($this->config["toolbar_location"])) { |
||
| 133 | $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = $this->config["toolbar_location"]; |
||
| 134 | $configured[] = "toolbar_location"; |
||
| 135 | } else { |
||
| 136 | $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = "top"; |
||
| 137 | } |
||
| 138 | |||
| 139 | if (isset($this->config["toolbar_align"])) { |
||
| 140 | $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = $this->config["toolbar_align"]; |
||
| 141 | $configured[] = "toolbar_align"; |
||
| 142 | } else { |
||
| 143 | $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = "left"; |
||
| 144 | } |
||
| 145 | |||
| 146 | if (isset($this->config["statusbar_location"])) { |
||
| 147 | $this->setting["theme_" . $this->setting["theme"] . "_statusbar_location"] = $this->config["statusbar_location"]; |
||
| 148 | $configured[] = "statusbar_location"; |
||
| 149 | } |
||
| 150 | |||
| 151 | if (isset($this->config["path_location"])) { |
||
| 152 | $this->setting["theme_" . $this->setting["theme"] . "_path_location"] = $this->config["path_location"]; |
||
| 153 | $configured[] = "path_location"; |
||
| 154 | } |
||
| 155 | |||
| 156 | if (isset($this->config["resize_horizontal"])) { |
||
| 157 | $this->setting["theme_" . $this->setting["theme"] . "_resize_horizontal"] = $this->config["resize_horizontal"]; |
||
| 158 | $configured[] = "resize_horizontal"; |
||
| 159 | } |
||
| 160 | |||
| 161 | if (isset($this->config["resizing"])) { |
||
| 162 | $this->setting["theme_" . $this->setting["theme"] . "_resizing"] = $this->config["resizing"]; |
||
| 163 | $configured[] = "resizing"; |
||
| 164 | } |
||
| 165 | |||
| 166 | if (!empty($this->config["fonts"])) { |
||
| 167 | $this->setting["theme_" . $this->setting["theme"] . "_fonts"] = $this->config["fonts"]; |
||
| 168 | $configured[] = "fonts"; |
||
| 169 | } |
||
| 170 | |||
| 171 | for ($i=1 ; $i <= 4 ; $i++ ) { |
||
| 172 | $buttons = array(); |
||
| 173 | if ( isset($this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"]) ) { |
||
| 174 | $checklist = explode(",", $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] ); |
||
| 175 | foreach ( $checklist as $plugin ) { |
||
| 176 | if ( strpos( strtolower($plugin), "xoops") !== false ) { |
||
| 177 | if ( in_array( $plugin, $this->xoopsPlugins ) ) { |
||
| 178 | $buttons[] = $plugin; |
||
| 179 | } |
||
| 180 | } else { |
||
| 181 | $buttons[] = $plugin; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = implode(",", $buttons); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | $configured = array_unique($configured); |
||
| 190 | foreach ($this->config as $key => $val) { |
||
| 191 | if (isset($this->setting[$key]) || in_array($key, $configured)) { |
||
| 192 | continue; |
||
| 193 | } |
||
| 194 | $this->setting[$key] = $val; |
||
| 195 | } |
||
| 196 | |||
| 197 | if (!is_dir($xoops_root_path . $this->rootpath . "/themes/" . $this->setting["theme"] . '/docs/' . $this->setting["language"] . '/')) { |
||
| 198 | $this->setting["docs_language"] = "en"; |
||
| 199 | } |
||
| 200 | |||
| 201 | unset($this->config, $configured); |
||
| 202 | |||
| 203 | return true; |
||
| 204 | } |
||
| 205 | |||
| 206 | // load all plugins execpt the plugins in setting["exclude_plugins"] |
||
| 207 | function loadPlugins() |
||
| 208 | { |
||
| 209 | $xoops_root_path = \XoopsBaseConfig::get('root-path'); |
||
| 210 | $plugins = array(); |
||
| 211 | $plugins_list = XoopsLists::getDirListAsArray( $xoops_root_path . $this->rootpath . "/plugins" ); |
||
| 212 | if (empty($this->setting["plugins"])) { |
||
| 213 | $plugins = $plugins_list; |
||
| 214 | } else { |
||
| 215 | $plugins = array_intersect(explode(",", $this->setting["plugins"]), $plugins_list); |
||
| 216 | } |
||
| 217 | if (!empty($this->setting["exclude_plugins"])) { |
||
| 218 | $plugins = array_diff($plugins, explode(",", $this->setting["exclude_plugins"])); |
||
| 219 | } |
||
| 220 | if (!empty($this->config["plugins"])) { |
||
| 221 | $plugins = array_merge($plugins, $this->config["plugins"]); |
||
| 222 | } |
||
| 223 | return $plugins; |
||
| 224 | } |
||
| 225 | |||
| 226 | // return all xoops plugins |
||
| 227 | function get_xoopsPlugins() { |
||
| 228 | $xoops_root_path = \XoopsBaseConfig::get('root-path'); |
||
| 229 | $xoopsPlugins = array(); |
||
| 230 | $allplugins = XoopsLists::getDirListAsArray( $xoops_root_path . $this->rootpath . "/plugins" ); |
||
| 231 | foreach ( $allplugins as $plugin ) { |
||
| 232 | if ( strpos( strtolower($plugin), "xoops") !== false && file_exists($xoops_root_path . $this->config["rootpath"] . "/include/$plugin.php") ) { |
||
| 233 | if ( $right = @include $xoops_root_path . $this->config["rootpath"] . "/include/$plugin.php" ) { |
||
| 234 | $xoopsPlugins[$plugin] = $plugin; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | return $xoopsPlugins; |
||
| 239 | } |
||
| 240 | |||
| 241 | function loadCss($css_file = 'style.css') |
||
| 242 | { |
||
| 243 | static $css_url, $css_path; |
||
| 244 | |||
| 245 | if (!isset($css_url)) { |
||
| 246 | $xoops = Xoops::getInstance(); |
||
| 247 | $css_url = dirname( $xoops->getCss($xoops->getConfig('theme_set')) ); |
||
| 248 | $css_path = str_replace(\XoopsBaseConfig::get('themes-url'), \XoopsBaseConfig::get('themes-path'), $css_url); |
||
| 249 | } |
||
| 250 | |||
| 251 | $css = array(); |
||
| 252 | $css[] = $css_url . '/' . $css_file; |
||
| 253 | $css_content = file_get_contents( $css_path . '/' . $css_file ); |
||
| 254 | |||
| 255 | // get all import css files |
||
| 256 | if ( preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER) ) { |
||
| 257 | foreach( $matches[1] as $key => $css_import ) { |
||
| 258 | $css = array_merge( $css, $this->loadCss( $css_import) ); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | return $css; |
||
| 262 | } |
||
| 263 | |||
| 264 | function render() |
||
| 265 | { |
||
| 266 | static $isTinyMceJsLoaded = false; |
||
| 267 | |||
| 268 | $xoops_url = \XoopsBaseConfig::get('url'); |
||
| 269 | |||
| 270 | $this->init(); |
||
| 271 | |||
| 272 | if ( !empty($this->setting["callback"]) ) { |
||
| 273 | $callback = $this->setting["callback"]; |
||
| 274 | unset($this->setting["callback"]); |
||
| 275 | } else { |
||
| 276 | $callback = ""; |
||
| 277 | } |
||
| 278 | |||
| 279 | // create returned string - start |
||
| 280 | $ret = "\n"; |
||
| 281 | $ret .= "<!-- Start TinyMce Rendering -->\n"; //debug |
||
| 282 | if ($isTinyMceJsLoaded) { |
||
| 283 | $ret .= "<!-- 'tiny_mce.js' SCRIPT IS ALREADY LOADED -->\n"; //debug |
||
| 284 | } else { |
||
| 285 | $ret .= "<script language='javascript' type='text/javascript' src='" . $xoops_url . $this->rootpath . "/jquery.tinymce.js'></script>\n"; |
||
| 286 | $isTinyMceJsLoaded = true; |
||
| 287 | } |
||
| 288 | $ret .= "<script language='javascript' type='text/javascript'>\n"; |
||
| 289 | $ret .= " $().ready(function() {\n"; |
||
| 290 | $ret .= "$('textarea." . $this->element . "').tinymce({\n"; |
||
| 291 | // Location of TinyMCE script |
||
| 292 | $ret .= "script_url : '" . $xoops_url . $this->rootpath . "/tiny_mce.js',\n"; |
||
| 293 | |||
| 294 | // set options - start |
||
| 295 | foreach ($this->setting as $key => $val) { |
||
| 296 | $ret .= $key . ":"; |
||
| 297 | if ($val === true) { |
||
| 298 | $ret.= "true,"; |
||
| 299 | } elseif ($val === false) { |
||
| 300 | $ret .= "false,"; |
||
| 301 | } else { |
||
| 302 | $ret .= "'{$val}',"; |
||
| 303 | } |
||
| 304 | $ret .= "\n"; |
||
| 305 | } |
||
| 306 | |||
| 307 | // set options - end |
||
| 308 | $ret .= "tinymceload: true\n"; |
||
| 309 | $ret .= "});\n"; |
||
| 310 | $ret .= "});\n"; |
||
| 311 | $ret .= $callback . "\n"; |
||
| 312 | //$ret .= "function toggleEditor(id) {tinyMCE.execCommand('mceToggleEditor',false, id);}\n"; |
||
| 313 | $ret .= "</script>\n"; |
||
| 314 | $ret .= "<!-- End TinyMce Rendering -->\n";//debug |
||
| 315 | // create returned string - end |
||
| 316 | return $ret ; |
||
| 317 | } |
||
| 318 | } |
||
| 319 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.