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 | public function add($def, $config) |
||
| 12 | { |
||
| 13 | if (!$this->checkDefType($def)) { |
||
| 14 | return null; |
||
| 15 | } |
||
| 16 | $file = $this->generateFilePath($config); |
||
| 17 | if (file_exists($file)) { |
||
| 18 | return false; |
||
| 19 | } |
||
| 20 | if (!$this->_prepareDir($config)) { |
||
| 21 | return false; |
||
| 22 | } |
||
| 23 | return $this->_write($file, serialize($def), $config); |
||
| 24 | } |
||
| 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 | public function replace($def, $config) |
||
| 49 | { |
||
| 50 | if (!$this->checkDefType($def)) { |
||
| 51 | return; |
||
| 52 | } |
||
| 53 | $file = $this->generateFilePath($config); |
||
| 54 | if (!file_exists($file)) { |
||
| 55 | return false; |
||
| 56 | } |
||
| 57 | if (!$this->_prepareDir($config)) { |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | return $this->_write($file, serialize($def), $config); |
||
| 61 | } |
||
| 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) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Tests permissions on a directory and throws out friendly |
||
| 254 | * error messages and attempts to chmod it itself if possible |
||
| 255 | * @param string $dir Directory path |
||
| 256 | * @param int $chmod Permissions |
||
| 257 | * @return bool True if directory is writable |
||
| 258 | */ |
||
| 259 | private function _testPermissions($dir, $chmod) |
||
| 304 | } |
||
| 305 | |||
| 307 |