Completed
Pull Request — master (#165)
by
unknown
02:54
created

OneParameterCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 116
Duplicated Lines 43.97 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 86.11%

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 6
dl 51
loc 116
rs 10
c 0
b 0
f 0
ccs 31
cts 36
cp 0.8611

3 Methods

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

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\Storage\PlayerStorage;
9
use Maniaplanet\DedicatedServer\Connection;
10
use Psr\Log\LoggerInterface;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
14
/**
15
 * Class ReasonUserCommand
16
 *
17
 * @author  Reaby
18
 * @package eXpansion\Bundle\AdminChat\ChatCommand
19
 */
20
class OneParameterCommand extends AbstractConnectionCommand
21
{
22
    /**
23
     * Description of the parameter
24
     *
25
     * @var string
26
     */
27
    protected $parameterDescription;
28
29
    /**
30
     * Description of the command.
31
     *
32
     * @var string
33
     */
34
    protected $description;
35
36
    /**
37
     * Message to display in chat.
38
     *
39
     * @var string
40
     */
41
    protected $chatMessage;
42
43
    /**
44
     * Name of the dedicated function to call.
45
     *
46
     * @var string
47
     */
48
    protected $functionName;
49
50
    /**
51
     * OneParameterCommand constructor.
52
     *
53
     * @param                  $command
54
     * @param string           $permission
55
     * @param array            $aliases
56
     * @param AdminGroups      $description
0 ignored issues
show
Documentation introduced by
There is no parameter named $description. Did you maybe mean $parameterDescription?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
57
     * @param Connection       $chatMessage
0 ignored issues
show
Bug introduced by
There is no parameter named $chatMessage. 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...
58
     * @param ChatNotification $functionName
59
     * @param PlayerStorage    $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 = $functionName;
0 ignored issues
show
Documentation Bug introduced by
It seems like $functionName of type object<eXpansion\Framewo...lpers\ChatNotification> is incompatible with the declared type string of property $functionName.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
95 1
        $this->parameterDescription = $parameterDescription;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parameterDescription of type object<eXpansion\Framewo...\Storage\PlayerStorage> is incompatible with the declared type string of property $parameterDescription.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
        } catch (\Exception $e) {
127
            $this->chatNotification->sendMessage(
128
                'expansion_admin_chat.dedicatedexception',
129
                $login,
130
                ['%message%' => $e->getMessage()]
131
            );
132
        }
133
134 1
    }
135
}
136