Completed
Push — master ( a1f3ed...74c577 )
by Kirill
02:49
created

GoogleSearchMiddleware   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 12.2 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 2
dl 5
loc 41
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 5 34 7

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 Domains\Bot\Middlewares;
3
4
use Domains\Message;
5
use Interfaces\Gitter\Middleware\MiddlewareInterface;
6
7
/**
8
 * Class GoogleSearchMiddleware
9
 */
10
class GoogleSearchMiddleware implements MiddlewareInterface
11
{
12
    /**
13
     * @param Message $message
14
     * @return mixed
15
     */
16
    public function handle(Message $message)
17
    {
18
        $text = $message->escaped_text;
19
20
        if (preg_match('/^(@.*?\s)?(?:погугли|загугли|гугли)\s(.*?)$/isu', $text, $matches)) {
21
            if (!trim($matches[2])) {
22
                return $message;
23
            }
24
25
            $hasMentions = count($message->mentions);
26
            $mention = null;
27
28 View Code Duplication
            if ($hasMentions) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
29
                $mention = $message->mentions[0]->login === \Auth::user()->login
30
                    ? $message->user
31
                    : $message->mentions[0];
32
            }
33
34
            $answer = trim($matches[1]) && $mention
35
                ? \Lang::get('google.personal', [
36
                    'user'  => $mention->login,
37
                    'query' => urlencode($matches[2]),
38
                ])
39
                : \Lang::get('google.common', [
40
                    'query' => urlencode($matches[2]),
41
                ]);
42
43
            $message->answer($answer);
44
45
            return null;
46
        }
47
48
        return $message;
49
    }
50
}
51