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 |
||
| 33 | abstract class RankingSystemService implements \Tfboe\FmLib\Service\RankingSystem\RankingSystemInterface |
||
| 34 | { |
||
| 35 | //<editor-fold desc="Fields"> |
||
| 36 | /** @var EntityManagerInterface */ |
||
| 37 | private $entityManager; |
||
| 38 | /** @var TimeServiceInterface */ |
||
| 39 | private $timeService; |
||
| 40 | /** @var EntityComparerInterface */ |
||
| 41 | private $entityComparer; |
||
| 42 | /** |
||
| 43 | * @var RankingSystemChangeInterface[][][] |
||
| 44 | * first key: tournament hierarchy entity id |
||
| 45 | * second key: ranking system id |
||
| 46 | * third key: player id |
||
| 47 | */ |
||
| 48 | private $changes; |
||
| 49 | /** |
||
| 50 | * @var RankingSystemChangeInterface[][][] |
||
| 51 | * first key: tournament hierarchy entity id |
||
| 52 | * second key: ranking system id |
||
| 53 | * third key: player id |
||
| 54 | */ |
||
| 55 | private $deletedChanges; |
||
| 56 | /** |
||
| 57 | * List of ranking systems for which update ranking got already called, indexed by id |
||
| 58 | * @var RankingSystemService[] |
||
| 59 | */ |
||
| 60 | private $updateRankingCalls; |
||
| 61 | |||
| 62 | /** @var ObjectCreatorServiceInterface */ |
||
| 63 | private $objectCreatorService; |
||
| 64 | //</editor-fold desc="Fields"> |
||
| 65 | |||
| 66 | //<editor-fold desc="Constructor"> |
||
| 67 | /** |
||
| 68 | * RankingSystemService constructor. |
||
| 69 | * @param EntityManagerInterface $entityManager |
||
| 70 | * @param TimeServiceInterface $timeService |
||
| 71 | * @param EntityComparerInterface $entityComparer |
||
| 72 | * @param ObjectCreatorServiceInterface $objectCreatorService |
||
| 73 | */ |
||
| 74 | public function __construct(EntityManagerInterface $entityManager, TimeServiceInterface $timeService, |
||
| 86 | //</editor-fold desc="Constructor"> |
||
| 87 | |||
| 88 | //<editor-fold desc="Public Methods"> |
||
| 89 | /** |
||
| 90 | * @inheritDoc |
||
| 91 | */ |
||
| 92 | public function getEarliestInfluence(RankingSystemInterface $ranking, TournamentInterface $tournament): ?\DateTime |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @inheritdoc |
||
| 99 | */ |
||
| 100 | public function updateRankingForTournament(RankingSystemInterface $ranking, TournamentInterface $tournament, |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @inheritDoc |
||
| 115 | */ |
||
| 116 | public function updateRankingFrom(RankingSystemInterface $ranking, \DateTime $from) |
||
| 174 | //</editor-fold desc="Public Methods"> |
||
| 175 | |||
| 176 | //<editor-fold desc="Protected Final Methods"> |
||
| 177 | /** |
||
| 178 | * Computes the average rating of the given entries |
||
| 179 | * @param RankingSystemListEntryInterface[] $entries |
||
| 180 | * @return float |
||
| 181 | */ |
||
| 182 | protected final function getAverage(array $entries): float |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Gets the relevant entities for updating |
||
| 197 | * @param RankingSystemInterface $ranking the ranking for which to get the entities |
||
| 198 | * @param \DateTime $from search for entities with a time value LARGER than $from, i.e. don't search for entities with |
||
| 199 | * time value exactly $from |
||
| 200 | * @return TournamentHierarchyEntity[] |
||
| 201 | */ |
||
| 202 | protected final function getEntities(RankingSystemInterface $ranking, \DateTime $from): array |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return EntityManagerInterface |
||
| 210 | */ |
||
| 211 | protected final function getEntityManager(): EntityManagerInterface |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param Collection|PlayerInterface[] $players |
||
| 218 | * @param RankingSystemListInterface $list |
||
| 219 | * @return RankingSystemListEntryInterface[] $entries |
||
| 220 | */ |
||
| 221 | protected final function getEntriesOfPlayers(Collection $players, RankingSystemListInterface $list): array |
||
| 229 | |||
| 230 | /** @noinspection PhpDocMissingThrowsInspection */ //PropertyNotExistingException |
||
| 231 | /** |
||
| 232 | * Gets or creates a tournament system change entry for the given entity, ranking and player. |
||
| 233 | * @param TournamentHierarchyInterface $entity the tournament hierarchy entity to search for |
||
| 234 | * @param RankingSystemInterface $ranking the ranking system to search for |
||
| 235 | * @param PlayerInterface $player the player to search for |
||
| 236 | * @return RankingSystemChangeInterface the found or newly created ranking system change |
||
| 237 | */ |
||
| 238 | protected final function getOrCreateChange(TournamentHierarchyInterface $entity, RankingSystemInterface $ranking, |
||
| 285 | |||
| 286 | /** @noinspection PhpDocMissingThrowsInspection */ //PropertyNotExistingException |
||
| 287 | /** |
||
| 288 | * @param RankingSystemListInterface $list the list in which to search for the entry or in which to add it |
||
| 289 | * @param PlayerInterface $player the player to search for |
||
| 290 | * @return RankingSystemListEntryInterface the found or the new entry |
||
| 291 | */ |
||
| 292 | protected final function getOrCreateRankingSystemListEntry(RankingSystemListInterface $list, |
||
| 312 | //</editor-fold desc="Protected Final Methods"> |
||
| 313 | |||
| 314 | //<editor-fold desc="Protected Methods"> |
||
| 315 | /** |
||
| 316 | * Gets additional fields for this ranking type mapped to its start value |
||
| 317 | * @return string[] list of additional fields |
||
| 318 | */ |
||
| 319 | protected abstract function getAdditionalFields(): array; |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Gets all ranking changes for the given entity for the given list. Must return a change for each involved player. |
||
| 323 | * The field pointsAfterwards get calculated afterwards and can be left empty. |
||
| 324 | * @param TournamentHierarchyEntity $entity the entity for which to compute the ranking changes |
||
| 325 | * @param RankingSystemListInterface $list the list for which to compute the ranking changes |
||
| 326 | * @return RankingSystemChangeInterface[] the changes |
||
| 327 | */ |
||
| 328 | protected abstract function getChanges(TournamentHierarchyEntity $entity, RankingSystemListInterface $list): array; |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Gets a query for getting the relevant entities for updating |
||
| 332 | * @param RankingSystemInterface $ranking the ranking for which to get the entities |
||
| 333 | * @param \DateTime $from search for entities with a time value LARGER than $from, i.e. don't search for entities with |
||
| 334 | * time value exactly $from |
||
| 335 | * @return QueryBuilder |
||
| 336 | */ |
||
| 337 | protected abstract function getEntitiesQueryBuilder(RankingSystemInterface $ranking, |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Gets the level of the ranking system service (see Level Enum) |
||
| 342 | * @return int |
||
| 343 | */ |
||
| 344 | protected abstract function getLevel(): int; |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Gets the start points for a new player in the ranking |
||
| 348 | * @return float |
||
| 349 | */ |
||
| 350 | protected function startPoints(): float |
||
| 354 | //</editor-fold desc="Protected Methods"> |
||
| 355 | |||
| 356 | //<editor-fold desc="Private Methods"> |
||
| 357 | /** |
||
| 358 | * Clones all ranking values from base and inserts them into list, furthermore removes all remaining ranking values of |
||
| 359 | * list. After this method was called list and base contain exactly the same rankings. |
||
| 360 | * @param RankingSystemListInterface $list the ranking list to change |
||
| 361 | * @param RankingSystemListInterface $base the ranking list to use as base list, this doesn't get changed |
||
| 362 | */ |
||
| 363 | private function cloneInto(RankingSystemListInterface $list, RankingSystemListInterface $base) |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * @param RankingSystemInterface $ranking |
||
| 404 | * @param TournamentHierarchyEntity[] $entities |
||
| 405 | */ |
||
| 406 | private function deleteOldChanges(RankingSystemInterface $ranking, array $entities) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Gets the earliest influence for the given entity |
||
| 427 | * @param RankingSystemInterface $ranking the ranking system for which to get the influence |
||
| 428 | * @param TournamentHierarchyInterface $entity the entity to analyze |
||
| 429 | * @param bool $parentIsRanked true iff a predecessor contained the given ranking in its ranking systems |
||
| 430 | * @return \DateTime|null the earliest influence or null if $parentIsRanked is false and the entity and all its |
||
| 431 | * successors do not have the ranking in its ranking systems |
||
| 432 | */ |
||
| 433 | private function getEarliestEntityInfluence(RankingSystemInterface $ranking, TournamentHierarchyInterface $entity, |
||
| 455 | |||
| 456 | /** @noinspection PhpDocMissingThrowsInspection */ //PropertyNotExistingException |
||
| 457 | /** |
||
| 458 | * Recomputes the given ranking list by using base as base list and applying the changes for the given entities |
||
| 459 | * starting from the given index. If list is not the current list only the entities up to $list->getLastEntryTime() |
||
| 460 | * are applied and the index gets changed accordingly. |
||
| 461 | * @param RankingSystemListInterface $list the list to recompute |
||
| 462 | * @param RankingSystemListInterface $base the list to use as base |
||
| 463 | * @param TournamentHierarchyEntity[] $entities the list of entities to use for the computation |
||
| 464 | * @param int $nextEntityIndex the first index in the entities list to consider |
||
| 465 | */ |
||
| 466 | private function recomputeBasedOn(RankingSystemListInterface $list, RankingSystemListInterface $base, array $entities, |
||
| 497 | //</editor-fold desc="Private Methods"> |
||
| 498 | } |
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: