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 Item|Item[] $items  | 
            ||
| 154 | * @return Item[]  | 
            ||
| 155 | * @throws NotEnoughItems  | 
            ||
| 156 | */  | 
            ||
| 157 | private function prepareItemsArray($items): array  | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Выбросить один или несколько предметов  | 
            ||
| 177 | * @param User $user  | 
            ||
| 178 | * @param Item|Item[] $items  | 
            ||
| 179 | * @param int $quantityToDrop Сколько предметов выбросить  | 
            ||
| 180 | * @return bool  | 
            ||
| 181 | * @throws ItemNotFound  | 
            ||
| 182 | * @throws NotEnoughItems  | 
            ||
| 183 | */  | 
            ||
| 184 | public function dropItems(User $user, $items, int $quantityToDrop): bool  | 
            ||
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * @param User $user  | 
            ||
| 223 | * @param Item[] $items  | 
            ||
| 224 | * @param int $quantityToDrop  | 
            ||
| 225 | */  | 
            ||
| 226 | private function logDroppedItems($user, array $items, int $quantityToDrop)  | 
            ||
| 241 | |||
| 242 | /**  | 
            ||
| 243 | * Взять один или несколько предметов  | 
            ||
| 244 | * @param User $user  | 
            ||
| 245 | * @param Item|Item[] $items  | 
            ||
| 246 | * @param int $quantityToTake Сколько предметов взять  | 
            ||
| 247 | */  | 
            ||
| 248 | public function takeItems(User $user, $items, int $quantityToTake = 1)  | 
            ||
| 281 | |||
| 282 | /**  | 
            ||
| 283 | * @param User $user  | 
            ||
| 284 | * @param Item[] $itemsToTake  | 
            ||
| 285 | * @param int $quantityToTake  | 
            ||
| 286 | */  | 
            ||
| 287 | private function logObtainedItems(User $user, array $itemsToTake, int $quantityToTake)  | 
            ||
| 303 | |||
| 304 | /**  | 
            ||
| 305 | * @param User $userFrom  | 
            ||
| 306 | * @param User $userTo  | 
            ||
| 307 | * @param Item[] $items  | 
            ||
| 308 | * @param int $quantityToGive  | 
            ||
| 309 | */  | 
            ||
| 310 | private function logGivenItems(User $userFrom, User $userTo, array $items, int $quantityToGive)  | 
            ||
| 328 | |||
| 329 | /**  | 
            ||
| 330 | * Установка рэндомного аватара  | 
            ||
| 331 | * @return string  | 
            ||
| 332 | */  | 
            ||
| 333 | public function pickAvatar(): string  | 
            ||
| 352 | |||
| 353 | /**  | 
            ||
| 354 | * Транслитерация и конвертация строки, удаление цифр  | 
            ||
| 355 | * @param string $string  | 
            ||
| 356 | * @return string  | 
            ||
| 357 | */  | 
            ||
| 358 | public function transliterate(string $string): string  | 
            ||
| 374 | |||
| 375 | /**  | 
            ||
| 376 | * Массив соответствия русских букв латинским  | 
            ||
| 377 | * @return string[]  | 
            ||
| 378 | */  | 
            ||
| 379 | private function getAlphabet(): array  | 
            ||
| 395 | |||
| 396 | /**  | 
            ||
| 397 | * Стартовая комната при создании персонажа  | 
            ||
| 398 | * @return Room  | 
            ||
| 399 | * @throws RoomNotFound  | 
            ||
| 400 | */  | 
            ||
| 401 | public function getStartRoom(): Room  | 
            ||
| 411 | |||
| 412 | /**  | 
            ||
| 413 | * Стартовые предметы при создании персонажа  | 
            ||
| 414 | * @param User $user  | 
            ||
| 415 | */  | 
            ||
| 416 | public function giveStarterItems(User $user)  | 
            ||
| 429 | |||
| 430 | /**  | 
            ||
| 431 | * Назначение вейтстейта юзеру  | 
            ||
| 432 | * @param int $waitState  | 
            ||
| 433 | * @return bool  | 
            ||
| 434 | */  | 
            ||
| 435 | public function addWaitstate(User $user, int $waitState): bool  | 
            ||
| 442 | |||
| 443 | /**  | 
            ||
| 444 | * @return EntityManager  | 
            ||
| 445 | */  | 
            ||
| 446 | private function getEntityManager(): EntityManager  | 
            ||
| 450 | |||
| 451 | /**  | 
            ||
| 452 | * @param User $user  | 
            ||
| 453 | */  | 
            ||
| 454 | public function dropWaitState(User $user)  | 
            ||
| 459 | }  | 
            ||
| 460 |