Complex classes like UserService 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 UserService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class UserService |
||
| 28 | { |
||
| 29 | |||
| 30 | /** @var KernelInterface */ |
||
| 31 | private $kernel; |
||
| 32 | /** @var RedisClient */ |
||
| 33 | private $redis; |
||
| 34 | /** @var HumanRepository */ |
||
| 35 | private $humanRepository; |
||
| 36 | /** @var InventoryItemRepository */ |
||
| 37 | private $inventoryItemRepository; |
||
| 38 | /** @var Logger */ |
||
| 39 | private $logger; |
||
| 40 | /** @var RoomRepository */ |
||
| 41 | private $roomRepository; |
||
| 42 | /** @var ItemRepository */ |
||
| 43 | private $itemRepository; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param KernelInterface $kernel |
||
| 47 | * @param RedisClient $redis |
||
| 48 | * @param Logger $logger |
||
| 49 | * @param HumanRepository $humanRepository |
||
| 50 | * @param InventoryItemRepository $inventoryItemRepository |
||
| 51 | * @param RoomRepository $roomRepository |
||
| 52 | * @param ItemRepository $itemRepository |
||
| 53 | */ |
||
| 54 | public function __construct( |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Запрос ID всех онлайн игроков в комнате |
||
| 74 | * @param Room $room |
||
| 75 | * @param int|int[] $excludePlayerIds |
||
| 76 | * @return int[] |
||
| 77 | */ |
||
| 78 | public function getOnlineUsersIdsInRoom(Room $room, $excludePlayerIds = []): array |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Запрос всех онлайн игроков в комнате |
||
| 90 | * @param Room $room |
||
| 91 | * @param int|array $excludePlayerIds |
||
| 92 | * @return Human[] |
||
| 93 | */ |
||
| 94 | public function getOnlineHumansInRoom(Room $room, $excludePlayerIds = []): array |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Запрос id всех игроков онлайн из redis |
||
| 105 | * @return int[] |
||
| 106 | */ |
||
| 107 | public function getOnlineUsersIds(): array |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param array $userIds |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function getSessionsByUserIds(array $userIds): array |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Передать один или несколько предметов другому персонажу |
||
| 123 | * @param User $userFrom |
||
| 124 | * @param User $userTo |
||
| 125 | * @param Item|Item[] $items |
||
| 126 | * @param int $quantityToGive Сколько предметов передать |
||
| 127 | * @return bool |
||
| 128 | * @throws \Exception |
||
| 129 | */ |
||
| 130 | public function giveItems(User $userFrom, User $userTo, $items, int $quantityToGive = 1): bool |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Выбросить один или несколько предметов |
||
| 153 | * @param User $user |
||
| 154 | * @param Item|Item[] $items |
||
| 155 | * @param int $quantityToDrop Сколько предметов выбросить |
||
| 156 | * @return bool |
||
| 157 | * @throws ItemNotFound |
||
| 158 | * @throws NotEnoughItems |
||
| 159 | */ |
||
| 160 | public function dropItems(User $user, $items, int $quantityToDrop): bool |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Взять один или несколько предметов |
||
| 199 | * @param User $user |
||
| 200 | * @param Item|Item[] $items |
||
| 201 | * @param int $quantityToTake Сколько предметов взять |
||
| 202 | */ |
||
| 203 | public function takeItems(User $user, $items, int $quantityToTake = 1) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Установка рэндомного аватара |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function pickAvatar(): string |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Транслитерация и конвертация строки, удаление цифр |
||
| 263 | * @param string $string |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function transliterate(string $string): string |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Массив соответствия русских букв латинским |
||
| 285 | * @return string[] |
||
| 286 | */ |
||
| 287 | private function getAlphabet(): array |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Стартовая комната при создании персонажа |
||
| 306 | * @return Room |
||
| 307 | * @throws RoomNotFound |
||
| 308 | */ |
||
| 309 | public function getStartRoom(): Room |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Стартовые предметы при создании персонажа |
||
| 322 | * @param User $user |
||
| 323 | */ |
||
| 324 | public function giveStarterItems(User $user) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Подготовка массива предметов |
||
| 340 | * @param Item|Item[] $items |
||
| 341 | * @return Item[] |
||
| 342 | * @throws NotEnoughItems |
||
| 343 | */ |
||
| 344 | private function prepareItemsArray($items): array |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param User $user |
||
| 364 | * @param Item[] $itemsToTake |
||
| 365 | * @param int $quantityToTake |
||
| 366 | */ |
||
| 367 | private function logObtainedItems(User $user, array $itemsToTake, int $quantityToTake) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param User $userFrom |
||
| 386 | * @param User $userTo |
||
| 387 | * @param Item[] $items |
||
| 388 | * @param int $quantityToGive |
||
| 389 | */ |
||
| 390 | private function logGivenItems(User $userFrom, User $userTo, array $items, int $quantityToGive) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param User $user |
||
| 411 | * @param Item[] $items |
||
| 412 | * @param int $quantityToDrop |
||
| 413 | */ |
||
| 414 | private function logDroppedItems($user, array $items, int $quantityToDrop) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Назначение вейтстейта юзеру |
||
| 432 | * @param int $waitState |
||
| 433 | * @return bool |
||
| 434 | */ |
||
| 435 | public function addWaitstate(User $user, int $waitState): bool { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return EntityManager |
||
| 444 | */ |
||
| 445 | private function getEntityManager(): EntityManager { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param User $user |
||
| 451 | */ |
||
| 452 | public function dropWaitState(User $user) |
||
| 457 | } |
||
| 458 |