Complex classes like SnCache 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 SnCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class SnCache { |
||
| 9 | /** |
||
| 10 | * Кэш данных - юзера, планеты, юниты, очередь, альянсы итд |
||
| 11 | * |
||
| 12 | * @var array $data |
||
| 13 | */ |
||
| 14 | protected static $data = array(); |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Кэширует соответствия между расположением объектов - в частности юнитов и очередей |
||
| 18 | * |
||
| 19 | * Массив $locator - хранит отношения между записями для быстрого доступа по тип_записи:тип_локации:ид_локации:внутренний_ид_записи=>информация |
||
| 20 | * Для LOC_UNIT внутренний ИД - это SNID, а информация - это ссылка на запись `unit` |
||
| 21 | * Для LOC_QUE внутренний ИД - это тип очереди, а информация - массив ссылок на `que` |
||
| 22 | * |
||
| 23 | * @var array $locator |
||
| 24 | */ |
||
| 25 | protected static $locator = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Кэш запросов |
||
| 29 | * |
||
| 30 | * @var array $queries |
||
| 31 | */ |
||
| 32 | protected static $queries = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Информация о блокировках |
||
| 36 | * |
||
| 37 | * @var array $locks |
||
| 38 | */ |
||
| 39 | protected static $locks = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Repacking data for $location_type |
||
| 43 | * |
||
| 44 | * @param int $location_type |
||
| 45 | * @param int $record_id |
||
| 46 | */ |
||
| 47 | public static function cache_repack($location_type, $record_id = 0) { |
||
| 57 | |||
| 58 | public static function cache_clear($location_type, $hard = true) { |
||
| 68 | |||
| 69 | public static function setNull(&$item) { |
||
| 72 | |||
| 73 | public static function cache_clear_all($hard = true) { |
||
| 81 | |||
| 82 | public static function cache_isset($location_type, $record_id) { |
||
| 85 | |||
| 86 | // TODO - UNUSED ???????????? |
||
|
|
|||
| 87 | public static function cache_get($location_type, $record_id) { |
||
| 90 | |||
| 91 | /* Кэшируем запись в соответствующий кэш |
||
| 92 | |||
| 93 | Писать в кэш: |
||
| 94 | 1. Если записи не существует в кэше |
||
| 95 | 2. Если стоит $force_overwrite |
||
| 96 | 3. Если во время транзакции существующая запись не заблокирована |
||
| 97 | |||
| 98 | Блокировать запись: |
||
| 99 | 1. Если идет транзакция и запись не заблокирована |
||
| 100 | 2. Если не стоит скип-лок |
||
| 101 | */ |
||
| 102 | public static function cache_set($location_type, $record, $force_overwrite = false, $skip_lock = false) { |
||
| 123 | |||
| 124 | public static function queryCacheSetByFilter($location_type, $filter, $record_id) { |
||
| 127 | |||
| 128 | public static function cache_unset($cache_id, $safe_record_id) { |
||
| 137 | |||
| 138 | public static function cache_lock_get($location_type, $record_id) { |
||
| 141 | |||
| 142 | public static function cache_lock_set($location_type, $record_id) { |
||
| 145 | |||
| 146 | // TODO - UNUSED ???????????? |
||
| 147 | public static function cache_lock_unset($location_type, $record_id) { |
||
| 154 | |||
| 155 | public static function cache_lock_unset_all() { |
||
| 162 | |||
| 163 | public static function getData($locationType = LOC_NONE) { |
||
| 166 | |||
| 167 | public static function cacheUnsetElement($locationType, $recordId) { |
||
| 170 | |||
| 171 | public static function isArrayLocation($locationType) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Return reference to record in $data by locationType and recordId |
||
| 177 | * |
||
| 178 | * @param int $locationType |
||
| 179 | * @param int $recordId |
||
| 180 | * |
||
| 181 | * @return &mixed |
||
| 182 | */ |
||
| 183 | public static function &getDataRefByLocationAndId($locationType, $recordId) { |
||
| 186 | |||
| 187 | public static function setUnitLocator($unit, $unit_id) { |
||
| 192 | |||
| 193 | public static function getUnitLocator($location_type, $location_id, $unit_snid) { |
||
| 196 | |||
| 197 | public static function getUnitLocatorByFullLocation($location_type, $location_id) { |
||
| 200 | |||
| 201 | public static function setUnitLocatorByLocationAndIDs($location_type, $location_id, $unit_data) { |
||
| 204 | |||
| 205 | public static function isUnitLocatorNotSet($location_type, $location_id) { |
||
| 208 | |||
| 209 | public static function locatorReset() { |
||
| 212 | |||
| 213 | public static function queriesReset() { |
||
| 216 | |||
| 217 | public static function getQueries() { |
||
| 220 | |||
| 221 | public static function getLocks() { |
||
| 224 | |||
| 225 | public static function getQueriesByLocationAndFilter($locationType, $filter) { |
||
| 228 | |||
| 229 | public static function isQueryCacheByLocationAndFilterEmpty($locationType, $filter) { |
||
| 232 | |||
| 233 | public static function queryCacheResetByLocationAndFilter($locationType, $filter) { |
||
| 236 | } |
||
| 237 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.