Complex classes like FileStorage 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 FileStorage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class FileStorage extends AbstractStorage { |
||
| 11 | var $folder; |
||
| 12 | protected $_multi_folder = false; |
||
| 13 | const ERROR_CODE = 100; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param KeyValueSettings|object $settings |
||
| 17 | * |
||
| 18 | * @throws KeyValueException |
||
| 19 | */ |
||
| 20 | 155 | function __construct($settings) { |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @param string $key Название ключа |
||
| 48 | * @param mixed $value Новое значение |
||
| 49 | * @param double|integer $ttl Кол-во секунд, после которых значение будет считаться просроченным |
||
| 50 | * |
||
| 51 | * @throws KeyValueException |
||
| 52 | */ |
||
| 53 | 60 | function set_value($key, $value, $ttl = 315576000) { |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Создаём папку |
||
| 78 | * |
||
| 79 | * @param string $folder |
||
| 80 | * @param boolean $multi_folder |
||
| 81 | * @param string $key |
||
| 82 | * |
||
| 83 | * @throws KeyValueException |
||
| 84 | */ |
||
| 85 | 65 | protected static function create_path($folder, $multi_folder, $key) { |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $key |
||
| 114 | * |
||
| 115 | * @return object|null |
||
| 116 | */ |
||
| 117 | 65 | protected function get_value_full_clear($key) { |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param string $key |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 145 | function get_filename($key) { |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $key |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | 65 | function get_folder($key) { |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Key for NokitaKaze\Mutex\MutexInterface |
||
| 162 | * |
||
| 163 | * @param string $key |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | 145 | function get_mutex_key_name($key) { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Создаём мьютекс, соответствующий ключу и кладём его в _locks |
||
| 174 | * |
||
| 175 | * @param string $key |
||
| 176 | */ |
||
| 177 | 65 | protected function create_lock($key) { |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $key |
||
| 188 | */ |
||
| 189 | 30 | function delete_value($key) { |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Санация части regexp для добавления напрямую в regexp |
||
| 205 | * |
||
| 206 | * @param string $text Часть, которую над санировать |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | * @codeCoverageIgnore |
||
| 210 | */ |
||
| 211 | protected static function sad_safe_reg($text) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $folder |
||
| 227 | */ |
||
| 228 | 20 | protected function delete_all_files_in_folder($folder) { |
|
| 237 | |||
| 238 | 25 | function clear() { |
|
| 272 | } |
||
| 273 | |||
| 274 | ?> |
If you suppress an error, we recommend checking for the error condition explicitly: