Completed
Pull Request — master (#175)
by
unknown
02:55
created

AdminShuffleCommand::execute()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 24
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 23
nc 9
nop 2
crap 30
1
<?php
2
3
namespace eXpansion\Bundle\AdminChat\ChatCommand;
4
5
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
6
use eXpansion\Framework\AdminGroups\Model\AbstractAdminChatCommand;
7
use eXpansion\Framework\Core\Helpers\ChatNotification;
8
use eXpansion\Framework\Core\Helpers\Time;
9
use eXpansion\Framework\Core\Storage\MapStorage;
10
use eXpansion\Framework\Core\Storage\PlayerStorage;
11
use Maniaplanet\DedicatedServer\Connection;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\Console\Input\InputInterface;
14
15
/**
16
 * Class ReasonUserCommand
17
 *
18
 * @author  Reaby
19
 * @package eXpansion\Bundle\AdminChat\ChatCommand
20
 */
21
class AdminShuffleCommand extends AbstractAdminChatCommand
22
{
23
    /** @var ChatNotification */
24
    private $chatNotification;
25
26
    /** @var PlayerStorage */
27
    private $playerStorage;
28
29
    /**
30
     * @var MapStorage
31
     */
32
    private $mapStorage;
33
    /**
34
     * @var Connection
35
     */
36
    private $connection;
37
38
    /**
39
     * AdminCommand constructor.
40
     *
41
     * @param                  $command
42
     * @param string           $permission
43
     * @param array            $aliases
44
     * @param ChatNotification $functionName
0 ignored issues
show
Bug introduced by
There is no parameter named $functionName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
45
     * @param AdminGroups      $adminGroupsHelper
46
     * @param Connection       $connection
47
     * @param ChatNotification $chatNotification
48
     * @param PlayerStorage    $playerStorage
49
     * @param LoggerInterface  $logger
50
     * @param Time             $timeHelper
0 ignored issues
show
Bug introduced by
There is no parameter named $timeHelper. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
51
     * @param MapStorage       $mapStorage
52
     */
53 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
        $command,
55
        $permission,
56
        array $aliases = [],
57
        AdminGroups $adminGroupsHelper,
58
        ChatNotification $chatNotification,
59
        PlayerStorage $playerStorage,
60
        LoggerInterface $logger,
61
        Connection $connection,
62
        MapStorage $mapStorage
63
    ) {
64
        parent::__construct(
65
            $command,
66
            $permission,
67
            $aliases = [],
68
            $adminGroupsHelper
69
        );
70
71
        $this->chatNotification = $chatNotification;
72
        $this->playerStorage = $playerStorage;
73
        $this->mapStorage = $mapStorage;
74
        $this->adminGroupsHelper = $adminGroupsHelper;
75
        $this->connection = $connection;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function execute($login, InputInterface $input)
82
    {
83
        $player = $this->playerStorage->getPlayerInfo($login)->getNickName();
0 ignored issues
show
Unused Code introduced by
$player 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...
84
        $allMaps = [];
85
        foreach ($this->mapStorage->getMaps() as $map) {
86
            $maps[] = $map->fileName;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$maps was never initialized. Although not strictly required by PHP, it is generally a good practice to add $maps = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
87
            $allMaps[] = $map->fileName;
88
            if (count($maps) > 250) {
89
                $this->connection->removeMapList($maps);
0 ignored issues
show
Bug introduced by
The variable $maps does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
90
                $maps = [];
91
            }
92
        }
93
        // for remaining maps, which didn't fit to 200
94
        $this->connection->removeMapList($maps);
95
        $this->connection->executeMulticall();
96
97
        shuffle($allMaps);
98
99
        $maps = [];
100
        foreach ($allMaps as $x => $mapFile) {
101
            $maps[] = $mapFile;
102
            if (count($maps) > 250) {
103
                $this->connection->insertMapList($maps, true);
104
            }
105
        }
106
        // for remaining maps, which didn't fit to 200
107
        $this->connection->insertMapList($maps, true);
108
        $this->connection->executeMulticall();
109
110
        $level = $this->adminGroupsHelper->getLoginGroupLabel($login);
111
        $admin = $this->playerStorage->getPlayerInfo($login)->getNickName();
112
        $this->chatNotification->sendMessage('expansion_admin_chat.shuffle.msg', null,
113
            ["%adminLevel%" => $level, "%admin%" => $admin]);
114
115
    }
116
}
117