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.

CommanderManager::getIncomingCommanders()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Commander Manager
5
 *
6
 * @author Noé Zufferey
7
 * @copyright Expansion - le jeu
8
 *
9
 * @package Arès
10
 * @update 20.05.13
11
*/
12
// !! lors d'un load, mettre c. avant les attribut where
13
14
namespace Asylamba\Modules\Ares\Manager;
15
16
use Asylamba\Classes\Entity\EntityManager;
17
use Asylamba\Classes\Library\Utils;
18
use Asylamba\Classes\Library\Game;
19
use Asylamba\Modules\Athena\Manager\OrbitalBaseManager;
20
use Asylamba\Modules\Zeus\Manager\PlayerManager;
21
use Asylamba\Modules\Zeus\Manager\PlayerBonusManager;
22
use Asylamba\Modules\Gaia\Manager\PlaceManager;
23
use Asylamba\Modules\Demeter\Manager\ColorManager;
24
use Asylamba\Modules\Hermes\Manager\NotificationManager;
25
use Asylamba\Classes\Container\ArrayList;
26
27
use Asylamba\Classes\Worker\EventDispatcher;
28
29
use Asylamba\Modules\Athena\Model\OrbitalBase;
30
use Asylamba\Modules\Gaia\Model\Place;
31
use Asylamba\Modules\Ares\Model\Report;
32
use Asylamba\Modules\Ares\Model\LiveReport;
33
use Asylamba\Modules\Zeus\Model\PlayerBonus;
34
use Asylamba\Modules\Ares\Model\Commander;
35
use Asylamba\Modules\Gaia\Resource\SquadronResource;
36
use Asylamba\Modules\Demeter\Model\Color;
37
38
use Asylamba\Classes\Scheduler\RealTimeActionScheduler;
39
40
use Asylamba\Modules\Gaia\Event\PlaceOwnerChangeEvent;
41
42
class CommanderManager
43
{
44
	/** @var EntityManager **/
45
	protected $entityManager;
46
	/** @var FightManager **/
47
	protected $fightManager;
48
	/** @var ReportManager **/
49
	protected $reportManager;
50
	/** @var OrbitalBaseManager **/
51
	protected $orbitalBaseManager;
52
	/** @var PlayerManager **/
53
	protected $playerManager;
54
	/** @var PlayerBonusManager **/
55
	protected $playerBonusManager;
56
	/** @var PlaceManager **/
57
	protected $placeManager;
58
	/** @var ColorManager **/
59
	protected $colorManager;
60
	/** @var NotificationManager **/
61
	protected $notificationManager;
62
	/** @var RealTimeActionScheduler **/
63
	protected $realtimeActionScheduler;
64
	/** @var EventDispatcher **/
65
	protected $eventDispatcher;
66
	/** @var int **/
67
	protected $commanderBaseLevel;
68
	
69
	protected $actions = [
70
		Commander::MOVE => 'uChangeBase',
71
		Commander::LOOT => 'uLoot',
72
		Commander::COLO => 'uConquer',
73
		Commander::BACK => 'uReturnBase'
74
	];
75
76
	/**
77
	 * @param EntityManager $entityManager
78
	 * @param FightManager $fightManager
79
	 * @param ReportManager $reportManager
80
	 * @param OrbitalBaseManager $orbitalBaseManager
81
	 * @param PlayerManager $playerManager
82
	 * @param PlayerBonusManager $playerBonusManager
83
	 * @param PlaceManager $placeManager
84
	 * @param ColorManager $colorManager
85
	 * @param NotificationManager $notificationManager
86
	 * @param RealTimeActionScheduler $realtimeActionScheduler
87
	 * @param EventDispatcher $eventDispatcher
88
	 * @param int $commanderBaseLevel
89
	 */
90
	public function __construct(
91
		EntityManager $entityManager,
92
		FightManager $fightManager,
93
		ReportManager $reportManager,
94
		OrbitalBaseManager $orbitalBaseManager,
95
		PlayerManager $playerManager,
96
		PlayerBonusManager $playerBonusManager,
97
		PlaceManager $placeManager,
98
		ColorManager $colorManager,
99
		NotificationManager $notificationManager,
100
		RealTimeActionScheduler $realtimeActionScheduler,
101
		EventDispatcher $eventDispatcher,
102
		$commanderBaseLevel
103
	) {
104
		$this->entityManager = $entityManager;
105
		$this->fightManager = $fightManager;
106
		$this->reportManager = $reportManager;
107
		$this->orbitalBaseManager = $orbitalBaseManager;
108
		$this->playerManager = $playerManager;
109
		$this->playerBonusManager = $playerBonusManager;
110
		$this->placeManager = $placeManager;
111
		$this->colorManager = $colorManager;
112
		$this->notificationManager = $notificationManager;
113
		$this->realtimeActionScheduler = $realtimeActionScheduler;
114
		$this->eventDispatcher = $eventDispatcher;
115
		$this->commanderBaseLevel = $commanderBaseLevel;
116
	}
117
	
118
	/**
119
	 * @param integer $id
120
	 * @return Commander
121
	 */
122
	public function get($id)
123
	{
124
		return $this->entityManager->getRepository(Commander::class)->get($id);
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 get() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository, Asylamba\Modules\Ares\Re...ry\LiveReportRepository, Asylamba\Modules\Ares\Repository\ReportRepository, Asylamba\Modules\Athena\...BuildingQueueRepository, Asylamba\Modules\Athena\...mmercialRouteRepository, Asylamba\Modules\Athena\...rcialShippingRepository, Asylamba\Modules\Athena\...y\OrbitalBaseRepository, Asylamba\Modules\Athena\...yclingMissionRepository, Asylamba\Modules\Athena\...ory\ShipQueueRepository, Asylamba\Modules\Athena\...y\TransactionRepository, Asylamba\Modules\Demeter...ository\ColorRepository, Asylamba\Modules\Demeter...ion\CandidateRepository, Asylamba\Modules\Demeter...tion\ElectionRepository, Asylamba\Modules\Demeter...m\FactionNewsRepository, Asylamba\Modules\Demeter...itory\Law\LawRepository, Asylamba\Modules\Gaia\Repository\PlaceRepository, Asylamba\Modules\Gaia\Repository\SectorRepository, Asylamba\Modules\Gaia\Repository\SystemRepository, Asylamba\Modules\Hermes\...\NotificationRepository, Asylamba\Modules\Prometh...chnologyQueueRepository, Asylamba\Modules\Zeus\Repository\PlayerRepository. 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...
125
	}
126
	
127
	/**
128
	 * @param integer $orbitalBaseId
129
	 * @param array $statements
130
	 * @return array
131
	 */
132
	public function getBaseCommanders($orbitalBaseId, $statements = [], $orderBy = [])
133
	{
134
		return $this->entityManager->getRepository(Commander::class)->getBaseCommanders($orbitalBaseId, $statements, $orderBy);
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 getBaseCommanders() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository. 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...
135
	}
136
	
137
	/**
138
	 * @param int $playerId
139
	 * @param array $statements
140
	 * @return array
141
	 */
142
	public function getPlayerCommanders($playerId, $statements = [], $orderBy = [])
143
	{
144
		return $this->entityManager->getRepository(Commander::class)->getPlayerCommanders($playerId, $statements, $orderBy);
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 getPlayerCommanders() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository. 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...
145
	}
146
	
147
	/**
148
	 * @return array
149
	 */
150
	public function getMovingCommanders()
151
	{
152
		return $this->entityManager->getRepository(Commander::class)->getMovingCommanders();
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 getMovingCommanders() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository. 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...
153
	}
154
	
155
	/**
156
	 * @param array $ids
157
	 * @return array
158
	 */
159
	public function getCommandersByIds($ids)
160
	{
161
		return $this->entityManager->getRepository(Commander::class)->getCommandersByIds($ids);
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 getCommandersByIds() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository. 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...
162
	}
163
	
164
	/**
165
	 * @param int $orbitalBaseId
166
	 * @param int $line
167
	 * @return array
168
	 */
169
	public function getCommandersByLine($orbitalBaseId, $line)
170
	{
171
		return $this->entityManager->getRepository(Commander::class)->getCommandersByLine($orbitalBaseId, $line);
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 getCommandersByLine() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository. 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...
172
	}
173
	
174
	/**
175
	 * @param int $playerId
176
	 * @return array
177
	 */
178
	public function getIncomingAttacks($playerId)
179
	{
180
		return $this->entityManager->getRepository(Commander::class)->getIncomingAttacks($playerId);
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 getIncomingAttacks() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository. 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...
181
	}
182
	
183
	/**
184
	 * @param int $playerId
185
	 * @return array
186
	 */
187
	public function getVisibleIncomingAttacks($playerId)
188
	{
189
		$attackingCommanders = $this->getIncomingAttacks($playerId) ;
190
		$incomingCommanders = [] ;
191
		
192
		foreach ($attackingCommanders as $commander) { 
193
			# va chercher les heures auxquelles il rentre dans les cercles d'espionnage
194
			$startPlace = $this->placeManager->get($commander->getRBase());
195
			$destinationPlace = $this->placeManager->get($commander->getRPlaceDestination());
196
			$times = Game::getAntiSpyEntryTime($startPlace, $destinationPlace, $commander->getArrivalDate());
197
198
			if (strtotime(Utils::now()) >= strtotime($times[0])) {
199
				# ajout de l'événement
200
				$incomingCommanders[] = $commander;
201
			}
202
		}		
203
		return $incomingCommanders;
204
	}
205
	
206
	/**
207
	 * @param int $playerId
208
	 * @return array
209
	 */
210
	public function getOutcomingAttacks($playerId)
211
	{
212
		return $this->entityManager->getRepository(Commander::class)->getOutcomingAttacks($playerId);
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 getOutcomingAttacks() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository. 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...
213
	}
214
	
215
	/**
216
	 * @param array $place
217
	 * @return array
218
	 */
219
	public function getIncomingCommanders($place)
220
	{
221
		return $this->entityManager->getRepository(Commander::class)->getIncomingCommanders($place);
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 getIncomingCommanders() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository. 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...
222
	}
223
	
224
	public function scheduleMovements()
225
	{
226
		$commanders = $this->getMovingCommanders();
227
		foreach ($commanders as $commander) {
228
			$this->realtimeActionScheduler->schedule(
229
				'ares.commander_manager',
230
				$this->actions[$commander->getTravelType()],
231
				$commander,
232
				$commander->dArrival,
233
				[
234
					'class' => Place::class,
235
					'id' => $commander->getRPlaceDestination()
236
				]
237
			);
238
		}
239
	}
240
	
241
	/**
242
	 * @param int $orbitalBaseId
243
	 * @param int $line
244
	 * @return int
245
	 */
246
	public function countCommandersByLine($orbitalBaseId, $line)
247
	{
248
		return $this->entityManager->getRepository(Commander::class)->countCommandersByLine($orbitalBaseId, $line);
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 countCommandersByLine() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository. 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...
249
	}
250
251
	public function setEarnedExperience(Commander $commander, Commander $enemyCommander) {
252
		$commander->setArmy();
253
		$finalOwnPev = 0;
254
255
		foreach ($commander->army as $squadron) {
256
			foreach ($squadron->getSquadron() as $ship) {
257
				$finalOwnPev += $ship->getPev();
258
			}
259
		}
260
		$importance = (($finalOwnPev + 1) * ($enemyCommander->getPevInBegin())) / 
261
			((($commander->pevInBegin + 1) * (($enemyCommander->getLevel() + 1) / 
262
				($commander->level + 1))));
263
264
		$commander->earnedExperience = $importance * Commander::COEFFEARNEDEXP;
0 ignored issues
show
Documentation Bug introduced by
It seems like $importance * \Asylamba\...mmander::COEFFEARNEDEXP can also be of type double. However, the property $earnedExperience is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
265
		if($commander->winner) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
266
			LiveReport::$importance = $importance;
0 ignored issues
show
Documentation Bug introduced by
It seems like $importance can also be of type double. However, the property $importance is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
267
		}
268
		
269
		if ($commander->rPlayer > 0) {
270
			$exp = round($commander->earnedExperience / Commander::COEFFEXPPLAYER);
271
			$this->playerManager->increaseExperience($this->playerManager->get($commander->rPlayer), $exp);
272
273
			if ($enemyCommander->isAttacker == TRUE) {
274
				LiveReport::$expPlayerD = $exp;
0 ignored issues
show
Documentation Bug introduced by
The property $expPlayerD was declared of type integer, but $exp is of type double. 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...
275
			} else {
276
				LiveReport::$expPlayerA = $exp;
0 ignored issues
show
Documentation Bug introduced by
The property $expPlayerA was declared of type integer, but $exp is of type double. 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...
277
			}
278
		}
279
	}
280
281
	public function setBonus(Commander $commander) {
282
		$commander->setArmy();
283
		$playerBonus = new PlayerBonus($commander->rPlayer);
0 ignored issues
show
Unused Code introduced by
The call to PlayerBonus::__construct() has too many arguments starting with $commander->rPlayer.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
284
		$playerBonus->load();
0 ignored issues
show
Bug introduced by
The method load() does not seem to exist on object<Asylamba\Modules\Zeus\Model\PlayerBonus>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
285
286
		foreach ($commander->army as $squadron) {
287
			foreach ($squadron->squadron as $ship) {
288
				$ship->setBonus($playerBonus->bonus);
289
			}
290
		}
291
	}
292
293
	public function upExperience(Commander $commander, $earnedExperience) {
294
		$commander->experience += $earnedExperience;
295
		$initialLevel = $commander->getLevel();
296
		
297
		while ($commander->experience >= $this->experienceToLevelUp($commander)) {
298
			$commander->setLevel($commander->getLevel() + 1);
299
		}
300
		$this->entityManager->getRepository(Commander::class)->updateExperience(
0 ignored issues
show
Bug introduced by
The method updateExperience() does not exist on Asylamba\Classes\Entity\AbstractRepository. Did you maybe mean update()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
301
			$commander,
302
			$earnedExperience,
303
			$commander->getLevel() - $initialLevel
304
		);
305
	}
306
307
	public function nbLevelUp($level, $newExperience) {
308
		$oLevel = $level;
309
		$nLevel = $level;
310
		while (1) {
311
			if ($newExperience >= (pow(2, $nLevel) * $this->commanderBaseLevel)) {
312
				$nLevel++;
313
			} else {
314
				break;
315
			}
316
		}
317
		return $nLevel - $oLevel;
318
	}
319
320
	public function experienceToLevelUp(Commander $commander) {
321
		return pow(2, $commander->level) * $this->commanderBaseLevel;
322
	}
323
324
	public function emptySquadrons(Commander $commander) {
325
		if (($orbitalBase = $this->orbitalBaseManager->get($commander->rBase)) === null) {
326
			return;
327
		}
328
		$nbSquadrons = count($commander->squadronsIds);
329
		for ($i = 0; $i < $nbSquadrons; ++$i) {
330
			for ($j = 0; $j < 12; $j++) {
331
				$orbitalBase->setShipStorage($j, $orbitalBase->getShipStorage($j) + $commander->getSquadron($i)->getNbrShipByType($j));
332
			}
333
			$commander->getSquadron($i)->emptySquadron();
334
		}
335
	}
336
337
	public function uExperienceInSchool()
338
	{
339
		$now = Utils::now();
340
		$commanders = $this->entityManager->getRepository(Commander::class)->getAllByStatements([Commander::INSCHOOL]);
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 getAllByStatements() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Ares\Re...ory\CommanderRepository. 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...
341
		$this->entityManager->beginTransaction();
342
		
343
		foreach ($commanders as $commander) {
344
			// If the commander was updated recently, we skip him
345
			if (Utils::interval($commander->uCommander, $now, 'h') === 0) {
346
				continue;
347
			}
348
			
349
			$nbrHours = Utils::intervalDates($now, $commander->uCommander);
350
			$commander->uCommander = $now;
351
			$orbitalBase = $this->orbitalBaseManager->get($commander->rBase);
352
			
353
			$playerBonus = $this->playerBonusManager->getBonusByPlayer($this->playerManager->get($commander->rPlayer));
354
			$this->playerBonusManager->load($playerBonus);
355
			$playerBonus = $playerBonus->bonus;
356
			foreach ($nbrHours as $hour) {
357
				$invest  = $orbitalBase->iSchool;
358
				$invest += $invest * $playerBonus->get(PlayerBonus::COMMANDER_INVEST) / 100;
359
360
				# xp gagnée
361
				$earnedExperience  = $invest / Commander::COEFFSCHOOL;
362
				$earnedExperience += (rand(0, 1) == 1) 
363
					? rand(0, $earnedExperience / 20)
364
					: -(rand(0, $earnedExperience / 20));
365
				$earnedExperience  = round($earnedExperience);
366
				$earnedExperience  = ($earnedExperience < 0)
367
					? 0 : $earnedExperience;
368
369
				$this->upExperience($commander, $earnedExperience);
370
			}
371
		}
372
		$this->entityManager->commit();
373
	}
374
375
	public function move(Commander $commander, $rDestinationPlace, $rStartPlace, $travelType, $travelLength, $duration) {
376
		$commander->rDestinationPlace = $rDestinationPlace;
377
		$commander->rStartPlace = $rStartPlace;
378
		$commander->travelType = $travelType;
379
		$commander->travelLength = $travelLength;
380
		$commander->statement = Commander::MOVING;
381
382
		$commander->dStart = ($travelType != 3) ? Utils::now() : $commander->dArrival;
383
		$commander->destinationPlaceName = ($travelType != 3) ? $commander->destinationPlaceName : $commander->startPlaceName;
384
		$commander->startPlaceName = ($travelType != 3) ? $commander->oBName : $commander->destinationPlaceName;
385
		$date = new \DateTime($commander->dStart);
386
		$date->modify('+' . $duration . 'second');
387
		$commander->dArrival = $date->format('Y-m-d H:i:s');
388
		
389
		$this->realtimeActionScheduler->schedule(
390
			'ares.commander_manager',
391
			$this->actions[$travelType],
392
			$commander,
0 ignored issues
show
Documentation introduced by
$commander is of type object<Asylamba\Modules\Ares\Model\Commander>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
393
			$commander->dArrival,
394
			[
395
				'class' => Place::class,
396
				'id' => $commander->getRPlaceDestination()
397
			]
398
		);
399
	}
400
	
401
	public function resultOfFight(Commander $commander, $isWinner, $enemyCommander) {
402
		if ($isWinner == TRUE) {
403
			$this->setEarnedExperience($commander, $enemyCommander);
404
			$commander->earnedExperience = round($commander->earnedExperience);
0 ignored issues
show
Documentation Bug introduced by
The property $earnedExperience was declared of type integer, but round($commander->earnedExperience) is of type double. 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...
405
			LiveReport::$expCom = $commander->earnedExperience;
0 ignored issues
show
Documentation Bug introduced by
The property $expCom was declared of type integer, but $commander->earnedExperience is of type double. 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...
406
407
			$commander->winner = TRUE;
408
			$commander->palmares++;
409
			$commander->setArmyAtEnd();
410
			$this->upExperience($commander, $commander->earnedExperience);
411
			$commander->hasChanged = TRUE;
0 ignored issues
show
Bug introduced by
The property hasChanged does not seem to exist in Asylamba\Modules\Ares\Model\Commander.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
412
		} else {
413
			/** @TOVERIFY **/
414
			$this->setEarnedExperience($commander, $commander, $enemyCommander);
0 ignored issues
show
Unused Code introduced by
The call to CommanderManager::setEarnedExperience() has too many arguments starting with $enemyCommander.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
415
			$commander->earnedExperience = round($commander->earnedExperience);
416
417
			$commander->winner = FALSE;
418
			$commander->setArmyAtEnd();
419
			$this->upExperience($commander, $commander->earnedExperience);
420
			$commander->hasChanged = TRUE;
421
		}
422
	}
423
424
	# ENGAGE UN COMBAT ENTRE CHAQUE SQUADRON CONTRE UN COMMANDANT
425
	public function engage(Commander $commander, $enemyCommander) {
426
		$commander->setArmy();
427
		
428
		for ($i = 0; $i < count($commander->squadronsIds); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
429
			$commander->getSquadron($i)->relId = 0;
430
		}
431
		$idSquadron = 0;
432
		foreach ($commander->army as $squadron) {
433
			if ($squadron->getNbrShips() != 0 AND $squadron->getLineCoord() * 3 <= FightManager::getCurrentLine()) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected and, but found AND.
Loading history...
434
				$enemyCommander = $squadron->engage($enemyCommander, $idSquadron, $commander->id, $commander->name, $commander);
435
			}
436
			$idSquadron++;
437
		}
438
		return $enemyCommander;
439
	}
440
441
	public function getPosition(Commander $commander, $x1, $y1, $x2, $y2) {
442
		$x = $x1;
443
		$y = $y1;
444
		if ($commander->statement == Commander::MOVING) {
445
			$parcouredTime = Utils::interval($commander->dStart, Utils::now(), 's');
446
			$totalTime = Utils::interval($commander->dStart, $commander->dArrival, 's');
447
			$progression = $parcouredTime / $totalTime;
448
449
			$x = $x1 + $progression * ($x2-$x1);
450
			$y = $y1 + $progression * ($y2-$y1);
451
		}
452
		return array($x, $y);
453
	}
454
455
	public function getEventInfo(Commander $commander) {
456
		$info = new ArrayList();
457
		$info->add('id', $commander->id);
458
		$info->add('name', $commander->name);
459
		$info->add('avatar', $commander->avatar);
460
		$info->add('level', $commander->level);
461
462
		$info->add('dStart', $commander->dStart);
463
		$info->add('rStart', $commander->rStartPlace);
464
		$info->add('nStart', $commander->startPlaceName);
465
		$info->add('dArrival', $commander->dArrival);
466
		$info->add('rArrival', $commander->rDestinationPlace);
467
		$info->add('nArrival', $commander->destinationPlaceName);
468
469
		$info->add('travelType', $commander->travelType);
470
		$info->add('resources', $commander->resources);
471
472
		return $info;
473
	}
474
475
	/**
476
	 * Fleet moving
477
	 * 
478
	 * @param int $commanderId
479
	 */
480
	public function uChangeBase($commanderId) {
481
		$commander = $this->get($commanderId);
482
		$place = $this->placeManager->get($commander->rDestinationPlace);
483
		$place->commanders = $this->getBaseCommanders($place->id);
484
		$commanderPlace = $this->placeManager->get($commander->rBase);
485
		$player = $this->playerManager->get($commander->rPlayer);
486
		$playerBonus = $this->playerBonusManager->getBonusByPlayer($player);
487
		$this->playerBonusManager->load($playerBonus);
488
		# si la place et la flotte ont la même couleur
489
		# on pose la flotte si il y a assez de place
490
		# sinon on met la flotte dans les hangars
491
		if ($place->playerColor !== $commander->playerColor or $place->typeOfBase !== Place::TYP_ORBITALBASE) {
492
			# retour forcé
493
			$this->comeBack($place, $commander, $commanderPlace, $playerBonus);
494
			$this->placeManager->sendNotif($place, Place::CHANGELOST, $commander);
495
			$this->entityManager->flush();
496
			return;
497
		}
498
		$maxCom =
499
			($place->typeOfOrbitalBase == OrbitalBase::TYP_MILITARY || $place->typeOfOrbitalBase == OrbitalBase::TYP_CAPITAL)
500
			? OrbitalBase::MAXCOMMANDERMILITARY
501
			: OrbitalBase::MAXCOMMANDERSTANDARD
502
		;
503
504
		# si place a assez de case libre :
505
		if (count($place->commanders) < $maxCom) {
506
			$comLine2 = 0;
507
508
			foreach ($place->commanders as $com) {
509
				if ($com->line == 2) {
510
					$comLine2++;
511
				}
512
			}
513
514
			if ($maxCom == OrbitalBase::MAXCOMMANDERMILITARY) {
515
				if ($comLine2 < 2) {
516
					$commander->line = 2;
517
				} else {
518
					$commander->line = 1;
519
				}
520
			} else {
521
				if ($comLine2 < 1) {
522
					$commander->line = 2;
523
				} else {
524
					$commander->line = 1;
525
				}
526
			}
527
528
			# changer rBase commander
529
			$commander->rBase = $place->id;
530
			$this->endTravel($commander, Commander::AFFECTED);
531
532
			# ajouter à la place le commandant
533
			$place->commanders[] = $commander;
534
535
			# envoi de notif
536
			$this->placeManager->sendNotif($place, Place::CHANGESUCCESS, $commander);
537
		} else {
538
			# changer rBase commander
539
			$commander->rBase = $place->id;
540
			$this->endTravel($commander, Commander::RESERVE);
541
542
			$this->emptySquadrons($commander);
543
544
			# envoi de notif
545
			$this->placeManager->sendNotif($place, Place::CHANGEFAIL, $commander);
546
		}
547
548
		# modifier le rPlayer (ne se modifie pas si c'est le même)
549
		$commander->rPlayer = $place->rPlayer;
550
551
		# instance de la place d'envoie + suppr commandant de ses flottes
552
		# enlever à rBase le commandant
553
		for ($i = 0; $i < count($commanderPlace->commanders); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
554
			if ($commanderPlace->commanders[$i]->id == $commander->id) {
555
				unset($commanderPlace->commanders[$i]);
556
				$commanderPlace->commanders = array_merge($commanderPlace->commanders);
557
			}
558
		}
559
		$this->entityManager->flush();
560
	}
561
562
	# pillage
563
	public function uLoot($commanderId) {
564
		$commander = $this->get($commanderId);
565
		$place = $this->placeManager->get($commander->rDestinationPlace);
566
		$place->commanders = $this->getBaseCommanders($place->id);
567
		$placePlayer = $this->playerManager->get($place->rPlayer);
568
		$placeBase = $this->orbitalBaseManager->get($place->id);
569
		$commanderPlace = $this->placeManager->get($commander->rBase);
570
		$commanderPlayer = $this->playerManager->get($commander->rPlayer);
571
		$commanderColor = $this->colorManager->get($commanderPlayer->rColor);
572
		$playerBonus = $this->playerBonusManager->getBonusByPlayer($commanderPlayer);
573
		$this->playerBonusManager->load($playerBonus);
574
		LiveReport::$type   = Commander::LOOT;
575
		LiveReport::$dFight = $commander->dArrival;
576
577
		# si la planète est vide
578
		if ($place->rPlayer == NULL) {
579
			LiveReport::$isLegal = Report::LEGAL;
580
581
			# planète vide : faire un combat
582
			$this->startFight($place, $commander, $commanderPlayer);
583
584
			# victoire
585
			if ($commander->getStatement() != Commander::DEAD) {
586
				# piller la planète
587
				$this->lootAnEmptyPlace($place, $commander, $playerBonus);
588
				# création du rapport de combat
589
				$report = $this->createReport($place);
590
591
				# réduction de la force de la planète
592
				$percentage = (($report->pevAtEndD + 1) / ($report->pevInBeginD + 1)) * 100;
593
				$place->danger = round(($percentage * $place->danger) / 100);
0 ignored issues
show
Documentation Bug introduced by
The property $danger was declared of type integer, but round($percentage * $place->danger / 100) is of type double. 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...
594
595
				$this->comeBack($place, $commander, $commanderPlace, $playerBonus);
596
				$this->placeManager->sendNotif($place, Place::LOOTEMPTYSSUCCESS, $commander, $report->id);
0 ignored issues
show
Documentation introduced by
$report->id is of type integer, but the function expects a object<Asylamba\Modules\Ares\Model\Report>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
597
			} else {
598
				# si il est mort
599
				# enlever le commandant de la session
600
				for ($i = 0; $i < count($commanderPlace->commanders); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
601
					if ($commanderPlace->commanders[$i]->getId() == $commander->getId()) {
602
						unset($commanderPlace->commanders[$i]);
603
						$commanderPlace->commanders = array_merge($commanderPlace->commanders);
604
					}
605
				}
606
607
				# création du rapport de combat
608
				$report = $this->createReport($place);
609
				$this->placeManager->sendNotif($place, Place::LOOTEMPTYFAIL, $commander, $report->id);
0 ignored issues
show
Documentation introduced by
$report->id is of type integer, but the function expects a object<Asylamba\Modules\Ares\Model\Report>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
610
611
				# réduction de la force de la planète
612
				$percentage = (($report->pevAtEndD + 1) / ($report->pevInBeginD + 1)) * 100;
613
				$place->danger = round(($percentage * $place->danger) / 100);
614
			}
615
		# si il y a une base d'un joueur
616
		} else {
617
			if ($commanderColor->colorLink[$place->playerColor] == Color::ALLY || $commanderColor->colorLink[$place->playerColor] == Color::PEACE) {
618
				LiveReport::$isLegal = Report::ILLEGAL;
619
			} else {
620
				LiveReport::$isLegal = Report::LEGAL;
621
			}
622
623
			# planète à joueur : si $this->rColor != commandant->rColor
624
			# si il peut l'attaquer
625
			if (($place->playerColor != $commander->getPlayerColor() && $place->playerLevel > 1 && $commanderColor->colorLink[$place->playerColor] != Color::ALLY) || ($place->playerColor == 0)) {
626
				$dCommanders = array();
627
				foreach ($place->commanders as $dCommander) {
628
					if ($dCommander->statement == Commander::AFFECTED && $dCommander->line == 1) {
629
						$dCommanders[] = $dCommander;
630
					}
631
				}
632
633
				# il y a des commandants en défense : faire un combat avec un des commandants
634
				if (count($dCommanders) != 0) {
635
					$aleaNbr = rand(0, count($dCommanders) - 1);
636
					$this->startFight($place, $commander, $commanderPlayer, $dCommanders[$aleaNbr], $placePlayer, TRUE);
637
638
					# victoire
639
					if ($commander->getStatement() != Commander::DEAD) {
640
						# piller la planète
641
						$this->lootAPlayerPlace($commander, $playerBonus, $placeBase);
642
						$this->comeBack($place, $commander, $commanderPlace, $playerBonus);
643
	
644
						# suppression des commandants						
645
						unset($place->commanders[$aleaNbr]);
646
						$place->commanders = array_merge($place->commanders);
647
648
						# création du rapport
649
						$report = $this->createReport($place);
650
651
						$this->placeManager->sendNotif($place, Place::LOOTPLAYERWHITBATTLESUCCESS, $commander, $report->id);
0 ignored issues
show
Documentation introduced by
$report->id is of type integer, but the function expects a object<Asylamba\Modules\Ares\Model\Report>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
652
				
653
					# défaite
654
					} else {
655
						# enlever le commandant de la session
656
						for ($i = 0; $i < count($commanderPlace->commanders); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
657
							if ($commanderPlace->commanders[$i]->getId() == $commander->getId()) {
658
								unset($commanderPlace->commanders[$i]);
659
								$commanderPlace->commanders = array_merge($commanderPlace->commanders);
660
							}
661
						}
662
663
						# création du rapport
664
						$report = $this->createReport($place);
665
666
						# mise à jour des flottes du commandant défenseur
667
						for ($j = 0; $j < count($dCommanders[$aleaNbr]->armyAtEnd); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
668
							for ($i = 0; $i < 12; $i++) { 
669
								$dCommanders[$aleaNbr]->armyInBegin[$j][$i] = $dCommanders[$aleaNbr]->armyAtEnd[$j][$i];
670
							}
671
						}
672
673
						$this->placeManager->sendNotif($place, Place::LOOTPLAYERWHITBATTLEFAIL, $commander, $report->id);
0 ignored issues
show
Documentation introduced by
$report->id is of type integer, but the function expects a object<Asylamba\Modules\Ares\Model\Report>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
674
					}
675
				} else {
676
					$this->lootAPlayerPlace($commander, $playerBonus, $placeBase);
677
					$this->comeBack($place, $commander, $commanderPlace, $playerBonus);
678
					$this->placeManager->sendNotif($place, Place::LOOTPLAYERWHITOUTBATTLESUCCESS, $commander);
679
				}
680
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
681
			} else {
682
				# si c'est la même couleur
683
				if ($place->rPlayer == $commander->rPlayer) {
684
					# si c'est une de nos planètes
685
					# on tente de se poser
686
					$this->uChangeBase($commander->id);
687
				} else {
688
					# si c'est une base alliée
689
					# on repart
690
					$this->comeBack($place, $commander, $commanderPlace, $playerBonus);
691
					$this->placeManager->sendNotif($place, Place::CHANGELOST, $commander);
692
				}
693
			}
694
		}
695
		$this->entityManager->flush();
696
	}
697
698
	# conquest
699
	public function uConquer($commanderId) {
700
		$commander = $this->get($commanderId);
701
		$place = $this->placeManager->get($commander->rDestinationPlace);
702
		$place->commanders = $this->getBaseCommanders($place->id);
703
		$placePlayer = $this->playerManager->get($place->rPlayer);
704
		$placeBase = $this->orbitalBaseManager->get($place->id);
705
		$commanderPlace = $this->placeManager->get($commander->rBase);
706
		$commanderPlayer = $this->playerManager->get($commander->rPlayer);
707
		$commanderColor = $this->colorManager->get($commanderPlayer->rColor);
708
		$baseCommanders = $this->getBaseCommanders($place->getId());
709
		$playerBonus = $this->playerBonusManager->getBonusByPlayer($commanderPlayer);
710
		$this->playerBonusManager->load($playerBonus);
711
		# conquete
712
		if ($place->rPlayer != NULL) {
713
			if (($place->playerColor != $commander->getPlayerColor() && $place->playerLevel > 3 && $commanderColor->colorLink[$place->playerColor] != Color::ALLY) || ($place->playerColor == 0)) {
714
				$tempCom = array();
715
716
				for ($i = 0; $i < count($place->commanders); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
717
					if ($place->commanders[$i]->line <= 1) {
718
						$tempCom[] = $place->commanders[$i];
719
					}
720
				}
721
				for ($i = 0; $i < count($place->commanders); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
722
					if ($place->commanders[$i]->line >= 2) {
723
						$tempCom[] = $place->commanders[$i];
724
					}
725
				}
726
727
				$place->commanders = $tempCom;
728
729
				$nbrBattle = 0;
730
				$reportIds   = array();
731
				$reportArray = array();
732
733
				while ($nbrBattle < count($place->commanders)) {
734
					if ($place->commanders[$nbrBattle]->statement == Commander::AFFECTED) {
735
						LiveReport::$type = Commander::COLO;
736
						LiveReport::$dFight = $commander->dArrival;
737
738
						if ($commanderColor->colorLink[$place->playerColor] == Color::ALLY || $commanderColor->colorLink[$place->playerColor] == Color::PEACE) {
739
							LiveReport::$isLegal = Report::ILLEGAL;
740
						} else {
741
							LiveReport::$isLegal = Report::LEGAL;
742
						}
743
744
						$this->startFight($place, $commander, $commanderPlayer, $place->commanders[$nbrBattle], $placePlayer, TRUE);
745
746
						$report = $this->createReport($place);
747
						$reportArray[] = $report;
748
						$reportIds[] = $report->id;
749
						# PATCH DEGUEU POUR LES MUTLIS-COMBATS
750
						$this->entityManager->clear($report);
751
						$reports = $this->reportManager->getByAttackerAndPlace($commander->rPlayer, $place->id, $commander->dArrival);
752
						foreach($reports as $r) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
753
							if ($r->id == $report->id) {
754
								continue;
755
							}
756
							$r->statementAttacker = Report::DELETED;
757
							$r->statementDefender = Report::DELETED;
758
						}
759
						$this->entityManager->flush(Report::class);
760
						$this->entityManager->clear(Report::class);
761
						########################################
762
763
						# mettre à jour armyInBegin si prochain combat pour prochain rapport
764
						for ($j = 0; $j < count($commander->armyAtEnd); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
765
							for ($i = 0; $i < 12; $i++) { 
766
								$commander->armyInBegin[$j][$i] = $commander->armyAtEnd[$j][$i];
767
							}
768
						}
769
						for ($j = 0; $j < count($place->commanders[$nbrBattle]->armyAtEnd); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
770
							for ($i = 0; $i < 12; $i++) {
771
								$place->commanders[$nbrBattle]->armyInBegin[$j][$i] = $place->commanders[$nbrBattle]->armyAtEnd[$j][$i];
772
							}
773
						}
774
						
775
						$nbrBattle++;
776
						# mort du commandant
777
						# arrêt des combats
778
						if ($commander->getStatement() == Commander::DEAD) {
779
							break;
780
						}
781
					} else {
782
						$nbrBattle++;
783
					}
784
				}
785
786
				# victoire
787
				if ($commander->getStatement() != Commander::DEAD) {
788
					if ($nbrBattle == 0) {
789
						$this->placeManager->sendNotif($place, Place::CONQUERPLAYERWHITOUTBATTLESUCCESS, $commander, NULL);
790
					} else {
791
						$this->placeManager->sendNotifForConquest($place, Place::CONQUERPLAYERWHITBATTLESUCCESS, $commander, $reportIds);
792
					}
793
794
795
					#attribuer le joueur à la place
796
					$place->commanders = array();
797
					$place->playerColor = $commander->playerColor;
798
					$place->rPlayer = $commander->rPlayer;
799
800
					# changer l'appartenance de la base (et de la place)
801
					$this->orbitalBaseManager->changeOwnerById($place->id, $placeBase, $commander->getRPlayer(), $baseCommanders);
802
					$place->commanders[] = $commander;
803
804
					$commander->rBase = $place->id;
805
					$this->endTravel($commander, Commander::AFFECTED);
806
					$commander->line = 2;
807
					
808
					$this->eventDispatcher->dispatch(new PlaceOwnerChangeEvent($place));
809
810
					# PATCH DEGUEU POUR LES MUTLIS-COMBATS
811
					$this->notificationManager->patchForMultiCombats($commander->rPlayer, $place->rPlayer, $commander->dArrival);
812
				# défaite
813
				} else {
814
					for ($i = 0; $i < count($place->commanders); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
815
						if ($place->commanders[$i]->statement == Commander::DEAD) {
816
							unset($place->commanders[$i]);
817
							$place->commanders = array_merge($place->commanders);
818
						}
819
					}
820
821
					$this->placeManager->sendNotifForConquest($place, Place::CONQUERPLAYERWHITBATTLEFAIL, $commander, $reportIds);
822
				}
823
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
824
			} else {
825
				# si c'est la même couleur
826
				if ($place->rPlayer == $commander->rPlayer) {
827
					# si c'est une de nos planètes
828
					# on tente de se poser
829
					$this->uChangeBase($commander->id);
830
				} else {
831
					# si c'est une base alliée
832
					# on repart
833
					$this->comeBack($place, $commander, $commanderPlace, $playerBonus);
834
					$this->placeManager->sendNotif($place, Place::CHANGELOST, $commander);
835
				}
836
			}
837
838
		# colonisation
839
		} else {
840
			# faire un combat
841
			LiveReport::$type = Commander::COLO;
842
			LiveReport::$dFight = $commander->dArrival;
843
			LiveReport::$isLegal = Report::LEGAL;
844
845
			$this->startFight($place, $commander, $commanderPlayer);
846
847
			# victoire
848
			if ($commander->getStatement() !== Commander::DEAD) {
849
				# attribuer le rPlayer à la Place !
850
				$place->rPlayer = $commander->rPlayer;
851
				$place->commanders[] = $commander;
852
				$place->playerColor = $commander->playerColor;
853
				$place->typeOfBase = 4; 
854
855
				# créer une base
856
				$ob = new OrbitalBase();
857
				$ob->rPlace = $place->id;
858
				$ob->setRPlayer($commander->getRPlayer());
859
				$ob->setName('colonie');
860
				$ob->iSchool = 500;
861
				$ob->iAntiSpy = 500;
862
				$ob->resourcesStorage = 2000;
863
				$ob->uOrbitalBase = Utils::now();
864
				$ob->dCreation = Utils::now();
865
				$this->orbitalBaseManager->updatePoints($ob);
866
867
				$this->orbitalBaseManager->add($ob);
868
869
				# attibuer le commander à la place
870
				$commander->rBase = $place->id;
871
				$this->endTravel($commander, Commander::AFFECTED);
872
				$commander->line = 2;
873
				
874
				# création du rapport
875
				$report = $this->createReport($place);
876
877
				$place->danger = 0;
878
879
				$this->placeManager->sendNotif($place, Place::CONQUEREMPTYSSUCCESS, $commander, $report->id);
880
				
881
				$this->eventDispatcher->dispatch(new PlaceOwnerChangeEvent($place));
882
			
883
			# défaite
884
			} else {
885
				# création du rapport
886
				$report = $this->createReport($place);
887
888
				# mise à jour du danger
889
				$percentage = (($report->pevAtEndD + 1) / ($report->pevInBeginD + 1)) * 100;
890
				$place->danger = round(($percentage * $place->danger) / 100);
0 ignored issues
show
Documentation Bug introduced by
The property $danger was declared of type integer, but round($percentage * $place->danger / 100) is of type double. 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...
891
892
				$this->placeManager->sendNotif($place, Place::CONQUEREMPTYFAIL, $commander);
893
894
				# enlever le commandant de la place
895
				foreach ($commanderPlace->commanders as $placeCommander) {
896
					if ($placeCommander->getId() == $commander->getId()) {
897
						unset($placeCommander);
898
						$commanderPlace->commanders = array_merge($commanderPlace->commanders);
899
					}
900
				}
901
			}
902
		}
903
		$this->entityManager->flush(Commander::class);
904
		$this->entityManager->flush($place);
905
	}
906
907
	/**
908
	 * @param int $commanderId
909
	 */
910
	public function uReturnBase($commanderId) {
911
		$commander = $this->get($commanderId);
912
		$place = $this->placeManager->get($commander->rDestinationPlace);
913
		$commanderBase = $this->orbitalBaseManager->get($commander->rBase);
914
		
915
		$this->endTravel($commander, Commander::AFFECTED);
916
917
		$this->placeManager->sendNotif($place, Place::COMEBACK, $commander);
918
919
		if ($commander->resources > 0) {
920
			$this->orbitalBaseManager->increaseResources($commanderBase, $commander->resources, TRUE);
921
			$commander->resources = 0;
922
		}
923
		$this->entityManager->flush($commander);
924
	}
925
	
926
	/**
927
	 * @param Commander $commander
928
	 * @param string $statement
929
	 */
930
	public function endTravel(Commander $commander, $statement)
931
	{
932
		$commander->travelType = null;
933
		$commander->travelLength = null;
934
		$commander->dStart = null;
935
		$commander->dArrival = null;
936
		$commander->rStartPlace = null;
937
		$commander->rDestinationPlace = null;
938
		$commander->statement = $statement;
939
	}
940
941
	# HELPER
942
943
	# comeBack
944
	public function comeBack(Place $place, $commander, $commanderPlace, $playerBonus) {
945
		$length   = Game::getDistance($place->getXSystem(), $commanderPlace->getXSystem(), $place->getYSystem(), $commanderPlace->getYSystem());
946
		$duration = Game::getTimeToTravel($commanderPlace, $place, $playerBonus->bonus);
947
948
		$this->move($commander, $commander->rBase, $place->id, Commander::BACK, $length, $duration);
949
	}
950
951
	private function lootAnEmptyPlace(Place $place, $commander, $playerBonus) {
952
		$bonus = $playerBonus->bonus->get(PlayerBonus::SHIP_CONTAINER);
953
	
954
		$storage = $commander->getPevToLoot() * Commander::COEFFLOOT;
955
		$storage += round($storage * ((2 * $bonus) / 100));
956
957
		$resourcesLooted = 0;
0 ignored issues
show
Unused Code introduced by
$resourcesLooted is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
958
		$resourcesLooted = ($storage > $place->resources) ? $place->resources : $storage;
959
960
		$place->resources -= $resourcesLooted;
961
		$commander->resources = $resourcesLooted;
962
963
		LiveReport::$resources = $resourcesLooted;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resourcesLooted can also be of type double. However, the property $resources is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
964
	}
965
966
	private function lootAPlayerPlace($commander, $playerBonus, $placeBase) {
967
		$bonus = $playerBonus->bonus->get(PlayerBonus::SHIP_CONTAINER);
968
969
		$resourcesToLoot = $placeBase->getResourcesStorage() - Commander::LIMITTOLOOT;
970
971
		$storage = $commander->getPevToLoot() * Commander::COEFFLOOT;
972
		$storage += round($storage * ((2 * $bonus) / 100));
973
974
		$resourcesLooted = 0;
0 ignored issues
show
Unused Code introduced by
$resourcesLooted is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
975
		$resourcesLooted = ($storage > $resourcesToLoot) ? $resourcesToLoot : $storage;
976
977
		if ($resourcesLooted > 0) {
978
			$this->orbitalBaseManager->decreaseResources($placeBase, $resourcesLooted);
979
			$commander->resources = $resourcesLooted;
980
981
			LiveReport::$resources = $resourcesLooted;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resourcesLooted can also be of type double. However, the property $resources is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
982
		}
983
	}
984
985
	private function startFight(Place $place, $commander, $player, $enemyCommander = NULL, $enemyPlayer = NULL, $pvp = FALSE) {
986
		if ($pvp == TRUE) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
987
			$commander->setArmy();
988
			$enemyCommander->setArmy();
989
990
			$this->fightManager->startFight($commander, $player, $enemyCommander, $enemyPlayer);
991
		} else {
992
			$commander->setArmy();
993
			$computerCommander = $this->createVirtualCommander($place);
994
995
			$this->fightManager->startFight($commander, $player, $computerCommander);
996
		}
997
	}
998
999
	private function createReport(Place $place) {
1000
		$report = new Report();
1001
1002
		$report->rPlayerAttacker = LiveReport::$rPlayerAttacker;
1003
		$report->rPlayerDefender =  LiveReport::$rPlayerDefender;
1004
		$report->rPlayerWinner = LiveReport::$rPlayerWinner;
1005
		$report->avatarA = LiveReport::$avatarA;
1006
		$report->avatarD = LiveReport::$avatarD;
1007
		$report->nameA = LiveReport::$nameA;
1008
		$report->nameD = LiveReport::$nameD;
1009
		$report->levelA = LiveReport::$levelA;
1010
		$report->levelD = LiveReport::$levelD;
1011
		$report->experienceA = LiveReport::$experienceA;
1012
		$report->experienceD = LiveReport::$experienceD;
1013
		$report->palmaresA = LiveReport::$palmaresA;
1014
		$report->palmaresD = LiveReport::$palmaresD;
1015
		$report->resources = LiveReport::$resources;
1016
		$report->expCom = LiveReport::$expCom;
1017
		$report->expPlayerA = LiveReport::$expPlayerA;
1018
		$report->expPlayerD = LiveReport::$expPlayerD;
1019
		$report->rPlace = $place->id;
1020
		$report->type = LiveReport::$type;
1021
		$report->round = LiveReport::$round;
1022
		$report->importance = LiveReport::$importance;
1023
		$report->squadrons = LiveReport::$squadrons;
1024
		$report->dFight = LiveReport::$dFight;
1025
		$report->isLegal = LiveReport::$isLegal;
1026
		$report->placeName = ($place->baseName == '') ? 'planète rebelle' : $place->baseName;
1027
		$report->setArmies();
1028
		$report->setPev();
1029
		
1030
		$this->reportManager->add($report);
1031
		LiveReport::clear();
1032
1033
		return $report;
1034
	}
1035
1036
	/**
1037
	 * @param Place $place
1038
	 * @return Commander
1039
	 */
1040
	public function createVirtualCommander(Place $place) {
1041
		$vCommander = new Commander();
1042
		$vCommander->id = 'Null';
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type integer, but 'Null' 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...
1043
		$vCommander->rPlayer = ID_GAIA;
1044
		$vCommander->name = 'rebelle';
1045
		$vCommander->avatar = 't3-c4';
1046
		$vCommander->sexe = 1;
1047
		$vCommander->age = 42;
1048
		$vCommander->statement = 1;
1049
		$vCommander->level = ceil((((($place->maxDanger / (Place::DANGERMAX / Place::LEVELMAXVCOMMANDER))) * 9) + ($place->population / (Place::POPMAX / Place::LEVELMAXVCOMMANDER))) / 10);
0 ignored issues
show
Documentation Bug introduced by
The property $level was declared of type integer, but ceil(($place->maxDanger ...ELMAXVCOMMANDER)) / 10) is of type double. 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...
1050
1051
		$nbrsquadron = ceil($vCommander->level * (($place->danger + 1) / ($place->maxDanger + 1)));
1052
1053
		$army = array();
1054
		$squadronsIds = array();
1055
1056
		for ($i = 0; $i < $nbrsquadron; $i++) {
1057
			$aleaNbr = ($place->coefHistory * $place->coefResources * $place->position * $i) % SquadronResource::size();
1058
			$army[] = SquadronResource::get($vCommander->level, $aleaNbr);
1059
			$squadronsIds[] = 0;
1060
		}
1061
1062
		for ($i = $vCommander->level - 1; $i >= $nbrsquadron; $i--) {
1063
			$army[$i] = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Utils::now());
1064
			$squadronsIds[] = 0;
1065
		}
1066
1067
		$vCommander->setSquadronsIds($squadronsIds);
1068
		$vCommander->setArmyInBegin($army);
1069
		$vCommander->setArmy();
1070
		$vCommander->setPevInBegin();
1071
1072
		return $vCommander;
1073
	}
1074
}
1075