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: ranking system id |
||
47 | * third key: player id |
||
48 | */ |
||
49 | private $changes; |
||
50 | /** |
||
51 | * @var RankingSystemChangeInterface[][][] |
||
52 | * first key: tournament hierarchy entity id |
||
53 | * second key: ranking system id |
||
54 | * third key: player id |
||
55 | */ |
||
56 | private $deletedChanges; |
||
57 | /** |
||
58 | * List of ranking systems for which update ranking got already called, indexed by id |
||
59 | * @var RankingSystemService[] |
||
60 | */ |
||
61 | private $updateRankingCalls; |
||
62 | |||
63 | /** @var ObjectCreatorServiceInterface */ |
||
64 | private $objectCreatorService; |
||
65 | //</editor-fold desc="Fields"> |
||
66 | |||
67 | //<editor-fold desc="Constructor"> |
||
68 | /** |
||
69 | * RankingSystemService constructor. |
||
70 | * @param EntityManagerInterface $entityManager |
||
71 | * @param TimeServiceInterface $timeService |
||
72 | * @param EntityComparerInterface $entityComparer |
||
73 | * @param ObjectCreatorServiceInterface $objectCreatorService |
||
74 | */ |
||
75 | public function __construct(EntityManagerInterface $entityManager, TimeServiceInterface $timeService, |
||
87 | //</editor-fold desc="Constructor"> |
||
88 | |||
89 | //<editor-fold desc="Public Methods"> |
||
90 | /** |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | public function getEarliestInfluence(RankingSystemInterface $ranking, TournamentInterface $tournament): ?\DateTime |
||
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | */ |
||
101 | public function updateRankingForTournament(RankingSystemInterface $ranking, TournamentInterface $tournament, |
||
113 | |||
114 | /** |
||
115 | * @inheritDoc |
||
116 | */ |
||
117 | public function updateRankingFrom(RankingSystemInterface $ranking, \DateTime $from) |
||
188 | //</editor-fold desc="Public Methods"> |
||
189 | |||
190 | //<editor-fold desc="Protected Final Methods"> |
||
191 | /** |
||
192 | * Computes the average rating of the given entries |
||
193 | * @param RankingSystemListEntryInterface[] $entries |
||
194 | * @return float |
||
195 | */ |
||
196 | protected final function getAverage(array $entries): float |
||
208 | |||
209 | /** |
||
210 | * Gets the relevant entities for updating |
||
211 | * @param RankingSystemInterface $ranking the ranking for which to get the entities |
||
212 | * @param \DateTime $from search for entities with a time value LARGER than $from, i.e. don't search for entities with |
||
213 | * time value exactly $from |
||
214 | * @return TournamentHierarchyEntity[] |
||
215 | */ |
||
216 | protected final function getEntities(RankingSystemInterface $ranking, \DateTime $from): array |
||
221 | |||
222 | /** |
||
223 | * @return EntityManagerInterface |
||
224 | */ |
||
225 | protected final function getEntityManager(): EntityManagerInterface |
||
229 | |||
230 | /** |
||
231 | * @param Collection|PlayerInterface[] $players |
||
232 | * @param RankingSystemListInterface $list |
||
233 | * @return RankingSystemListEntryInterface[] $entries |
||
234 | */ |
||
235 | protected final function getEntriesOfPlayers(Collection $players, RankingSystemListInterface $list): array |
||
243 | |||
244 | /** @noinspection PhpDocMissingThrowsInspection */ //PropertyNotExistingException |
||
245 | /** |
||
246 | * Gets or creates a tournament system change entry for the given entity, ranking and player. |
||
247 | * @param TournamentHierarchyInterface $entity the tournament hierarchy entity to search for |
||
248 | * @param RankingSystemInterface $ranking the ranking system to search for |
||
249 | * @param PlayerInterface $player the player to search for |
||
250 | * @return RankingSystemChangeInterface the found or newly created ranking system change |
||
251 | */ |
||
252 | protected final function getOrCreateChange(TournamentHierarchyInterface $entity, RankingSystemInterface $ranking, |
||
299 | |||
300 | /** @noinspection PhpDocMissingThrowsInspection */ //PropertyNotExistingException |
||
301 | /** |
||
302 | * @param RankingSystemListInterface $list the list in which to search for the entry or in which to add it |
||
303 | * @param PlayerInterface $player the player to search for |
||
304 | * @return RankingSystemListEntryInterface the found or the new entry |
||
305 | */ |
||
306 | protected final function getOrCreateRankingSystemListEntry(RankingSystemListInterface $list, |
||
326 | //</editor-fold desc="Protected Final Methods"> |
||
327 | |||
328 | //<editor-fold desc="Protected Methods"> |
||
329 | /** |
||
330 | * Gets additional fields for this ranking type mapped to its start value |
||
331 | * @return string[] list of additional fields |
||
332 | */ |
||
333 | protected abstract function getAdditionalFields(): array; |
||
334 | |||
335 | /** |
||
336 | * Gets all ranking changes for the given entity for the given list. Must return a change for each involved player. |
||
337 | * The field pointsAfterwards get calculated afterwards and can be left empty. |
||
338 | * @param TournamentHierarchyEntity $entity the entity for which to compute the ranking changes |
||
339 | * @param RankingSystemListInterface $list the list for which to compute the ranking changes |
||
340 | * @return RankingSystemChangeInterface[] the changes |
||
341 | */ |
||
342 | protected abstract function getChanges(TournamentHierarchyEntity $entity, RankingSystemListInterface $list): array; |
||
343 | |||
344 | /** |
||
345 | * Gets a query for getting the relevant entities for updating |
||
346 | * @param RankingSystemInterface $ranking the ranking for which to get the entities |
||
347 | * @param \DateTime $from search for entities with a time value LARGER than $from, i.e. don't search for entities with |
||
348 | * time value exactly $from |
||
349 | * @return QueryBuilder |
||
350 | */ |
||
351 | protected abstract function getEntitiesQueryBuilder(RankingSystemInterface $ranking, |
||
353 | |||
354 | /** |
||
355 | * Gets the level of the ranking system service (see Level Enum) |
||
356 | * @return int |
||
357 | */ |
||
358 | protected abstract function getLevel(): int; |
||
359 | |||
360 | /** |
||
361 | * Gets the start points for a new player in the ranking |
||
362 | * @return float |
||
363 | */ |
||
364 | protected function startPoints(): float |
||
368 | //</editor-fold desc="Protected Methods"> |
||
369 | |||
370 | //<editor-fold desc="Private Methods"> |
||
371 | /** |
||
372 | * Clones all ranking values from base and inserts them into list, furthermore removes all remaining ranking values of |
||
373 | * list. After this method was called list and base contain exactly the same rankings. |
||
374 | * @param RankingSystemListInterface $list the ranking list to change |
||
375 | * @param RankingSystemListInterface $base the ranking list to use as base list, this doesn't get changed |
||
376 | */ |
||
377 | private function cloneInto(RankingSystemListInterface $list, RankingSystemListInterface $base) |
||
414 | |||
415 | |||
416 | /** |
||
417 | * @param RankingSystemInterface $ranking |
||
418 | * @param TournamentHierarchyEntity[] $entities |
||
419 | */ |
||
420 | private function deleteOldChanges(RankingSystemInterface $ranking, array $entities) |
||
438 | |||
439 | /** |
||
440 | * Gets the earliest influence for the given entity |
||
441 | * @param RankingSystemInterface $ranking the ranking system for which to get the influence |
||
442 | * @param TournamentHierarchyInterface $entity the entity to analyze |
||
443 | * @param bool $parentIsRanked true iff a predecessor contained the given ranking in its ranking systems |
||
444 | * @return \DateTime|null the earliest influence or null if $parentIsRanked is false and the entity and all its |
||
445 | * successors do not have the ranking in its ranking systems |
||
446 | */ |
||
447 | private function getEarliestEntityInfluence(RankingSystemInterface $ranking, TournamentHierarchyInterface $entity, |
||
469 | |||
470 | /** |
||
471 | * @param \DateTime $time the time of the last list |
||
472 | * @param int $generationLevel the list generation level |
||
473 | * @return \DateTime the time of the next list generation |
||
474 | */ |
||
475 | private function getNextGenerationTime(\DateTime $time, int $generationLevel): \DateTime |
||
492 | |||
493 | /** @noinspection PhpDocMissingThrowsInspection */ //PropertyNotExistingException |
||
494 | /** |
||
495 | * Recomputes the given ranking list by using base as base list and applying the changes for the given entities |
||
496 | * starting from the given index. If list is not the current list only the entities up to $list->getLastEntryTime() |
||
497 | * are applied and the index gets changed accordingly. |
||
498 | * @param RankingSystemListInterface $list the list to recompute |
||
499 | * @param RankingSystemListInterface $base the list to use as base |
||
500 | * @param TournamentHierarchyEntity[] $entities the list of entities to use for the computation |
||
501 | * @param int $nextEntityIndex the first index in the entities list to consider |
||
502 | * @param \DateTime $lastListTime the time of the last list or the first entry |
||
503 | */ |
||
504 | private function recomputeBasedOn(RankingSystemListInterface $list, RankingSystemListInterface $base, array $entities, |
||
547 | //</editor-fold desc="Private Methods"> |
||
548 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: