Complex classes like Dedimania 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 Dedimania, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class Dedimania implements StatusAwarePluginInterface, ListenerInterfaceExpTimer, ListenerInterfaceMpScriptMap, ListenerInterfaceMpScriptMatch, ListenerInterfaceRaceData, ListenerInterfaceMpLegacyPlayer |
||
| 43 | { |
||
| 44 | const dedimaniaUrl = "http://dedimania.net:8081/Dedimania"; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var DedimaniaService |
||
| 48 | */ |
||
| 49 | private $dedimaniaService; |
||
| 50 | /** |
||
| 51 | * @var ConfigInterface |
||
| 52 | */ |
||
| 53 | private $enabled; |
||
| 54 | /** |
||
| 55 | * @var ConfigInterface |
||
| 56 | */ |
||
| 57 | private $apikey; |
||
| 58 | /** |
||
| 59 | * @var ConfigInterface |
||
| 60 | */ |
||
| 61 | private $serverLogin; |
||
| 62 | |||
| 63 | private $read = []; |
||
| 64 | private $write = []; |
||
| 65 | private $except = []; |
||
| 66 | |||
| 67 | /** @var \Webaccess */ |
||
| 68 | protected $webaccess; |
||
| 69 | /** |
||
| 70 | * @var Console |
||
| 71 | */ |
||
| 72 | private $console; |
||
| 73 | /** @var int */ |
||
| 74 | private $tryTimer = -1; |
||
| 75 | |||
| 76 | /** @var int */ |
||
| 77 | private $lastUpdate; |
||
| 78 | /** @var string|null */ |
||
| 79 | private $sessionId = null; |
||
| 80 | private $titles; |
||
| 81 | /** |
||
| 82 | * @var Connection |
||
| 83 | */ |
||
| 84 | private $connection; |
||
|
|
|||
| 85 | /** |
||
| 86 | * @var GameDataStorage |
||
| 87 | */ |
||
| 88 | private $gameDataStorage; |
||
| 89 | /** |
||
| 90 | * @var MapStorage |
||
| 91 | */ |
||
| 92 | private $mapStorage; |
||
| 93 | /** |
||
| 94 | * @var PlayerStorage |
||
| 95 | */ |
||
| 96 | private $playerStorage; |
||
| 97 | /** |
||
| 98 | * @var Notifications |
||
| 99 | */ |
||
| 100 | private $notifications; |
||
| 101 | /** |
||
| 102 | * @var Time |
||
| 103 | */ |
||
| 104 | private $time; |
||
| 105 | /** |
||
| 106 | * @var GetScores |
||
| 107 | */ |
||
| 108 | private $getScores; |
||
| 109 | /** |
||
| 110 | * @var FileSystem |
||
| 111 | */ |
||
| 112 | private $fileSystem; |
||
| 113 | |||
| 114 | /** @var ChatNotification */ |
||
| 115 | private $chatNotification; |
||
| 116 | |||
| 117 | /** @var Factory */ |
||
| 118 | private $factory; |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * Dedimania constructor. |
||
| 123 | * @param $titles |
||
| 124 | * @param ConfigInterface $enabled |
||
| 125 | * @param ConfigInterface $apikey |
||
| 126 | * @param ConfigInterface $serverLogin |
||
| 127 | * @param DedimaniaService $dedimaniaService |
||
| 128 | * @param Console $console |
||
| 129 | * @param Factory $connectionFactory |
||
| 130 | * @param GameDataStorage $gameDataStorage |
||
| 131 | * @param MapStorage $mapStorage |
||
| 132 | * @param PlayerStorage $playerStorage |
||
| 133 | * @param Notifications $notifications |
||
| 134 | * @param Time $time |
||
| 135 | * @param GetScores $getScores |
||
| 136 | * @param FileSystem $fileSystem |
||
| 137 | * @param ChatNotification $chatNotification |
||
| 138 | */ |
||
| 139 | public function __construct( |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Set the status of the plugin |
||
| 177 | * |
||
| 178 | * @param boolean $status |
||
| 179 | * |
||
| 180 | * @return void |
||
| 181 | * @throws \Exception |
||
| 182 | */ |
||
| 183 | public function setStatus($status) |
||
| 194 | |||
| 195 | |||
| 196 | /** @api */ |
||
| 197 | public function onPreLoop() |
||
| 201 | |||
| 202 | /** @api */ |
||
| 203 | public function onPostLoop() |
||
| 220 | |||
| 221 | /** @api */ |
||
| 222 | public function onEverySecond() |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Send a request to Dedimania |
||
| 238 | * |
||
| 239 | * @param Request $request |
||
| 240 | * @param callable $callback |
||
| 241 | */ |
||
| 242 | final public function sendRequest(Request $request, $callback) |
||
| 256 | |||
| 257 | final public function process($response, $callback) |
||
| 329 | |||
| 330 | //#region public dedimania methods |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $serverlogin |
||
| 334 | * @param string $apikey |
||
| 335 | * @throws \Exception |
||
| 336 | * |
||
| 337 | */ |
||
| 338 | public function openSession($serverlogin, $apikey) |
||
| 371 | |||
| 372 | public function getRecords() |
||
| 436 | |||
| 437 | public function updateServerPlayers() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param DedicatedPlayer $player |
||
| 459 | */ |
||
| 460 | public function connectPlayer(DedicatedPlayer $player) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param DedicatedPlayer $player |
||
| 488 | */ |
||
| 489 | public function disconnectPlayer(DedicatedPlayer $player) |
||
| 504 | |||
| 505 | public function connectAllPlayers() |
||
| 539 | |||
| 540 | /* |
||
| 541 | */ |
||
| 542 | |||
| 543 | public function setRecords() |
||
| 593 | |||
| 594 | //endregion |
||
| 595 | //#region protected helper functions |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @param string $top1Login |
||
| 599 | * @param array $bestCheckpoints |
||
| 600 | * @return array |
||
| 601 | */ |
||
| 602 | protected function getReplays($top1Login, $bestCheckpoints) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Sets new Ghost replay for the map |
||
| 625 | * @param $login |
||
| 626 | */ |
||
| 627 | protected function setGReplay($login) |
||
| 646 | |||
| 647 | protected function getVotesInfo() |
||
| 656 | |||
| 657 | protected function getMapInfo() |
||
| 670 | |||
| 671 | |||
| 672 | protected function getPlayers() |
||
| 684 | |||
| 685 | protected function getServerInfo() |
||
| 697 | |||
| 698 | protected function getGameMode() |
||
| 713 | |||
| 714 | protected function getPackMask($titleId) |
||
| 724 | |||
| 725 | //#endregion |
||
| 726 | //#region Dedicated server callbacks |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Callback sent when the "StartMap" section start. |
||
| 730 | * |
||
| 731 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 732 | * @param int $time Server time when the callback was sent |
||
| 733 | * @param boolean $restarted true if the map was restarted, false otherwise |
||
| 734 | * @param Map $map Map started with. |
||
| 735 | * |
||
| 736 | * @return void |
||
| 737 | */ |
||
| 738 | public function onStartMapStart($count, $time, $restarted, Map $map) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Callback sent when the "EndMap" section start. |
||
| 748 | * |
||
| 749 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 750 | * @param int $time Server time when the callback was sent |
||
| 751 | * @param boolean $restarted true if the map was restarted, false otherwise |
||
| 752 | * @param Map $map Map started with. |
||
| 753 | * |
||
| 754 | * @return void |
||
| 755 | */ |
||
| 756 | public function onEndMapStart($count, $time, $restarted, Map $map) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @param string $login Login of the player that crossed the CP point |
||
| 765 | * @param int $time Server time when the event occured, |
||
| 766 | * @param int $raceTime Total race time in milliseconds |
||
| 767 | * @param int $stuntsScore Stunts score |
||
| 768 | * @param int $cpInRace Number of checkpoints crossed since the beginning of the race |
||
| 769 | * @param int[] $curCps Checkpoints times since the beginning of the race |
||
| 770 | * @param string $blockId Id of the checkpoint block |
||
| 771 | * @param string $speed Speed of the player in km/h |
||
| 772 | * @param string $distance Distance traveled by the player |
||
| 773 | */ |
||
| 774 | public function onPlayerEndRace( |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Callback sent when the "StartMap" section end. |
||
| 802 | * |
||
| 803 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 804 | * @param int $time Server time when the callback was sent |
||
| 805 | * @param boolean $restarted true if the map was restarted, false otherwise |
||
| 806 | * @param Map $map Map started with. |
||
| 807 | * |
||
| 808 | * @return void |
||
| 809 | */ |
||
| 810 | public function onStartMapEnd($count, $time, $restarted, Map $map) |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Callback sent when the "EndMatch" section start. |
||
| 817 | * |
||
| 818 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 819 | * @param int $time Server time when the callback was sent |
||
| 820 | * |
||
| 821 | * @return void |
||
| 822 | */ |
||
| 823 | public function onEndMatchStart($count, $time) |
||
| 827 | |||
| 828 | |||
| 829 | /** |
||
| 830 | * Callback sent when the "EndMap" section end. |
||
| 831 | * |
||
| 832 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 833 | * @param int $time Server time when the callback was sent |
||
| 834 | * @param boolean $restarted true if the map was restarted, false otherwise |
||
| 835 | * @param Map $map Map started with. |
||
| 836 | * |
||
| 837 | * @return void |
||
| 838 | */ |
||
| 839 | public function onEndMapEnd($count, $time, $restarted, Map $map) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Callback sent when the "StartMatch" section start. |
||
| 846 | * |
||
| 847 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 848 | * @param int $time Server time when the callback was sent |
||
| 849 | * |
||
| 850 | * @return void |
||
| 851 | */ |
||
| 852 | public function onStartMatchStart($count, $time) |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Callback sent when the "StartMatch" section end. |
||
| 859 | * |
||
| 860 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 861 | * @param int $time Server time when the callback was sent |
||
| 862 | * |
||
| 863 | * @return void |
||
| 864 | */ |
||
| 865 | public function onStartMatchEnd($count, $time) |
||
| 869 | |||
| 870 | |||
| 871 | /** |
||
| 872 | * Callback sent when the "EndMatch" section end. |
||
| 873 | * |
||
| 874 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 875 | * @param int $time Server time when the callback was sent |
||
| 876 | * |
||
| 877 | * @return void |
||
| 878 | */ |
||
| 879 | public function onEndMatchEnd($count, $time) |
||
| 883 | |||
| 884 | /** |
||
| 885 | * Callback sent when the "StartTurn" section start. |
||
| 886 | * |
||
| 887 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 888 | * @param int $time Server time when the callback was sent |
||
| 889 | * |
||
| 890 | * @return void |
||
| 891 | */ |
||
| 892 | public function onStartTurnStart($count, $time) |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Callback sent when the "StartTurn" section end. |
||
| 899 | * |
||
| 900 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 901 | * @param int $time Server time when the callback was sent |
||
| 902 | * |
||
| 903 | * @return void |
||
| 904 | */ |
||
| 905 | public function onStartTurnEnd($count, $time) |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Callback sent when the "EndMatch" section start. |
||
| 912 | * |
||
| 913 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 914 | * @param int $time Server time when the callback was sent |
||
| 915 | * |
||
| 916 | * @return void |
||
| 917 | */ |
||
| 918 | public function onEndTurnStart($count, $time) |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Callback sent when the "EndMatch" section end. |
||
| 925 | * |
||
| 926 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 927 | * @param int $time Server time when the callback was sent |
||
| 928 | * |
||
| 929 | * @return void |
||
| 930 | */ |
||
| 931 | public function onEndTurnEnd($count, $time) |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Callback sent when the "StartRound" section start. |
||
| 938 | * |
||
| 939 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 940 | * @param int $time Server time when the callback was sent |
||
| 941 | * |
||
| 942 | * @return void |
||
| 943 | */ |
||
| 944 | public function onStartRoundStart($count, $time) |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Callback sent when the "StartRound" section end. |
||
| 951 | * |
||
| 952 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 953 | * @param int $time Server time when the callback was sent |
||
| 954 | * |
||
| 955 | * @return void |
||
| 956 | */ |
||
| 957 | public function onStartRoundEnd($count, $time) |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Callback sent when the "EndMatch" section start. |
||
| 964 | * |
||
| 965 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 966 | * @param int $time Server time when the callback was sent |
||
| 967 | * |
||
| 968 | * @return void |
||
| 969 | */ |
||
| 970 | public function onEndRoundStart($count, $time) |
||
| 974 | |||
| 975 | /** |
||
| 976 | * Callback sent when the "EndMatch" section end. |
||
| 977 | * |
||
| 978 | * @param int $count Each time this section is played, this number is incremented by one |
||
| 979 | * @param int $time Server time when the callback was sent |
||
| 980 | * |
||
| 981 | * @return void |
||
| 982 | */ |
||
| 983 | public function onEndRoundEnd($count, $time) |
||
| 987 | |||
| 988 | |||
| 989 | /** |
||
| 990 | * @param DedicatedPlayer $player |
||
| 991 | * @return void |
||
| 992 | */ |
||
| 993 | public function onPlayerConnect(DedicatedPlayer $player) |
||
| 997 | |||
| 998 | /** |
||
| 999 | * @param DedicatedPlayer $player |
||
| 1000 | * @param string $disconnectionReason |
||
| 1001 | * @return void |
||
| 1002 | */ |
||
| 1003 | public function onPlayerDisconnect(DedicatedPlayer $player, $disconnectionReason) |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * @param DedicatedPlayer $oldPlayer |
||
| 1010 | * @param DedicatedPlayer $player |
||
| 1011 | * @return void |
||
| 1012 | */ |
||
| 1013 | public function onPlayerInfoChanged( |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * @param DedicatedPlayer $oldPlayer |
||
| 1022 | * @param DedicatedPlayer $player |
||
| 1023 | * @return void |
||
| 1024 | */ |
||
| 1025 | public function onPlayerAlliesChanged( |
||
| 1031 | } |
This check marks private properties in classes that are never used. Those properties can be removed.