Completed
Pull Request — master (#252)
by
unknown
05:45
created

OneParameterCommand::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 26

Duplication

Lines 30
Ratio 100 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 30
loc 30
rs 8.8571
ccs 16
cts 16
cp 1
cc 1
eloc 26
nc 1
nop 11
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\Storage\PlayerStorage;
10
use Maniaplanet\DedicatedServer\Connection;
11
use Maniaplanet\DedicatedServer\Xmlrpc\Exception as DedicatedException;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
16
/**
17
 * Class ReasonUserCommand
18
 *
19
 * @author  Reaby
20
 * @package eXpansion\Bundle\AdminChat\ChatCommand
21
 */
22
class OneParameterCommand extends AbstractConnectionCommand
23
{
24
    /**
25
     * Description of the parameter
26
     *
27
     * @var string
28
     */
29
    protected $parameterDescription;
30
31
    /**
32
     * Description of the command.
33
     *
34
     * @var string
35
     */
36
    protected $description;
37
38
    /**
39
     * Message to display in chat.
40
     *
41
     * @var string
42
     */
43
    protected $chatMessage;
44
45
    /**
46
     * Name of the dedicated function to call.
47
     *
48
     * @var string
49
     */
50
    protected $functionName;
51
52
    /**
53
     * OneParameterCommand constructor.
54
     *
55
     * @param                  $command
56
     * @param string           $permission
57
     * @param array            $aliases
58
     * @param string           $functionName
59
     * @param string           $parameterDescription
60
     * @param AdminGroups      $adminGroupsHelper
61
     * @param Connection       $connection
62
     * @param ChatNotification $chatNotification
63
     * @param PlayerStorage    $playerStorage
64
     * @param LoggerInterface  $logger
65
     * @param Time             $timeHelper
66
     */
67 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...
68
        $command,
69
        $permission,
70
        array $aliases = [],
71
        $functionName,
72
        $parameterDescription,
73
        AdminGroups $adminGroupsHelper,
74
        Connection $connection,
75
        ChatNotification $chatNotification,
76
        PlayerStorage $playerStorage,
77
        LoggerInterface $logger,
78
        Time $timeHelper
79
    ) {
80 1
        parent::__construct(
81 1
            $command,
82 1
            $permission,
83 1
            $aliases,
84 1
            $adminGroupsHelper,
85 1
            $connection,
86 1
            $chatNotification,
87 1
            $playerStorage,
88 1
            $logger,
89 1
            $timeHelper
90
        );
91
92 1
        $this->description = 'expansion_admin_chat.'.strtolower($functionName).'.description';
93 1
        $this->chatMessage = 'expansion_admin_chat.'.strtolower($functionName).'.msg';
94 1
        $this->functionName = (string)$functionName;
95 1
        $this->parameterDescription = (string)$parameterDescription;
96 1
    }
97
98
99
    /**
100
     * @inheritdoc
101
     */
102 1
    protected function configure()
103
    {
104 1
        parent::configure();
105
106 1
        $this->inputDefinition->addArgument(
107 1
            new InputArgument('parameter', InputArgument::REQUIRED, $this->parameterDescription)
108
        );
109 1
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114 1 View Code Duplication
    public function execute($login, InputInterface $input)
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...
115
    {
116 1
        $nickName = $this->playerStorage->getPlayerInfo($login)->getNickName();
117 1
        $parameter = $input->getArgument('parameter');
118 1
        $group = $this->getGroupLabel($login);
119
        try {
120 1
            $this->connection->{$this->functionName}($parameter);
121 1
            $this->chatNotification->sendMessage(
122 1
                $this->chatMessage,
123 1
                $this->isPublic ? null : $login,
124 1
                ['%adminLevel%' => $group, '%admin%' => $nickName, "%parameter%" => $parameter]
125
            );
126
127 1
            $logMessage = $this->chatNotification->getMessage($this->chatMessage,
128 1
                ['%adminLevel%' => $group, '%admin%' => $nickName, "%parameter%" => $parameter], "en");
129 1
            $this->logger->info("[". $login. "] " . TMString::trimStyles($logMessage));
130
131
        } catch (DedicatedException $e) {
132
            $this->logger->error("Error on admin command", ["exception" => $e]);
133
            $this->chatNotification->sendMessage("expansion_admin_chat.dedicatedexception", $login,
134
                ["%message%" => $e->getMessage()]);
135
        }
136
137 1
    }
138
}
139