| Conditions | 9 |
| Paths | 1 |
| Total 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 |
||
| 123 | public function recalculateRankingSystems(): void |
||
| 124 | { |
||
| 125 | //clear entityManager to save memory |
||
| 126 | $this->entityManager->flush(); |
||
| 127 | $this->entityManager->clear(); |
||
| 128 | $rankingSystemOpenSyncFroms = []; |
||
| 129 | /** @var RankingSystemInterface[] $rankingSystems */ |
||
| 130 | $rankingSystems = []; |
||
| 131 | $this->entityManager->transactional( |
||
| 132 | function (EntityManager $em) use (&$rankingSystems, &$rankingSystemOpenSyncFroms) { |
||
| 133 | $em->find(LastRecalculationInterface::class, 1, LockMode::PESSIMISTIC_WRITE); |
||
| 134 | $query = $em->createQueryBuilder(); |
||
| 135 | $query |
||
| 136 | ->from(RankingSystemInterface::class, 's') |
||
| 137 | ->select('s') |
||
| 138 | ->where($query->expr()->isNotNull('s.openSyncFrom')) |
||
| 139 | ->orWhere($query->expr()->isNotNull('s.openSyncFromInProcess')); |
||
| 140 | /** @var RankingSystemInterface[] $rankingSystems */ |
||
| 141 | $rankingSystems = $query->getQuery()->setLockMode(LockMode::PESSIMISTIC_WRITE)->getResult(); |
||
| 142 | foreach ($rankingSystems as $rankingSystem) { |
||
| 143 | if ($rankingSystem->getOpenSyncFrom() !== null && ($rankingSystem->getOpenSyncFromInProcess() === null || |
||
| 144 | $rankingSystem->getOpenSyncFrom() < $rankingSystem->getOpenSyncFromInProcess())) { |
||
| 145 | $rankingSystemOpenSyncFroms[$rankingSystem->getId()] = $rankingSystem->getOpenSyncFrom(); |
||
| 146 | $rankingSystem->setOpenSyncFrom(null); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | ); |
||
| 151 | |||
| 152 | $this->entityManager->transactional( |
||
| 153 | function (EntityManager $em) use (&$rankingSystems, &$rankingSystemOpenSyncFroms) { |
||
| 154 | /** @var LastRecalculationInterface $lastRecalculation */ |
||
| 155 | $lastRecalculation = $em->find(LastRecalculationInterface::class, 1, LockMode::PESSIMISTIC_WRITE); |
||
| 156 | foreach ($rankingSystems as $rankingSystem) { |
||
| 157 | if (array_key_exists($rankingSystem->getId(), $rankingSystemOpenSyncFroms) && |
||
| 158 | $rankingSystemOpenSyncFroms[$rankingSystem->getId()] < $rankingSystem->getOpenSyncFromInProcess()) { |
||
| 159 | $rankingSystem->setOpenSyncFromInProcess($rankingSystemOpenSyncFroms[$rankingSystem->getId()]); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | $em->flush(); |
||
| 163 | $lastRecalculation->setStartTime(new \DateTime()); |
||
| 164 | foreach ($rankingSystems as $rankingSystem) { |
||
| 165 | $service = $this->dsls->loadRankingSystemService($rankingSystem->getServiceName()); |
||
| 166 | $service->updateRankingFrom($rankingSystem, $rankingSystem->getOpenSyncFrom()); |
||
|
|
|||
| 167 | $rankingSystem->setOpenSyncFromInProcess(null); |
||
| 168 | } |
||
| 169 | $lastRecalculation->setEndTime(new \DateTime()); |
||
| 170 | $lastRecalculation->setVersion($lastRecalculation->getVersion() + 1); |
||
| 171 | } |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | //</editor-fold desc="Public Methods"> |
||
| 190 | } |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: