Completed
Push — master ( 6b7dcc...b3fe52 )
by Alexey
11:45
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
 * This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony 3
4
 * https://github.com/wow-apps/symfony-slack-bot
5
 *
6
 * (c) 2016 WoW-Apps
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WowApps\SlackBundle\Traits;
13
14
/**
15
 * Trait SlackMessageTrait
16
 *
17
 * @author Alexey Samara <[email protected]>
18
 * @package WowApps\SlackBundle
19
 */
20
trait SlackMessageTrait
21
{
22
    /**
23
     * @param array $lines
24
     * @return string
25
     */
26
    public function inlineMultilines(array $lines): string
27
    {
28
        return implode("\n", $lines);
29
    }
30
31
    /**
32
     * @param string $string
33
     * @return string
34
     */
35
    public function formatBold(string $string): string
36
    {
37
        return '*' . $string . '*';
38
    }
39
40
    /**
41
     * @param string $string
42
     * @return string
43
     */
44
    public function formatItalic(string $string): string
45
    {
46
        return '_' . $string . '_';
47
    }
48
49
    /**
50
     * @param string $string
51
     * @return string
52
     */
53
    public function formatStrikeThought(string $string): string
54
    {
55
        return '~' . $string . '~';
56
    }
57
58
    /**
59
     * @param array $list
60
     * @return string
61
     * @throws \InvalidArgumentException
62
     */
63 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...
64
    {
65
        if (empty($list)) {
66
            throw new \InvalidArgumentException('The list must contain at least one value');
67
        }
68
69
        $output = "\n";
70
        foreach ($list as $value) {
71
            $output .= "• " . $value . "\n";
72
        }
73
74
        return $output;
75
    }
76
77
    /**
78
     * @param array $list
79
     * @return string
80
     * @throws \InvalidArgumentException
81
     */
82 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...
83
    {
84
        if (empty($list)) {
85
            throw new \InvalidArgumentException('The list must contain at least one value');
86
        }
87
88
        $n = 0;
89
        $output = "\n";
90
        foreach ($list as $value) {
91
            $output .= ++$n . ". " . $value . "\n";
92
        }
93
94
        return $output;
95
    }
96
97
    /**
98
     * @param array $lines
99
     * @return string
100
     */
101
    public function formatCode(array $lines): string
102
    {
103
        $output = "\n```";
104
        $output .= $this->inlineMultilines($lines);
105
        $output .= "```\n";
106
107
        return $output;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function newLine(): string
114
    {
115
        return "\n";
116
    }
117
118
    /**
119
     * @param string $title
120
     * @param string $url
121
     * @return string
122
     */
123
    public function formatLink(string $title, string $url): string
124
    {
125
        return "<" . $url . "|" . $title . ">";
126
    }
127
128
    /**
129
     * @param string $string
130
     * @return string
131
     */
132
    public function escapeCharacters(string $string): string
133
    {
134
        $string = str_replace('&', '&amp;', $string);
135
        $string = str_replace('<', '&lt;', $string);
136
        $string = str_replace('>', '&gt;', $string);
137
        return $string;
138
    }
139
}
140