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) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param HTMLPurifier_Config $config |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public function cleanup($config) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Generates the file path to the serial file corresponding to |
||
| 150 | * the configuration and definition name |
||
| 151 | * @param HTMLPurifier_Config $config |
||
| 152 | * @return string |
||
| 153 | * @todo Make protected |
||
| 154 | */ |
||
| 155 | public function generateFilePath($config) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Generates the path to the directory contain this cache's serial files |
||
| 163 | * @param HTMLPurifier_Config $config |
||
| 164 | * @return string |
||
| 165 | * @note No trailing slash |
||
| 166 | * @todo Make protected |
||
| 167 | */ |
||
| 168 | public function generateDirectoryPath($config) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Generates path to base directory that contains all definition type |
||
| 176 | * serials |
||
| 177 | * @param HTMLPurifier_Config $config |
||
| 178 | * @return mixed|string |
||
| 179 | * @todo Make protected |
||
| 180 | */ |
||
| 181 | public function generateBaseDirectoryPath($config) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Convenience wrapper function for file_put_contents |
||
| 190 | * @param string $file File name to write to |
||
| 191 | * @param string $data Data to write into file |
||
| 192 | * @param HTMLPurifier_Config $config |
||
| 193 | * @return int|bool Number of bytes written if success, or false if failure. |
||
| 194 | */ |
||
| 195 | private function _write($file, $data, $config) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Prepares the directory that this type stores the serials in |
||
| 213 | * @param HTMLPurifier_Config $config |
||
| 214 | * @return bool True if successful |
||
| 215 | */ |
||
| 216 | private function _prepareDir($config) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Tests permissions on a directory and throws out friendly |
||
| 261 | * error messages and attempts to chmod it itself if possible |
||
| 262 | * @param string $dir Directory path |
||
| 263 | * @param int $chmod Permissions |
||
| 264 | * @return bool True if directory is writable |
||
| 265 | */ |
||
| 266 | private function _testPermissions($dir, $chmod) |
||
| 311 | } |
||
| 312 | |||
| 314 |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.