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

OneParameterCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 117
Duplicated Lines 46.15 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 10
dl 54
loc 117
ccs 26
cts 30
cp 0.8667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 24 24 3
A __construct() 30 30 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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  Reaby
21
 * @package eXpansion\Bundle\AdminChat\ChatCommand
22
 */
23
class OneParameterCommand extends AbstractConnectionCommand
24
{
25
    /**
26
     * Description of the parameter
27
     *
28
     * @var string
29
     */
30
    protected $parameterDescription;
31
32
    /**
33
     * Description of the command.
34
     *
35
     * @var string
36
     */
37
    protected $description;
38
39
    /**
40
     * Message to display in chat.
41
     *
42
     * @var string
43
     */
44
    protected $chatMessage;
45
46
    /**
47
     * Name of the dedicated function to call.
48
     *
49
     * @var string
50
     */
51
    protected $functionName;
52
53
    /**
54
     * OneParameterCommand constructor.
55
     *
56
     * @param                  $command
57
     * @param string           $permission
58
     * @param array            $aliases
59
     * @param string           $functionName
60
     * @param string           $parameterDescription
61
     * @param AdminGroups      $adminGroupsHelper
62
     * @param Connection       $connection
0 ignored issues
show
Bug introduced by
There is no parameter named $connection. 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...
63
     * @param ChatNotification $chatNotification
64
     * @param PlayerStorage    $playerStorage
65
     * @param LoggerInterface  $logger
66
     * @param Time             $timeHelper
67
     */
68 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...
69
        $command,
70
        $permission,
71
        array $aliases = [],
72
        $functionName,
73
        $parameterDescription,
74
        AdminGroups $adminGroupsHelper,
75
        Factory $factory,
76
        ChatNotification $chatNotification,
77
        PlayerStorage $playerStorage,
78
        LoggerInterface $logger,
79
        Time $timeHelper
80
    ) {
81 1
        parent::__construct(
82 1
            $command,
83
            $permission,
84
            $aliases,
85
            $adminGroupsHelper,
86
            $factory,
87
            $chatNotification,
88
            $playerStorage,
89
            $logger,
90
            $timeHelper
91
        );
92
93 1
        $this->description = 'expansion_admin_chat.'.strtolower($functionName).'.description';
94 1
        $this->chatMessage = 'expansion_admin_chat.'.strtolower($functionName).'.msg';
95 1
        $this->functionName = (string) $functionName;
96 1
        $this->parameterDescription = (string) $parameterDescription;
97 1
    }
98
99
100
    /**
101
     * @inheritdoc
102
     */
103 1
    protected function configure()
104
    {
105 1
        parent::configure();
106
107 1
        $this->inputDefinition->addArgument(
108 1
            new InputArgument('parameter', InputArgument::REQUIRED, $this->parameterDescription)
109
        );
110 1
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 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...
116
    {
117 1
        $nickName = $this->playerStorage->getPlayerInfo($login)->getNickName();
118 1
        $parameter = $input->getArgument('parameter');
119 1
        $group = $this->getGroupLabel($login);
120
        try {
121 1
            $this->factory->getConnection()->{$this->functionName}($parameter);
122 1
            $this->chatNotification->sendMessage(
123 1
                $this->chatMessage,
124 1
                $this->isPublic ? null : $login,
125 1
                ['%adminLevel%' => $group, '%admin%' => $nickName, "%parameter%" => $parameter]
126
            );
127
128 1
            $logMessage = $this->chatNotification->getMessage($this->chatMessage,
129 1
                ['%adminLevel%' => $group, '%admin%' => $nickName, "%parameter%" => $parameter], "en");
130 1
            $this->logger->info("[".$login."] ".TMString::trimStyles($logMessage));
131
132
        } catch (DedicatedException $e) {
133
            $this->logger->error("Error on admin command", ["exception" => $e]);
134
            $this->chatNotification->sendMessage("expansion_admin_chat.dedicatedexception", $login,
135
                ["%message%" => $e->getMessage()]);
136
        }
137
138 1
    }
139
}
140