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 | * @var db_mysql $db |
||
43 | */ |
||
44 | protected $db; |
||
45 | |||
46 | /** |
||
47 | * SnDbCachedOperator constructor. |
||
48 | * |
||
49 | * @param \Common\GlobalContainer $gc |
||
50 | */ |
||
51 | public function __construct($gc) { |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Repacking data for $location_type |
||
58 | * |
||
59 | * @param int $location_type |
||
60 | * @param int $record_id |
||
61 | */ |
||
62 | public function cache_repack($location_type, $record_id = 0) { |
||
72 | |||
73 | public function cache_clear($location_type, $hard = true) { |
||
83 | |||
84 | // TODO - UNUSED ???????????? |
||
85 | public function cache_clear_all($hard = true) { |
||
93 | |||
94 | public function cache_isset($location_type, $record_id) { |
||
97 | |||
98 | // TODO - UNUSED ???????????? |
||
99 | public function cache_get($location_type, $record_id) { |
||
102 | |||
103 | /* Кэшируем запись в соответствующий кэш |
||
104 | |||
105 | Писать в кэш: |
||
106 | 1. Если записи не существует в кэше |
||
107 | 2. Если стоит $force_overwrite |
||
108 | 3. Если во время транзакции существующая запись не заблокирована |
||
109 | |||
110 | Блокировать запись: |
||
111 | 1. Если идет транзакция и запись не заблокирована |
||
112 | 2. Если не стоит скип-лок |
||
113 | */ |
||
114 | public function cache_set($location_type, $record, $force_overwrite = false, $skip_lock = false) { |
||
135 | |||
136 | public function queryCacheSetByFilter($location_type, $filter, $record_id) { |
||
139 | |||
140 | public function cache_unset($cache_id, $safe_record_id) { |
||
149 | |||
150 | public function cache_lock_get($location_type, $record_id) { |
||
153 | |||
154 | public function cache_lock_set($location_type, $record_id) { |
||
157 | |||
158 | // TODO - UNUSED ???????????? |
||
159 | public function cache_lock_unset($location_type, $record_id) { |
||
166 | |||
167 | public function cache_lock_unset_all() { |
||
174 | |||
175 | public function getData($locationType = LOC_NONE) { |
||
178 | |||
179 | public function cacheUnsetElement($locationType, $recordId) { |
||
182 | |||
183 | public function isArrayLocation($locationType) { |
||
186 | |||
187 | /** |
||
188 | * Return reference to record in $data by locationType and recordId |
||
189 | * |
||
190 | * @param int $locationType |
||
191 | * @param int $recordId |
||
192 | * |
||
193 | * @return &mixed |
||
194 | */ |
||
195 | public function &getDataRefByLocationAndId($locationType, $recordId) { |
||
198 | |||
199 | // TODO UNUSED ???? |
||
200 | public function setUnitLocator($unit, $unit_id) { |
||
205 | |||
206 | public function getUnitLocator($location_type, $location_id, $unit_snid) { |
||
209 | |||
210 | public function getUnitLocatorByFullLocation($location_type, $location_id) { |
||
213 | |||
214 | public function setUnitLocatorByLocationAndIDs($location_type, $location_id, $unit_data) { |
||
217 | |||
218 | public function isUnitLocatorNotSet($location_type, $location_id) { |
||
221 | |||
222 | public function locatorReset() { |
||
225 | |||
226 | public function queriesReset() { |
||
229 | |||
230 | public function getQueries() { |
||
233 | |||
234 | public function getLocks() { |
||
237 | |||
238 | public function getQueriesByLocationAndFilter($locationType, $filter) { |
||
241 | |||
242 | public function isQueryCacheByLocationAndFilterEmpty($locationType, $filter) { |
||
245 | |||
246 | public function queryCacheResetByLocationAndFilter($locationType, $filter) { |
||
249 | |||
250 | } |
||
251 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.