Completed
Push — master ( 996d1c...c64174 )
by Hilmi Erdem
01:46
created

ShortMessage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 12.73 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 0
dl 14
loc 110
ccs 27
cts 27
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A body() 0 4 1
A hasManyReceivers() 0 4 1
A receivers() 0 4 1
A receiversString() 0 4 1
A toArray() 0 7 1
A toSingleMessageXml() 7 7 1
A toMultipleMessagesXml() 7 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 Erdemkeren\JetSms;
4
5
/**
6
 * Class ShortMessage.
7
 */
8
class ShortMessage
9
{
10
    /**
11
     * The short message body.
12
     *
13
     * @var string
14
     */
15
    protected $body;
16
17
    /**
18
     * The receivers.
19
     *
20
     * @var array
21
     */
22
    protected $receivers;
23
24
    /**
25
     * ShortMessage constructor.
26
     *
27
     * @param string|array $receivers
28
     * @param string       $body
29
     */
30 6
    public function __construct($receivers, $body)
31
    {
32 6
        $this->body = $body;
33 6
        $this->receivers = is_array($receivers)
34 6
            ? array_map('trim', $receivers)
35 6
            : [trim($receivers)];
36 6
    }
37
38
    /**
39
     * Get the body of the short message.
40
     *
41
     * @return string
42
     */
43 6
    public function body()
44
    {
45 6
        return $this->body;
46
    }
47
48
    /**
49
     * Determine if the short message has many receivers or not.
50
     *
51
     * @return bool
52
     */
53 1
    public function hasManyReceivers()
54
    {
55 1
        return count($this->receivers()) > 1;
56
    }
57
58
    /**
59
     * Get the receivers of the short message.
60
     *
61
     * @return array
62
     */
63 2
    public function receivers()
64
    {
65 2
        return $this->receivers;
66
    }
67
68
    /**
69
     * Get the receivers of the short message as concatenated string.
70
     *
71
     * @param  string|null $glue
72
     * @return string
73
     */
74 6
    public function receiversString($glue = null)
75
    {
76 6
        return implode($glue, $this->receivers);
77
    }
78
79
    /**
80
     * Get the array representation of the short message.
81
     *
82
     * @return array
83
     */
84 1
    public function toArray()
85
    {
86 1
        return array_filter([
87 1
            'Msisdns'        => $this->receiversString('|'),
88 1
            'Messages'       => $this->body(),
89 1
        ]);
90
    }
91
92
    /**
93
     * Get the single message xml representation of the short message.
94
     *
95
     * @return string
96
     */
97 1 View Code Duplication
    public function toSingleMessageXml()
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...
98
    {
99 1
        $text = str_replace("'", "&apos;", htmlentities($this->body()));
100 1
        $gsmNo = $this->receiversString(',');
101
102 1
        return "<text>{$text}</text><message><gsmnos>{$gsmNo}</gsmnos></message>";
103
    }
104
105
    /**
106
     * Get the multiple messages xml representation of the short message.
107
     *
108
     * @return string
109
     */
110 1 View Code Duplication
    public function toMultipleMessagesXml()
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...
111
    {
112 1
        $text = str_replace("'", "&apos;", htmlentities($this->body()));
113 1
        $gsmNo = $this->receiversString(',');
114
115 1
        return "<message><gsmno>{$gsmNo}</gsmno><text>{$text}</text></message>";
116
    }
117
}
118