| Conditions | 9 |
| Paths | 11 |
| Total Lines | 79 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 30 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 31 | { |
||
| 32 | $container = $this->getContainer(); |
||
| 33 | |||
| 34 | $itemRepository = $container->get('kingdom.item_repository'); |
||
| 35 | $humanRepository = $container->get('kingdom.human_repository'); |
||
| 36 | $em = $itemRepository->getEntityManager(); |
||
| 37 | |||
| 38 | $items = $itemRepository->findAllItems(); |
||
| 39 | $users = $humanRepository->findAllHumans(); |
||
| 40 | $rooms = $container->get('kingdom.room_repository')->findAllRooms(); |
||
| 41 | |||
| 42 | if (count($items)) { |
||
| 43 | $output->writeln( |
||
| 44 | sprintf('Уже создано %d предметов. Удалите их командой kingdom:purge:items', count($items)) |
||
| 45 | ); |
||
| 46 | } else { |
||
| 47 | $output->writeln('Загрузка данных о предметах ... '); |
||
| 48 | $allNewItemsData = $this->parseItemsFromYaml(); |
||
| 49 | |||
| 50 | foreach ($allNewItemsData as $newItemType => $newItemsData) { |
||
| 51 | $newItemType = mb_convert_case($newItemType, MB_CASE_TITLE); |
||
| 52 | $itemClass = 'Rottenwood\\KingdomBundle\\Entity\\Items\\' . $newItemType; |
||
| 53 | |||
| 54 | foreach ($newItemsData as $newItemId => $newItemData) { |
||
| 55 | /** @var Item $newItem */ |
||
| 56 | $newItem = new $itemClass( |
||
| 57 | $newItemId, |
||
| 58 | $newItemData['name'][0], |
||
| 59 | $newItemData['name'][1], |
||
| 60 | $newItemData['name'][2], |
||
| 61 | $newItemData['name'][3], |
||
| 62 | $newItemData['name'][4], |
||
| 63 | $newItemData['name'][5], |
||
| 64 | $newItemData['desc'], |
||
| 65 | $newItemData['pic'], |
||
| 66 | is_array($newItemData['slots']) ? $newItemData['slots'] : [$newItemData['slots']] |
||
| 67 | ); |
||
| 68 | |||
| 69 | $em->persist($newItem); |
||
| 70 | |||
| 71 | $output->writeln(sprintf('Создан предмет %s!', $newItem->getName())); |
||
| 72 | |||
| 73 | foreach ($users as $user) { |
||
| 74 | $inventoryItem = new InventoryItem($user, $newItem); |
||
| 75 | $em->persist($inventoryItem); |
||
| 76 | |||
| 77 | $output->writeln( |
||
| 78 | sprintf('Предмет "%s" передан персонажу %s.', $newItem->getName(), $user->getName()) |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($newItem instanceof ResourceWood) { |
||
| 83 | foreach ($rooms as $room) { |
||
| 84 | if ($room->getType() instanceof Forest) { |
||
| 85 | $roomResource = new RoomResource($room, $newItem, self::RESOURCE_QUANTITY); |
||
| 86 | $em->persist($roomResource); |
||
| 87 | |||
| 88 | $output->writeln( |
||
| 89 | sprintf( |
||
| 90 | 'Ресурс "%s" добавлен в комнату %s[%d/%d] в количестве %d единиц.', |
||
| 91 | $newItem->getName(), |
||
| 92 | $room->getName(), |
||
| 93 | $room->getX(), |
||
| 94 | $room->getY(), |
||
| 95 | $roomResource->getQuantity() |
||
| 96 | ) |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | $em->flush(); |
||
| 105 | |||
| 106 | $output->writeln('Создано новых предметов: ' . count($itemRepository->findAll())); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 130 |