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:
| 1 | <?php |
||
| 21 | class smf_cache extends cache_api |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var string The path to the current $cachedir directory. |
||
| 25 | */ |
||
| 26 | private $cachedir = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritDoc} |
||
| 30 | */ |
||
| 31 | public function __construct() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritDoc} |
||
| 41 | */ |
||
| 42 | View Code Duplication | public function isSupported($test = false) |
|
|
|
|||
| 43 | { |
||
| 44 | $supported = is_writable($this->cachedir); |
||
| 45 | |||
| 46 | if ($test) |
||
| 47 | return $supported; |
||
| 48 | return parent::isSupported() && $supported; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritDoc} |
||
| 53 | */ |
||
| 54 | public function getData($key, $ttl = null) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritDoc} |
||
| 84 | */ |
||
| 85 | public function putData($key, $value, $ttl = null) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritDoc} |
||
| 120 | */ |
||
| 121 | public function cleanCache($type = '') |
||
| 122 | { |
||
| 123 | $cachedir = $this->cachedir; |
||
| 124 | |||
| 125 | // No directory = no game. |
||
| 126 | if (!is_dir($cachedir)) |
||
| 127 | return; |
||
| 128 | |||
| 129 | // Remove the files in SMF's own disk cache, if any |
||
| 130 | $dh = opendir($cachedir); |
||
| 131 | while ($file = readdir($dh)) |
||
| 132 | { |
||
| 133 | if ($file != '.' && $file != '..' && $file != 'index.php' && $file != '.htaccess' && (!$type || substr($file, 0, strlen($type)) == $type)) |
||
| 134 | @unlink($cachedir . '/' . $file); |
||
| 135 | } |
||
| 136 | closedir($dh); |
||
| 137 | |||
| 138 | // Make this invalid. |
||
| 139 | $this->invalidateCache(); |
||
| 140 | |||
| 141 | return true; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritDoc} |
||
| 146 | */ |
||
| 147 | public function invalidateCache() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritDoc} |
||
| 160 | */ |
||
| 161 | View Code Duplication | public function cacheSettings(array &$config_vars) |
|
| 162 | { |
||
| 163 | global $context, $txt; |
||
| 164 | |||
| 165 | $config_vars[] = $txt['cache_smf_settings']; |
||
| 166 | $config_vars[] = array('cachedir', $txt['cachedir'], 'file', 'text', 36, 'cache_cachedir'); |
||
| 167 | |||
| 168 | if (!isset($context['settings_post_javascript'])) |
||
| 169 | $context['settings_post_javascript'] = ''; |
||
| 170 | |||
| 171 | $context['settings_post_javascript'] .= ' |
||
| 172 | $("#cache_accelerator").change(function (e) { |
||
| 173 | var cache_type = e.currentTarget.value; |
||
| 174 | $("#cachedir").prop("disabled", cache_type != "smf"); |
||
| 175 | });'; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Sets the $cachedir or uses the SMF default $cachedir.. |
||
| 180 | * |
||
| 181 | * @access public |
||
| 182 | * @param string $dir A valid path |
||
| 183 | * @return boolean If this was successful or not. |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function setCachedir($dir = null) |
|
| 186 | { |
||
| 187 | global $cachedir; |
||
| 188 | |||
| 189 | // If its invalid, use SMF's. |
||
| 190 | if (is_null($dir) || !is_writable($dir)) |
||
| 191 | $this->cachedir = $cachedir; |
||
| 192 | else |
||
| 193 | $this->cachedir = $dir; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Gets the current $cachedir. |
||
| 198 | * |
||
| 199 | * @access public |
||
| 200 | * @return string the value of $ttl. |
||
| 201 | */ |
||
| 202 | public function getCachedir() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritDoc} |
||
| 209 | */ |
||
| 210 | public function getVersion() |
||
| 216 | } |
||
| 217 | |||
| 218 | ?> |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.