GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( dec958...1175d2 )
by Jacky
32s
created

RankingManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
nc 1
nop 8
dl 0
loc 20
rs 9.4285
c 1
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Asylamba\Modules\Atlas\Manager;
4
5
use Asylamba\Classes\Entity\EntityManager;
6
7
use Asylamba\Modules\Demeter\Manager\ColorManager;
8
use Asylamba\Modules\Hermes\Manager\ConversationManager;
9
use Asylamba\Modules\Hermes\Manager\ConversationMessageManager;
10
11
use Asylamba\Modules\Atlas\Routine\PlayerRoutine;
12
use Asylamba\Modules\Atlas\Routine\FactionRoutine;
13
14
use Asylamba\Modules\Hermes\Model\ConversationUser;
15
use Asylamba\Modules\Hermes\Model\ConversationMessage;
16
use Asylamba\Modules\Demeter\Resource\ColorResource;
17
use Asylamba\Modules\Athena\Helper\OrbitalBaseHelper;
18
use Asylamba\Modules\Zeus\Manager\PlayerManager;
19
20
use Asylamba\Modules\Zeus\Model\Player;
21
use Asylamba\Modules\Atlas\Model\PlayerRanking;
22
use Asylamba\Modules\Atlas\Model\FactionRanking;
23
use Asylamba\Modules\Atlas\Model\Ranking;
24
use Asylamba\Modules\Gaia\Model\Sector;
25
26
use Asylamba\Classes\Exception\ErrorException;
27
28
use Asylamba\Classes\Library\Utils;
29
30
class RankingManager
31
{
32
	/** @var EntityManager **/
33
	protected $entityManager;
34
	/** @var PlayerRankingManager **/
35
	protected $playerRankingManager;
36
	/** @var FactionRankingManager **/
37
	protected $factionRankingManager;
38
	/** @var ColorManager **/
39
	protected $colorManager;
40
	/** @var ConversationManager **/
41
	protected $conversationManager;
42
	/** @var ConversationMessageManager **/
43
	protected $conversationMessageManager;
44
	/** @var PlayerManager **/
45
	protected $playerManager;
46
	/** @var OrbitalBaseHelper **/
47
	protected $orbitalBaseHelper;
48
	
49
	/**
50
	 * @param EntityManager $entityManager
51
	 * @param PlayerRankingManager $playerRankingManager
52
	 * @param FactionRankingManager $factionRankingManager
53
	 * @param ColorManager $colorManager
54
	 * @param ConversationManager $conversationManager
55
	 * @param ConversationMessageManager $conversationMessageManager
56
	 * @param PlayerManager $playerManager
57
	 * @param OrbitalBaseHelper $orbitalBaseHelper
58
	 */
59
	public function __construct(
60
		EntityManager $entityManager,
61
		PlayerRankingManager $playerRankingManager,
62
		FactionRankingManager $factionRankingManager,
63
		ColorManager $colorManager,
64
		ConversationManager $conversationManager,
65
		ConversationMessageManager $conversationMessageManager,
66
		PlayerManager $playerManager,
67
		OrbitalBaseHelper $orbitalBaseHelper
68
	)
69
	{
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
70
		$this->entityManager = $entityManager;
71
		$this->playerRankingManager = $playerRankingManager;
72
		$this->factionRankingManager = $factionRankingManager;
73
		$this->colorManager = $colorManager;
74
		$this->conversationManager = $conversationManager;
75
		$this->conversationMessageManager = $conversationMessageManager;
76
		$this->playerManager = $playerManager;
77
		$this->orbitalBaseHelper = $orbitalBaseHelper;
78
	}
79
	
80
	public function processPlayersRanking()
81
	{
82
		if ($this->entityManager->getRepository(Ranking::class)->hasBeenAlreadyProcessed(true, false) === true) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Asylamba\Classes\Entity\AbstractRepository as the method hasBeenAlreadyProcessed() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Atlas\R...itory\RankingRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
83
			return;
84
		}
85
		$playerRoutine = new PlayerRoutine();
86
		
87
		$players = $this->playerManager->getByStatements([Player::ACTIVE, Player::INACTIVE, Player::HOLIDAY]);
88
		
89
		$playerRankingRepository = $this->entityManager->getRepository(PlayerRanking::class);
90
		
91
		$ranking = $this->createRanking(true, false);
92
		
93
		$playerRoutine->execute(
94
			$players,
95
			$playerRankingRepository->getPlayersResources(),
96
			$playerRankingRepository->getPlayersResourcesData(),
97
			$playerRankingRepository->getPlayersGeneralData(),
98
			$playerRankingRepository->getPlayersArmiesData(),
99
			$playerRankingRepository->getPlayersPlanetData(),
100
			$playerRankingRepository->getPlayersTradeRoutes(),
101
			$playerRankingRepository->getPlayersLinkedTradeRoutes(),
102
			$playerRankingRepository->getAttackersButcherRanking(),
103
			$playerRankingRepository->getDefendersButcherRanking(),
104
			$this->orbitalBaseHelper
105
		);
106
		
107
		$S_PRM1 = $this->playerRankingManager->getCurrentSession();
108
		$this->playerRankingManager->newSession();
109
		$this->playerRankingManager->loadLastContext();
110
111
		$playerRoutine->processResults($ranking, $players, $this->playerRankingManager, $playerRankingRepository);
0 ignored issues
show
Compatibility introduced by
$playerRankingRepository of type object<Asylamba\Classes\...ity\AbstractRepository> is not a sub-type of object<Asylamba\Modules\...layerRankingRepository>. It seems like you assume a child class of the class Asylamba\Classes\Entity\AbstractRepository to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
112
		
113
		$this->playerRankingManager->changeSession($S_PRM1);
114
		
115
		$this->entityManager->flush();
116
	}
117
	
118
	public function processFactionsRanking()
119
	{
120
		if ($this->entityManager->getRepository(Ranking::class)->hasBeenAlreadyProcessed(false, true) === true) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Asylamba\Classes\Entity\AbstractRepository as the method hasBeenAlreadyProcessed() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Atlas\R...itory\RankingRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
121
			return;
122
		}
123
		$factionRoutine = new FactionRoutine();
124
		
125
		$factions = $this->colorManager->getInGameFactions();
126
		$playerRankingRepository = $this->entityManager->getRepository(PlayerRanking::class);
127
		$factionRankingRepository = $this->entityManager->getRepository(FactionRanking::class);
128
		$sectors = $this->entityManager->getRepository(Sector::class)->getAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Asylamba\Classes\Entity\AbstractRepository as the method getAll() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Athena\...BuildingQueueRepository, Asylamba\Modules\Athena\...rcialShippingRepository, Asylamba\Modules\Athena\...y\OrbitalBaseRepository, Asylamba\Modules\Athena\...yclingMissionRepository, Asylamba\Modules\Athena\...ory\ShipQueueRepository, Asylamba\Modules\Demeter...ository\ColorRepository, Asylamba\Modules\Gaia\Repository\SectorRepository, Asylamba\Modules\Gaia\Repository\SystemRepository, Asylamba\Modules\Prometh...chnologyQueueRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
129
		
130
		$ranking = $this->createRanking(false, true);
131
		
132
		foreach ($factions as $faction) {
133
			$this->colorManager->updateInfos($faction);
134
			
135
			$routesIncome = $factionRankingRepository->getRoutesIncome($faction);
136
			$playerRankings = $playerRankingRepository->getFactionPlayerRankings($faction);
137
			
138
			$factionRoutine->execute($faction, $playerRankings, $routesIncome, $sectors);
139
		}
140
		
141
		$S_FRM1 = $this->factionRankingManager->getCurrentSession();
142
		$this->factionRankingManager->newSession();
143
		$this->factionRankingManager->loadLastContext();
144
		
145
		$winningFactionId = $factionRoutine->processResults($ranking, $factions, $this->factionRankingManager);
146
147
		$this->factionRankingManager->changeSession($S_FRM1);
148
		
149
		if ($winningFactionId !== null) {
150
			$this->processWinningFaction($winningFactionId);
151
		}
152
		$this->entityManager->flush();
153
	}
154
	
155
	protected function processWinningFaction($factionId)
156
	{
157
		$faction = $this->colorManager->get($factionId);
158
		$faction->isWinner = Color::WIN;
159
160
		# envoyer un message de Jean-Mi
161
		$winnerName = ColorResource::getInfo($faction->id, 'officialName');
162
		$content = 'Salut,<br /><br />La victoire vient d\'être remportée par : <br /><strong>' . $winnerName . '</strong><br />';
163
		$content .= 'Cette faction a atteint les ' . POINTS_TO_WIN . ' points, la partie est donc terminée.<br /><br />Bravo et un grand merci à tous les participants !';
164
165
		$S_CVM1 = $this->conversationManager->getCurrentSession();
166
		$this->conversationManager->newSession();
167
		$this->conversationManager->load(
168
			['cu.rPlayer' => ID_JEANMI]
169
		);
170
171
		if ($this->conversationManager->size() == 1) {
172
			$conv = $this->conversationManager->get();
173
174
			$conv->messages++;
175
			$conv->dLastMessage = Utils::now();
176
177
			# désarchiver tous les users
178
			$users = $conv->players;
179
			foreach ($users as $user) {
180
				$user->convStatement = ConversationUser::CS_DISPLAY;
181
			}
182
183
			# création du message
184
			$message = new ConversationMessage();
185
186
			$message->rConversation = $conv->id;
187
			$message->rPlayer = ID_JEANMI;
188
			$message->type = ConversationMessage::TY_STD;
189
			$message->content = $content;
0 ignored issues
show
Documentation Bug introduced by
The property $content was declared of type integer, but $content is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
190
			$message->dCreation = Utils::now();
0 ignored issues
show
Documentation Bug introduced by
The property $dCreation was declared of type integer, but \Asylamba\Classes\Library\Utils::now() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
191
			$message->dLastModification = NULL;
192
193
			$this->conversationMessageManager->add($message);
194
		} else {
195
			throw new ErrorException('La conversation n\'existe pas ou ne vous appartient pas.');
196
		}
197
		$this->conversationManager->changeSession($S_CVM1);
198
	}
199
	
200
	protected function createRanking($isPlayer, $isFaction)
201
	{
202
		$ranking =
203
			(new Ranking())
204
			->setIsPlayer($isPlayer)
205
			->setIsFaction($isFaction)
206
			->setCreatedAt(Utils::now())
207
		;
208
		$this->entityManager->persist($ranking);
209
		$this->entityManager->flush($ranking);
210
		return $ranking;
211
	}
212
}