ChatNotification   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 11.5 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 13
loc 113
ccs 21
cts 30
cp 0.7
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessage() 0 4 1
A getMessages() 0 4 1
A __construct() 13 13 1
B sendMessage() 0 33 8

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\Framework\Core\Helpers;
4
5
use eXpansion\Framework\Core\Model\Helpers\ChatNotificationInterface;
6
use eXpansion\Framework\Core\Model\UserGroups\Group;
7
use eXpansion\Framework\Core\Services\Console;
8
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory;
9
use eXpansion\Framework\Core\Storage\PlayerStorage;
10
use Maniaplanet\DedicatedServer\Connection;
11
use Maniaplanet\DedicatedServer\InvalidArgumentException;
12
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
13
use Psr\Log\LoggerInterface;
14
15
/**
16
 * Class ChatNotification
17
 *
18
 * @package eXpansion\Framework\Core\Helpers;
19
 * @author  oliver de Cramer <[email protected]>
20
 */
21
class ChatNotification implements ChatNotificationInterface
22
{
23
    /** @var  Factory */
24
    protected $factory;
25
26
    /** @var Translations */
27
    protected $translations;
28
29
    /** @var PlayerStorage */
30
    protected $playerStorage;
31
32
    /** @var Console */
33
    protected $console;
34
    /**
35
     * @var LoggerInterface
36
     */
37
    private $logger;
38
39
    /**
40
     * ChatNotification constructor.
41
     *
42
     * @param Factory $connectionFactory
43
     * @param Translations $translations
44
     * @param PlayerStorage $playerStorage
45
     * @param Console $console
46
     * @param LoggerInterface $logger
47
     */
48 11 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...
49
        Factory $connectionFactory,
50
        Translations $translations,
51
        PlayerStorage $playerStorage,
52
        Console $console,
53
        LoggerInterface $logger
54
    ) {
55 11
        $this->factory = $connectionFactory;
56 11
        $this->translations = $translations;
57 11
        $this->playerStorage = $playerStorage;
58 11
        $this->console = $console;
59 11
        $this->logger = $logger;
60 11
    }
61
62
    /**
63
     * Send message.
64
     *
65
     * @param string                     $messageId
66
     * @param string|string[]|Group|null $to
67
     * @param string[]                   $parameters
68
     */
69 6
    public function sendMessage($messageId, $to = null, $parameters = [])
70
    {
71 6
        $message = $messageId;
72
73 6
        if (is_string($to)) {
74 2
            $player = $this->playerStorage->getPlayerInfo($to);
75 2
            $message = $this->translations->getTranslation($messageId, $parameters, strtolower($player->getLanguage()));
76
        }
77
78 6
        if (is_array($to)) {
79 2
            $to = implode(",", $to);
80 2
            $message = $this->translations->getTranslations($messageId, $parameters);
81
        }
82
83 6
        if ($to instanceof Group) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Core\Model\UserGroups\Group does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
84
            $to = implode(",", $to->getLogins());
85
            $message = $this->translations->getTranslations($messageId, $parameters);
86
        }
87
88 6
        if ($to === null || $to instanceof Group) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Core\Model\UserGroups\Group does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
89 2
            $message = $this->translations->getTranslations($messageId, $parameters);
90 2
            $this->console->writeln(end($message)['Text']);
91
        }
92
93
        try {
94 6
            $this->factory->getConnection()->chatSendServerMessage($message, $to);
0 ignored issues
show
Bug introduced by
It seems like $message can also be of type array; however, Maniaplanet\DedicatedSer...chatSendServerMessage() does only seem to accept string|array<integer,array<integer,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...
95
        } catch (UnknownPlayerException $e) {
96
            $this->logger->info("can't send chat message: $message", ["to" => $to, "exception" => $e]);
97
            // Nothing to do, it happens.
98
        } catch (InvalidArgumentException $ex) {
99
            // Nothing to do
100
        }
101 6
    }
102
103
    /**
104
     * Return messageId with arguments as a string
105
     * Usage: used for retrieving partials for chat messages
106
     *  * defaults to English locale, without parameters
107
     *
108
     * @param string $messageId
109
     * @param array  $parameters
110
     * @param string $locale
111
     * @return string
112
     */
113
    public function getMessage($messageId, $parameters = [], $locale = "en")
114
    {
115
        return $this->translations->getTranslation($messageId, $parameters, $locale);
116
    }
117
118
    /**
119
     * Return messageId with arguments as a string
120
     * Usage: used for retrieving partials for chat messages
121
     *  * defaults to English locale, without parameters
122
     *
123
     * @param string $messageId
124
     * @param array  $parameters
125
     *
126
     * @return string[]
127
     */
128
    public function getMessages($messageId, $parameters = [])
129
    {
130
        return $this->translations->getTranslations($messageId, $parameters);
131
    }
132
133
}
134