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

DailyRoutine::execute()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 5
nop 8
dl 0
loc 32
rs 8.439
c 0
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\Hephaistos\Routine;
4
5
use Asylamba\Classes\Entity\EntityManager;
6
7
use Asylamba\Modules\Zeus\Manager\PlayerManager;
8
9
use Asylamba\Classes\Worker\API;
10
11
use Asylamba\Modules\Hermes\Model\Notification;
12
use Asylamba\Modules\Zeus\Model\Player;
13
14
use Asylamba\Classes\Library\Utils;
15
16
class DailyRoutine
17
{
18
	/**
19
	 * @param EntityManager $entityManager
20
	 * @param API $api
21
	 * @param string $apimode
22
	 * @param PlayerManager $playerManager
23
	 * @param int $playerInactiveTimeLimit
24
	 * @param int $playerGlobalInactiveTime
25
	 * @param int $readTimeout
26
	 * @param int $unreadTimeout
27
	 */
28
	public function execute(
29
		EntityManager $entityManager,
30
		API $api,
31
		$apimode,
32
		PlayerManager $playerManager,
33
		$playerInactiveTimeLimit,
34
		$playerGlobalInactiveTime,
35
		$readTimeout,
36
		$unreadTimeout
37
	)
38
	{
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...
39
		$entityManager->getRepository(Notification::class)->cleanNotifications($readTimeout, $unreadTimeout);
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 cleanNotifications() does only exist in the following sub-classes of Asylamba\Classes\Entity\AbstractRepository: Asylamba\Modules\Hermes\...\NotificationRepository. 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...
40
		
41
		$players = $playerManager->getByStatements([Player::ACTIVE, Player::INACTIVE]);
42
		$nbPlayers = count($players);
43
		// @TODO understand this strange loop condition
44
		for ($i = $nbPlayers - 1; $i >= 0; $i--) {
45
			$player = $players[$i];
46
			if (Utils::interval(Utils::now(), $player->getDLastConnection()) >= $playerInactiveTimeLimit) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
47
48
				$playerManager->kill($player->id);
49
			} elseif (Utils::interval(Utils::now(), $player->getDLastConnection()) >= $playerGlobalInactiveTime AND $player->statement == Player::ACTIVE) {
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...
50
				$player->statement = Player::INACTIVE;
51
52
				if ($apimode === 'enabled') {
53
					# sending email API call
54
					$api->sendMail($player->bind, API::TEMPLATE_INACTIVE_PLAYER);
55
				}
56
			}
57
		}
58
		$entityManager->flush(Player::class);
59
	}
60
}