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 $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 $locator = array(); |
||
26 | |||
27 | /** |
||
28 | * Кэш запросов |
||
29 | * |
||
30 | * @var array $queries |
||
31 | */ |
||
32 | protected $queries = array(); |
||
33 | |||
34 | /** |
||
35 | * Информация о блокировках |
||
36 | * |
||
37 | * @var array $locks |
||
38 | */ |
||
39 | protected $locks = array(); |
||
40 | |||
41 | /** |
||
42 | * @var db_mysql $db |
||
43 | */ |
||
44 | protected $db; |
||
45 | |||
46 | /** |
||
47 | * SnDbCachedOperator constructor. |
||
48 | * |
||
49 | * @param \Common\GlobalContainer $gc |
||
50 | * @param db_mysql $db |
||
51 | */ |
||
52 | public function __construct($gc, $db) { |
||
55 | |||
56 | |||
57 | /** |
||
58 | * Repacking data for $location_type |
||
59 | * |
||
60 | * @param int $location_type |
||
61 | * @param int $record_id |
||
62 | */ |
||
63 | public function cache_repack($location_type, $record_id = 0) { |
||
73 | |||
74 | public function cache_clear($location_type, $hard = true) { |
||
84 | |||
85 | // TODO - UNUSED ???????????? |
||
86 | public function cache_clear_all($hard = true) { |
||
94 | |||
95 | public function cache_isset($location_type, $record_id) { |
||
98 | |||
99 | // TODO - UNUSED ???????????? |
||
100 | public function cache_get($location_type, $record_id) { |
||
103 | |||
104 | /* Кэшируем запись в соответствующий кэш |
||
105 | |||
106 | Писать в кэш: |
||
107 | 1. Если записи не существует в кэше |
||
108 | 2. Если стоит $force_overwrite |
||
109 | 3. Если во время транзакции существующая запись не заблокирована |
||
110 | |||
111 | Блокировать запись: |
||
112 | 1. Если идет транзакция и запись не заблокирована |
||
113 | 2. Если не стоит скип-лок |
||
114 | */ |
||
115 | public function cache_set($location_type, $record, $force_overwrite = false, $skip_lock = false) { |
||
136 | |||
137 | public function queryCacheSetByFilter($location_type, $filter, $record_id) { |
||
140 | |||
141 | public function cache_unset($cache_id, $safe_record_id) { |
||
150 | |||
151 | public function cache_lock_get($location_type, $record_id) { |
||
154 | |||
155 | public function cache_lock_set($location_type, $record_id) { |
||
158 | |||
159 | // TODO - UNUSED ???????????? |
||
160 | public function cache_lock_unset($location_type, $record_id) { |
||
167 | |||
168 | public function cache_lock_unset_all() { |
||
175 | |||
176 | public function getData($locationType = LOC_NONE) { |
||
179 | |||
180 | public function cacheUnsetElement($locationType, $recordId) { |
||
183 | |||
184 | public function isArrayLocation($locationType) { |
||
187 | |||
188 | /** |
||
189 | * Return reference to record in $data by locationType and recordId |
||
190 | * |
||
191 | * @param int $locationType |
||
192 | * @param int $recordId |
||
193 | * |
||
194 | * @return &mixed |
||
195 | */ |
||
196 | public function &getDataRefByLocationAndId($locationType, $recordId) { |
||
199 | |||
200 | // TODO UNUSED ???? |
||
201 | public function setUnitLocator($unit, $unit_id) { |
||
206 | |||
207 | public function getUnitLocator($location_type, $location_id, $unit_snid) { |
||
210 | |||
211 | public function getUnitLocatorByFullLocation($location_type, $location_id) { |
||
214 | |||
215 | public function setUnitLocatorByLocationAndIDs($location_type, $location_id, $unit_data) { |
||
218 | |||
219 | public function isUnitLocatorNotSet($location_type, $location_id) { |
||
222 | |||
223 | public function locatorReset() { |
||
226 | |||
227 | public function queriesReset() { |
||
230 | |||
231 | public function getQueries() { |
||
234 | |||
235 | public function getLocks() { |
||
238 | |||
239 | public function getQueriesByLocationAndFilter($locationType, $filter) { |
||
242 | |||
243 | public function isQueryCacheByLocationAndFilterEmpty($locationType, $filter) { |
||
246 | |||
247 | public function queryCacheResetByLocationAndFilter($locationType, $filter) { |
||
250 | |||
251 | } |
||
252 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.