UtilCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 22.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 9
loc 40
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A processCoin() 0 17 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
 * T3Bot.
4
 *
5
 * @author Frank Nägler <[email protected]>
6
 *
7
 * @link http://www.t3bot.de
8
 * @link http://wiki.typo3.org/T3Bot
9
 */
10
namespace T3Bot\Commands;
11
12
use Slack\Payload;
13
use Slack\RealTimeClient;
14
15
/**
16
 * Class UtilCommand.
17
 *
18
 * @property string commandName
19
 * @property array helpCommands
20
 */
21
class UtilCommand extends AbstractCommand
22
{
23
    /**
24
     * AbstractCommand constructor.
25
     *
26
     * @param Payload $payload
27
     * @param RealTimeClient $client
28
     * @param array|null $configuration
29
     */
30 6 View Code Duplication
    public function __construct(Payload $payload, RealTimeClient $client, array $configuration = null)
31
    {
32 6
        $this->commandName = 'util';
33 6
        $this->helpCommands = [
34
            'help' => 'shows this help',
35
            'coin [options]' => 'coin toss with [options] (separate by comma)',
36
        ];
37 6
        parent::__construct($payload, $client, $configuration);
38 6
    }
39
40
    /**
41
     * @return string
42
     */
43 5
    protected function processCoin() : string
44
    {
45 5
        $params = $this->params;
46 5
        array_shift($params);
47 5
        $params = implode(' ', $params);
48 5
        $options = array_map('trim', explode(',', $params));
49 5
        if (count($options) === 1) {
50 2
            return '*Botty says:* _A complicated decision ..._';
51
        }
52 3
        if (count(array_count_values($options)) === 1) {
53 1
            return '*Botty says:* _it is undecidable ..._';
54
        }
55
56 2
        $option = $options[random_int(0, count($options) - 1)];
57
58 2
        return '*Botty says:* _' . $option . '_';
59
    }
60
}
61