Completed
Push — develop ( b4fba5...9773af )
by Kirill
12:38
created

GitterRoomListenCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 6
c 5
b 0
f 1
lcom 1
cbo 8
dl 18
loc 90
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 18 18 2
A listen() 0 17 1
A onMessageFallback() 0 18 2
A onMessage() 0 4 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
namespace App\Console\Commands;
3
4
5
use App\Room;
6
use Gitter\Client;
7
use React\EventLoop\LoopInterface;
8
use Gitter\Models\Room as GitterRoom;
9
use Gitter\Models\Message as GitterMessage;
10
use Illuminate\Contracts\Config\Repository;
11
use Illuminate\Contracts\Container\Container;
12
use Gitter\Iterators\PromiseIterator\Controls;
13
14
15
16
/**
17
 * Class GitterRoomListenCommand
18
 * @package App\Console\Commands
19
 *
20
 * Прослушивание сообщений требуемой комнаты
21
 */
22
class GitterRoomListenCommand extends AbstractCommand
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $signature = 'gitter:listen {room}';
28
29
    /**
30
     * @var string
31
     */
32
    protected $description = 'Listen gitter room';
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @param Repository $config
38
     * @param Container $container
39
     *
40
     * @return mixed
41
     */
42 View Code Duplication
    public function handle(Repository $config, Container $container)
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...
43
    {
44
        /** @var Client $client */
45
        $client = $this->createClient($container, $config->get('gitter.token'));
46
47
        $this->findRoom($client, $this->argument('room'), function(GitterRoom $room) {
0 ignored issues
show
Bug introduced by
It seems like $this->argument('room') targeting Illuminate\Console\Command::argument() can also be of type array; however, App\Console\Commands\AbstractCommand::findRoom() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48
            try {
49
                $this->listen($room);
50
            } catch (\Throwable $e) {
51
                $this->error($e->getMessage());
52
            }
53
        });
54
55
56
        /** @var LoopInterface $loop */
57
        $loop = $container->make(LoopInterface::class);
58
        $loop->run();
59
    }
60
61
    /**
62
     * @param GitterRoom $gitter
63
     */
64
    protected function listen(GitterRoom $gitter)
65
    {
66
        /** @var Room $room */
67
        $room = Room::make($gitter, function(Room $room) {
0 ignored issues
show
Unused Code introduced by
$room is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
The parameter $room is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
            //
69
        });
70
71
        /** @var LoopInterface $loop */
72
        $loop = app(LoopInterface::class);
73
74
75
        $gitter->onMessage(function(GitterMessage $msg) {
76
            $this->onMessage($msg);
77
        }, function(\Throwable $e) use ($loop, $gitter) {
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
            $this->onMessageFallback($loop, $gitter);
79
        });
80
    }
81
82
    /**
83
     * Message stream throw http api
84
     *
85
     * @param LoopInterface $loop
86
     * @param GitterRoom $gitter
87
     */
88
    protected function onMessageFallback(LoopInterface $loop, GitterRoom $gitter)
89
    {
90
        $this->warn('Stream not available. Use message fallback fetch mode.');
91
92
        $gitter->getMessages()
93
            ->fetch(function(GitterMessage $message, Controls $controls) {
94
                $this->onMessage($message);
95
96
                if ($controls->index() <= 100) {
97
                    $controls->next();
98
                }
99
            });
100
101
        $loop->addTimer(10, function() use ($gitter) {
0 ignored issues
show
Documentation introduced by
10 is of type integer, but the function expects a object<React\EventLoop\numeric>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
            $this->warn('Connection retries.');
103
            $this->listen($gitter);
104
        });
105
    }
106
107
    protected function onMessage(GitterMessage $gitter)
0 ignored issues
show
Unused Code introduced by
The parameter $gitter is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        //
110
    }
111
}
112
113
114
115
116
117