Complex classes like RankingSystemService 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 RankingSystemService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | abstract class RankingSystemService implements \Tfboe\FmLib\Service\RankingSystem\RankingSystemInterface |
||
| 35 | { |
||
| 36 | //<editor-fold desc="Fields"> |
||
| 37 | /** @var EntityManagerInterface */ |
||
| 38 | private $entityManager; |
||
| 39 | /** @var TimeServiceInterface */ |
||
| 40 | private $timeService; |
||
| 41 | /** @var EntityComparerInterface */ |
||
| 42 | private $entityComparer; |
||
| 43 | /** |
||
| 44 | * @var RankingSystemChangeInterface[][] |
||
| 45 | * first key: tournament hierarchy entity id |
||
| 46 | * second key: player id |
||
| 47 | */ |
||
| 48 | private $changes; |
||
| 49 | /** |
||
| 50 | * @var RankingSystemChangeInterface[][] |
||
| 51 | * first key: tournament hierarchy entity id |
||
| 52 | * second key: player id |
||
| 53 | */ |
||
| 54 | private $oldChanges; |
||
| 55 | /** |
||
| 56 | * List of ranking systems for which update ranking got already called, indexed by id |
||
| 57 | * @var RankingSystemService[] |
||
| 58 | */ |
||
| 59 | private $updateRankingCalls; |
||
| 60 | |||
| 61 | /** @var ObjectCreatorServiceInterface */ |
||
| 62 | private $objectCreatorService; |
||
| 63 | //</editor-fold desc="Fields"> |
||
| 64 | |||
| 65 | //<editor-fold desc="Constructor"> |
||
| 66 | /** |
||
| 67 | * RankingSystemService constructor. |
||
| 68 | * @param EntityManagerInterface $entityManager |
||
| 69 | * @param TimeServiceInterface $timeService |
||
| 70 | * @param EntityComparerInterface $entityComparer |
||
| 71 | * @param ObjectCreatorServiceInterface $objectCreatorService |
||
| 72 | */ |
||
| 73 | public function __construct(EntityManagerInterface $entityManager, TimeServiceInterface $timeService, |
||
| 85 | //</editor-fold desc="Constructor"> |
||
| 86 | |||
| 87 | //<editor-fold desc="Public Methods"> |
||
| 88 | /** |
||
| 89 | * @inheritDoc |
||
| 90 | */ |
||
| 91 | public function getEarliestInfluence(RankingSystemInterface $ranking, TournamentInterface $tournament): ?\DateTime |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @inheritdoc |
||
| 98 | */ |
||
| 99 | public function updateRankingForTournament(RankingSystemInterface $ranking, TournamentInterface $tournament, |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @inheritDoc |
||
| 114 | */ |
||
| 115 | public function updateRankingFrom(RankingSystemInterface $ranking, \DateTime $from) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param RankingSystemInterface $ranking |
||
| 191 | * @param RankingSystemListInterface $lastReusable |
||
| 192 | * @param RankingSystemListInterface $list |
||
| 193 | * @return array|TournamentHierarchyEntity[] |
||
| 194 | */ |
||
| 195 | private function getNextEntities(RankingSystemInterface $ranking, RankingSystemListInterface $lastReusable, |
||
| 212 | //</editor-fold desc="Public Methods"> |
||
| 213 | //<editor-fold desc="Protected Final Methods"> |
||
| 214 | /** |
||
| 215 | * Computes the average rating of the given entries |
||
| 216 | * @param RankingSystemListEntryInterface[] $entries |
||
| 217 | * @return float |
||
| 218 | */ |
||
| 219 | protected final function getAverage(array $entries): float |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Gets the relevant entities for updating |
||
| 234 | * @param RankingSystemInterface $ranking the ranking for which to get the entities |
||
| 235 | * @param \DateTime $from search for entities with a time value LARGER than $from, i.e. don't search for entities with |
||
| 236 | * time value exactly $from |
||
| 237 | * @param \DateTime to search for entities with a time value SMALLER OR EQUAL than $to |
||
| 238 | * @return TournamentHierarchyEntity[] |
||
| 239 | */ |
||
| 240 | protected final function getEntities(RankingSystemInterface $ranking, \DateTime $from, \DateTime $to): array |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return EntityManagerInterface |
||
| 248 | */ |
||
| 249 | protected final function getEntityManager(): EntityManagerInterface |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param Collection|PlayerInterface[] $players |
||
| 256 | * @param RankingSystemListInterface $list |
||
| 257 | * @return RankingSystemListEntryInterface[] $entries |
||
| 258 | */ |
||
| 259 | protected final function getEntriesOfPlayers(Collection $players, RankingSystemListInterface $list): array |
||
| 267 | |||
| 268 | /** @noinspection PhpDocMissingThrowsInspection */ //PropertyNotExistingException |
||
| 269 | /** |
||
| 270 | * Gets or creates a tournament system change entry for the given entity, ranking and player. |
||
| 271 | * @param TournamentHierarchyInterface $entity the tournament hierarchy entity to search for |
||
| 272 | * @param RankingSystemInterface $ranking the ranking system to search for |
||
| 273 | * @param PlayerInterface $player the player to search for |
||
| 274 | * @return RankingSystemChangeInterface the found or newly created ranking system change |
||
| 275 | */ |
||
| 276 | protected final function getOrCreateChange(TournamentHierarchyInterface $entity, RankingSystemInterface $ranking, |
||
| 306 | |||
| 307 | /** @noinspection PhpDocMissingThrowsInspection */ //PropertyNotExistingException |
||
| 308 | /** |
||
| 309 | * @param RankingSystemListInterface $list the list in which to search for the entry or in which to add it |
||
| 310 | * @param PlayerInterface $player the player to search for |
||
| 311 | * @return RankingSystemListEntryInterface the found or the new entry |
||
| 312 | */ |
||
| 313 | protected final function getOrCreateRankingSystemListEntry(RankingSystemListInterface $list, |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param RankingSystemListEntryInterface $entry |
||
| 331 | * @throws \Tfboe\FmLib\Exceptions\PropertyNotExistingException |
||
| 332 | */ |
||
| 333 | private function resetListEntry(RankingSystemListEntryInterface $entry) |
||
| 343 | //</editor-fold desc="Protected Final Methods"> |
||
| 344 | |||
| 345 | //<editor-fold desc="Protected Methods"> |
||
| 346 | /** |
||
| 347 | * Gets additional fields for this ranking type mapped to its start value |
||
| 348 | * @return string[] list of additional fields |
||
| 349 | */ |
||
| 350 | protected abstract function getAdditionalFields(): array; |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Gets all ranking changes for the given entity for the given list. Must return a change for each involved player. |
||
| 354 | * The field pointsAfterwards get calculated afterwards and can be left empty. |
||
| 355 | * @param TournamentHierarchyEntity $entity the entity for which to compute the ranking changes |
||
| 356 | * @param RankingSystemListInterface $list the list for which to compute the ranking changes |
||
| 357 | * @return RankingSystemChangeInterface[] the changes |
||
| 358 | */ |
||
| 359 | protected abstract function getChanges(TournamentHierarchyEntity $entity, RankingSystemListInterface $list): array; |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Gets a query for getting the relevant entities for updating |
||
| 363 | * @param RankingSystemInterface $ranking the ranking for which to get the entities |
||
| 364 | * @param \DateTime $from search for entities with a time value LARGER than $from, i.e. don't search for entities with |
||
| 365 | * time value exactly $from |
||
| 366 | * @param \DateTime to search for entities with a time value SMALLER OR EQUAL than $to |
||
| 367 | * @return QueryBuilder |
||
| 368 | */ |
||
| 369 | protected abstract function getEntitiesQueryBuilder(RankingSystemInterface $ranking, |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Gets the level of the ranking system service (see Level Enum) |
||
| 374 | * @return int |
||
| 375 | */ |
||
| 376 | protected abstract function getLevel(): int; |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Gets the start points for a new player in the ranking |
||
| 380 | * @return float |
||
| 381 | */ |
||
| 382 | protected function startPoints(): float |
||
| 386 | //</editor-fold desc="Protected Methods"> |
||
| 387 | |||
| 388 | //<editor-fold desc="Private Methods"> |
||
| 389 | /** |
||
| 390 | * Clones all ranking values from base and inserts them into list, furthermore removes all remaining ranking values of |
||
| 391 | * list. After this method was called list and base contain exactly the same rankings. |
||
| 392 | * @param RankingSystemListInterface $list the ranking list to change |
||
| 393 | * @param RankingSystemListInterface $base the ranking list to use as base list, this doesn't get changed |
||
| 394 | */ |
||
| 395 | private function cloneInto(RankingSystemListInterface $list, RankingSystemListInterface $base) |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * @param RankingSystemInterface $ranking |
||
| 437 | * @param TournamentHierarchyEntity[] $entities |
||
| 438 | */ |
||
| 439 | private function markOldChangesAsDeleted(RankingSystemInterface $ranking, array $entities) |
||
| 466 | |||
| 467 | private function deleteOldChanges() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Gets the earliest influence for the given entity |
||
| 479 | * @param RankingSystemInterface $ranking the ranking system for which to get the influence |
||
| 480 | * @param TournamentHierarchyInterface $entity the entity to analyze |
||
| 481 | * @param bool $parentIsRanked true iff a predecessor contained the given ranking in its ranking systems |
||
| 482 | * @return \DateTime|null the earliest influence or null if $parentIsRanked is false and the entity and all its |
||
| 483 | * successors do not have the ranking in its ranking systems |
||
| 484 | */ |
||
| 485 | private function getEarliestEntityInfluence(RankingSystemInterface $ranking, TournamentHierarchyInterface $entity, |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @param \DateTime $time the time of the last list |
||
| 510 | * @param int $generationLevel the list generation level |
||
| 511 | * @return \DateTime the time of the next list generation |
||
| 512 | */ |
||
| 513 | private function getNextGenerationTime(\DateTime $time, int $generationLevel): \DateTime |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @return \DateTime |
||
| 533 | * @throws \Exception |
||
| 534 | */ |
||
| 535 | private function getMaxDate(): \DateTime |
||
| 539 | |||
| 540 | /** @noinspection PhpDocMissingThrowsInspection */ //PropertyNotExistingException |
||
| 541 | /** |
||
| 542 | * Recomputes the given ranking list by using base as base list and applying the changes for the given entities |
||
| 543 | * starting from the given index. If list is not the current list only the entities up to $list->getLastEntryTime() |
||
| 544 | * are applied and the index gets changed accordingly. |
||
| 545 | * @param RankingSystemListInterface $list the list to recompute |
||
| 546 | * @param RankingSystemListInterface $base the list to use as base |
||
| 547 | * @param TournamentHierarchyEntity[] $entities the list of entities to use for the computation |
||
| 548 | * @param \DateTime $lastListTime the time of the last list or the first entry |
||
| 549 | */ |
||
| 550 | private function recomputeBasedOn(RankingSystemListInterface $list, RankingSystemListInterface $base, |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @param TournamentHierarchyInterface[] $entities |
||
| 600 | * @param int &$current |
||
| 601 | */ |
||
| 602 | private function flushAndForgetEntities(&$entities, &$current) |
||
| 631 | //</editor-fold desc="Private Methods"> |
||
| 632 | } |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: