Completed
Pull Request — master (#393)
by De Cramer
02:39
created

ReasonUserCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 32
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 32
loc 32
ccs 9
cts 9
cp 1
rs 9.408
c 0
b 0
f 0
cc 1
nc 1
nop 12
crap 1

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 eXpansion\Bundle\AdminChat\ChatCommand;
4
5
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
6
use eXpansion\Framework\Core\Helpers\ChatNotification;
7
use eXpansion\Framework\Core\Helpers\Time;
8
use eXpansion\Framework\Core\Helpers\TMString;
9
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory;
10
use eXpansion\Framework\Core\Storage\PlayerStorage;
11
use Maniaplanet\DedicatedServer\Connection;
12
use Maniaplanet\DedicatedServer\Xmlrpc\Exception as DedicatedException;
13
use Psr\Log\LoggerInterface;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
17
/**
18
 * Class ReasonUserCommand
19
 *
20
 * @author    de Cramer Oliver<[email protected]>
21
 * @copyright 2017 eXpansion
22
 * @package   eXpansion\Bundle\AdminChat\ChatCommand
23
 */
24
class ReasonUserCommand extends AbstractConnectionCommand
25
{
26
    /**
27
     * Description of the login parameter
28
     *
29
     * @var string
30
     */
31
    protected $parameterLoginDescription;
32
33
    /**
34
     * Description of the reason parameter.
35
     *
36
     * @var string
37
     */
38
    protected $parameterReasonDescription;
39
40
    /**
41
     * Description of the command.
42
     *
43
     * @var string
44
     */
45
    protected $description;
46
47
    /**
48
     * Message to display in chat.
49
     *
50
     * @var string
51
     */
52
    protected $chatMessage;
53
54
    /**
55
     * Name of the dedicated function to call.
56
     *
57
     * @var string
58
     */
59
    protected $functionName;
60
61
    /**
62
     * ReasonUserCommand constructor.
63
     *
64
     * @param $command
65
     * @param $permission
66
     * @param array $aliases
67
     * @param $functionName
68
     * @param $parameterLoginDescription
69
     * @param $parameterReasonDescription
70
     * @param AdminGroups $adminGroupsHelper
71
     * @param Factory $factory
72
     * @param ChatNotification $chatNotification
73
     * @param PlayerStorage $playerStorage
74
     * @param LoggerInterface $logger
75
     * @param Time $timeHelper
76
     */
77 1 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...
78
        $command,
79
        $permission,
80
        array $aliases = [],
81
        $functionName,
82
        $parameterLoginDescription,
83
        $parameterReasonDescription,
84
        AdminGroups $adminGroupsHelper,
85
        Factory $factory,
86
        ChatNotification $chatNotification,
87
        PlayerStorage $playerStorage,
88
        LoggerInterface $logger,
89
        Time $timeHelper
90
    ) {
91 1
        parent::__construct(
92 1
            $command,
93
            $permission,
94
            $aliases,
95
            $adminGroupsHelper,
96
            $factory,
97
            $chatNotification,
98
            $playerStorage,
99
            $logger,
100
            $timeHelper
101
        );
102
103 1
        $this->description = 'expansion_admin_chat.'.strtolower($functionName).'.description';
104 1
        $this->chatMessage = 'expansion_admin_chat.'.strtolower($functionName).'.msg';
105 1
        $this->functionName = $functionName;
106 1
        $this->parameterLoginDescription = $parameterLoginDescription;
107 1
        $this->parameterReasonDescription = $parameterReasonDescription;
108 1
    }
109
110
111
    /**
112
     * @inheritdoc
113
     */
114 1
    protected function configure()
115
    {
116 1
        parent::configure();
117
118 1
        $this->inputDefinition->addArgument(
119 1
            new InputArgument('login', InputArgument::REQUIRED, $this->parameterLoginDescription)
120
        );
121 1
        $this->inputDefinition->addArgument(
122 1
            new InputArgument('reason', InputArgument::REQUIRED, $this->parameterReasonDescription)
123
        );
124 1
    }
125
126
127
    /**
128
     * @inheritdoc
129
     */
130 1
    public function execute($login, InputInterface $input)
131
    {
132 1
        $nickName = $this->playerStorage->getPlayerInfo($login)->getNickName();
133 1
        $playerLogin = $input->getArgument('login');
134 1
        $reason = $input->getArgument('reason');
135 1
        $group = $this->getGroupLabel($login);
136
137 1
        $playerNickName = $this->playerStorage->getPlayerInfo($playerLogin)->getNickName();
0 ignored issues
show
Bug introduced by
It seems like $playerLogin defined by $input->getArgument('login') on line 133 can also be of type array<integer,string> or null; however, eXpansion\Framework\Core...torage::getPlayerInfo() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
138
        try {
139 1
            $this->factory->getConnection()->{$this->functionName}($playerLogin, $reason);
140 1
            $this->chatNotification->sendMessage(
141 1
                $this->chatMessage,
142 1
                $this->isPublic ? null : $login,
143 1
                ['%adminLevel%' => $group, '%admin%' => $nickName, '%player%' => $playerNickName, "%reason%" => $reason]
144
            );
145
146 1
            $logMessage = $this->chatNotification->getMessage($this->chatMessage,
147
                [
148 1
                    '%adminLevel%' => $group,
149 1
                    '%admin%' => $nickName,
150 1
                    '%player%' => $playerNickName,
151 1
                    "%reason%" => $reason
152 1
                ], "en");
153 1
            $this->logger->info("[".$login."] ".TMString::trimStyles($logMessage));
154
155
156
        } catch (DedicatedException $e) {
157
            $this->logger->error("Error on admin command", ["exception" => $e]);
158
            $this->chatNotification->sendMessage("expansion_admin_chat.dedicatedexception", $login,
159
                ["%message%" => $e->getMessage()]);
160
        }
161 1
    }
162
}
163