Test Setup Failed
Push — master ( 66f5b7...72fefc )
by Alexey
11:50
created

SlackMessageTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 120
Duplicated Lines 22.5 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 0
dl 27
loc 120
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A inlineMultilines() 0 4 1
A formatBold() 0 4 1
A formatItalic() 0 4 1
A formatStrikeThought() 0 4 1
A formatListMarker() 13 13 3
A formatListNumeric() 14 14 3
A formatCode() 0 8 1
A newLine() 0 4 1
A formatLink() 0 4 1
A escapeCharacters() 0 7 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
3
namespace WowApps\SlackBotBundle\Traits;
4
5
trait SlackMessageTrait
6
{
7
    /**
8
     * @param array $lines
9
     * @return string
10
     */
11
    public function inlineMultilines(array $lines): string
12
    {
13
        return implode("\n", $lines);
14
    }
15
16
    /**
17
     * @param string $string
18
     * @return string
19
     */
20
    public function formatBold(string $string): string
21
    {
22
        return '*' . $string . '*';
23
    }
24
25
    /**
26
     * @param string $string
27
     * @return string
28
     */
29
    public function formatItalic(string $string): string
30
    {
31
        return '_' . $string . '_';
32
    }
33
34
    /**
35
     * @param string $string
36
     * @return string
37
     */
38
    public function formatStrikeThought(string $string): string
39
    {
40
        return '~' . $string . '~';
41
    }
42
43
    /**
44
     * @param array $list
45
     * @return string
46
     * @throws \InvalidArgumentException
47
     */
48 View Code Duplication
    public function formatListMarker(array $list): string
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
    {
50
        if (empty($list)) {
51
            throw new \InvalidArgumentException('The list must contain at least one value');
52
        }
53
54
        $output = "\n";
55
        foreach ($list as $value) {
56
            $output .= "• " . $value . "\n";
57
        }
58
59
        return $output;
60
    }
61
62
    /**
63
     * @param array $list
64
     * @return string
65
     * @throws \InvalidArgumentException
66
     */
67 View Code Duplication
    public function formatListNumeric(array $list): string
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...
68
    {
69
        if (empty($list)) {
70
            throw new \InvalidArgumentException('The list must contain at least one value');
71
        }
72
73
        $n = 0;
74
        $output = "\n";
75
        foreach ($list as $value) {
76
            $output .= ++$n . ". " . $value . "\n";
77
        }
78
79
        return $output;
80
    }
81
82
    /**
83
     * @param array $lines
84
     * @return string
85
     */
86
    public function formatCode(array $lines): string
87
    {
88
        $output = "\n```";
89
        $output .= $this->inlineMultilines($lines);
90
        $output .= "```\n";
91
92
        return $output;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function newLine(): string
99
    {
100
        return "\n";
101
    }
102
103
    /**
104
     * @param string $title
105
     * @param string $url
106
     * @return string
107
     */
108
    public function formatLink(string $title, string $url): string
109
    {
110
        return "<" . $url . "|" . $title . ">";
111
    }
112
113
    /**
114
     * @param string $string
115
     * @return string
116
     */
117
    public function escapeCharacters(string $string): string
118
    {
119
        $string = str_replace('&', '&amp;', $string);
120
        $string = str_replace('<', '&lt;', $string);
121
        $string = str_replace('>', '&gt;', $string);
122
        return $string;
123
    }
124
}
125