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

LongMessageMiddleware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 39.22 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 20
loc 51
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 20 40 6

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 LongMessageMiddleware
9
 */
10
class LongMessageMiddleware implements MiddlewareInterface
11
{
12
    const MAX_CHARS = 1000;
13
    const MAX_LINES = 20;
14
    const MAX_CODE_LINES = 20;
15
16
    /**
17
     * @param Message $message
18
     * @return mixed
19
     */
20
    public function handle(Message $message)
21
    {
22
        $text = $message->text;
23
        $lines = count(explode("\n", $text));
24
        $chars = mb_strlen($text);
25
26
        if (preg_match_all('/^((`{3}\s*)(\w+)?(\s*([\w\W]+?)\n*)\2)\n*(?:[^\S\w\s]|$)/m', $text, $matches)) {
27
28
            $codeLines = 0;
29
30
            foreach ($matches[5] as $code) {
31
32
                $codeLines += count(explode("\n", $code));
33
34
            }
35
36 View Code Duplication
            if ($codeLines > self::MAX_CODE_LINES) {
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...
37
38
                $answer = \Lang::get('long.code_personal', [
39
                    'user'  => $message->user->login,
40
                ]);
41
42
                $message->italic($answer);
43
44
                return null;
45
            }
46
47 View Code Duplication
        } elseif ($lines > self::MAX_LINES || $chars > self::MAX_CHARS) {
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...
48
49
            $answer = \Lang::get('long.text_personal', [
50
                'user'  => $message->user->login,
51
            ]);
52
53
            $message->italic($answer);
54
55
            return null;
56
        }
57
58
        return $message;
59
    }
60
}
61