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 HTMLPurifier_DefinitionCache_Serializer 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 HTMLPurifier_DefinitionCache_Serializer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class HTMLPurifier_DefinitionCache_Serializer extends HTMLPurifier_DefinitionCache |
||
| 4 | { |
||
| 5 | |||
| 6 | /** |
||
| 7 | * @param HTMLPurifier_Definition $def |
||
| 8 | * @param HTMLPurifier_Config $config |
||
| 9 | * @return int|bool |
||
| 10 | */ |
||
| 11 | View Code Duplication | public function add($def, $config) |
|
| 25 | |||
| 26 | /** |
||
| 27 | * @param HTMLPurifier_Definition $def |
||
| 28 | * @param HTMLPurifier_Config $config |
||
| 29 | * @return int|bool |
||
| 30 | */ |
||
| 31 | public function set($def, $config) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param HTMLPurifier_Definition $def |
||
| 45 | * @param HTMLPurifier_Config $config |
||
| 46 | * @return int|bool |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function replace($def, $config) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param HTMLPurifier_Config $config |
||
| 65 | * @return bool|HTMLPurifier_Config |
||
| 66 | */ |
||
| 67 | public function get($config) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param HTMLPurifier_Config $config |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public function remove($config) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param HTMLPurifier_Config $config |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | public function flush($config) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param HTMLPurifier_Config $config |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function cleanup($config) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Generates the file path to the serial file corresponding to |
||
| 152 | * the configuration and definition name |
||
| 153 | * @param HTMLPurifier_Config $config |
||
| 154 | * @return string |
||
| 155 | * @todo Make protected |
||
| 156 | */ |
||
| 157 | public function generateFilePath($config) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Generates the path to the directory contain this cache's serial files |
||
| 165 | * @param HTMLPurifier_Config $config |
||
| 166 | * @return string |
||
| 167 | * @note No trailing slash |
||
| 168 | * @todo Make protected |
||
| 169 | */ |
||
| 170 | public function generateDirectoryPath($config) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Generates path to base directory that contains all definition type |
||
| 178 | * serials |
||
| 179 | * @param HTMLPurifier_Config $config |
||
| 180 | * @return mixed|string |
||
| 181 | * @todo Make protected |
||
| 182 | */ |
||
| 183 | public function generateBaseDirectoryPath($config) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Convenience wrapper function for file_put_contents |
||
| 192 | * @param string $file File name to write to |
||
| 193 | * @param string $data Data to write into file |
||
| 194 | * @param HTMLPurifier_Config $config |
||
| 195 | * @return int|bool Number of bytes written if success, or false if failure. |
||
| 196 | */ |
||
| 197 | private function _write($file, $data, $config) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Prepares the directory that this type stores the serials in |
||
| 212 | * @param HTMLPurifier_Config $config |
||
| 213 | * @return bool True if successful |
||
| 214 | */ |
||
| 215 | private function _prepareDir($config) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Tests permissions on a directory and throws out friendly |
||
| 259 | * error messages and attempts to chmod it itself if possible |
||
| 260 | * @param string $dir Directory path |
||
| 261 | * @param int $chmod Permissions |
||
| 262 | * @return bool True if directory is writable |
||
| 263 | */ |
||
| 264 | private function _testPermissions($dir, $chmod) |
||
| 309 | } |
||
| 310 | |||
| 312 |