Message   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 263
Duplicated Lines 14.83 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 19
c 7
b 0
f 3
lcom 2
cbo 6
dl 39
loc 263
ccs 71
cts 71
cp 1
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 6 1
A getHeaders() 0 7 2
A addHeader() 0 7 1
A setFrom() 9 9 1
A getFromAddressList() 0 6 1
A setTo() 0 7 1
A getToAddressList() 0 6 1
A addTo() 9 9 1
A addCc() 7 7 1
A addBcc() 7 7 1
A addReplyTo() 7 7 1
A setSubject() 0 6 1
A getSubject() 0 5 1
A setBody() 0 6 1
A getBodyText() 0 8 2
A getHeader() 0 9 2

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
/**
4
 * This file is part of slick/mail package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mail;
11
12
use Slick\Mail\Header\AddressList;
13
use Slick\Mail\Header\AddressListInterface;
14
use Slick\Mail\Header\GenericHeader;
15
use Slick\Mail\Header\HeaderInterface;
16
17
/**
18
 * Wraps an e-mail message
19
 *
20
 * @package Slick\Mail
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
class Message implements MessageInterface
24
{
25
26
    /**
27
     * @var HeaderInterface[]
28
     */
29
    protected $headers;
30
31
    /**
32
     * @var MessageBodyInterface
33
     */
34
    protected $body;
35
36
    /**
37
     * Is the message valid?
38
     *
39
     * If we don't have any From addresses, we're invalid, according
40
     * to RFC2822.
41
     *
42
     * @return bool
43
     */
44 2
    public function isValid()
45
    {
46
        /** @var AddressList $from */
47 2
        $from = $this->getHeader('From', new AddressList('From'));
48 2
        return !$from->isEmpty();
49
    }
50
51
    /**
52
     * Access headers collection
53
     *
54
     * Lazy-loads if not already attached.
55
     *
56
     * @return HeaderInterface[]
57
     */
58 26
    public function getHeaders()
59
    {
60 26
        if (null == $this->headers) {
61 8
            $this->headers['Date'] = New GenericHeader('Date', date('r'));
62 4
        }
63 26
        return $this->headers;
64
    }
65
66
    /**
67
     * Adds an header with provided value
68
     *
69
     * If key already exists it will be overridden
70
     *
71
     * @param string $name
72
     * @param HeaderInterface $header
73
     *
74
     * @return MessageInterface
75
     */
76 12
    public function addHeader($name, HeaderInterface $header)
77
    {
78 12
        $headers = $this->getHeaders();
79 12
        $headers[$name] = $header;
80 12
        $this->headers = $headers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $headers of type array<string,object<Slic...eader\HeaderInterface>> is incompatible with the declared type array<integer,object<Sli...eader\HeaderInterface>> of property $headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81 12
        return $this;
82
    }
83
84
    /**
85
     * Set (overwrite) From addresses
86
     *
87
     * @param  string $emailAddress
88
     * @param  string|null $name
89
     *
90
     * @return MessageInterface
91
     */
92 6 View Code Duplication
    public function setFrom($emailAddress, $name = null)
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...
93
    {
94 6
        $address = new AddressList\Address($emailAddress, $name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 92 can also be of type string; however, Slick\Mail\Header\Addres...\Address::__construct() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
95 6
        $from = (new AddressList('From'))->add($address);
96 6
        $this->addHeader('From', $from);
97 6
        $sender = new GenericHeader('Sender', (string) $address);
98 6
        $this->addHeader('Sender', $sender);
99 6
        return $this;
100
    }
101
102
    /**
103
     * Access the address list of the From header.
104
     *
105
     * @return string
106
     */
107 4
    public function getFromAddressList()
108
    {
109
        /** @var AddressList $from */
110 4
        $from = $this->getHeader('From', new AddressList('From'));
111 4
        return trim(str_replace('From:', '', $from->toString()));
112
    }
113
114
    /**
115
     * Overwrite the address list in the To recipients
116
     *
117
     * @param  string $email
118
     * @param  string|null $name
119
     *
120
     * @return MessageInterface
121
     */
122 2
    public function setTo($email, $name = null)
123
    {
124 2
        $toHeader = (new AddressList('To'))
125 2
            ->add(new AddressList\Address($email, $name));
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 122 can also be of type string; however, Slick\Mail\Header\Addres...\Address::__construct() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
126 2
        $this->addHeader('To', $toHeader);
127 2
        return $this;
128
    }
129
130
    /**
131
     * Access the address list of the To header.
132
     *
133
     * @return string
134
     */
135 6
    public function getToAddressList()
136
    {
137
        /** @var AddressList $from */
138 6
        $from = $this->getHeader('To', new AddressList('To'));
139 6
        return trim(str_replace('To:', '', $from->toString()));
140
    }
141
142
    /**
143
     * Add one or more addresses to the To recipients
144
     *
145
     * Appends to the list.
146
     *
147
     * @param  string $email
148
     * @param  null|string $name
149
     *
150
     * @return MessageInterface
151
     */
152 8 View Code Duplication
    public function addTo($email, $name = null)
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...
153
    {
154 8
        $address = new AddressList\Address($email, $name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 152 can also be of type string; however, Slick\Mail\Header\Addres...\Address::__construct() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
155
        /** @var AddressListInterface $toHeader */
156 8
        $toHeader = $this->getHeader('To', new AddressList('To'));
157 8
        $toHeader->add($address);
158 8
        $this->addHeader('To', $toHeader);
159 8
        return $this;
160
    }
161
162
    /**
163
     * Add a "Cc" address
164
     *
165
     * @param  string $email
166
     * @param  null|string $name
167
     *
168
     * @return MessageInterface
169
     */
170 2 View Code Duplication
    public function addCc($email, $name = null)
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...
171
    {
172
        /** @var AddressList $header */
173 2
        $header = $this->getHeader('Cc', new AddressList('Cc'));
174 2
        $header->add(new AddressList\Address($email, $name));
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 170 can also be of type string; however, Slick\Mail\Header\Addres...\Address::__construct() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
175 2
        return $this;
176
    }
177
178
    /**
179
     * Add a "Bcc" address
180
     *
181
     * @param  string $email
182
     * @param  null|string $name
183
     *
184
     * @return MessageInterface
185
     */
186 2 View Code Duplication
    public function addBcc($email, $name = null)
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...
187
    {
188
        /** @var AddressList $header */
189 2
        $header = $this->getHeader('Bcc', new AddressList('Bcc'));
190 2
        $header->add(new AddressList\Address($email, $name));
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 186 can also be of type string; however, Slick\Mail\Header\Addres...\Address::__construct() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
191 2
        return $this;
192
    }
193
194
    /**
195
     * Add one or more addresses to the Reply-To recipients
196
     *
197
     * Appends to the list.
198
     *
199
     * @param  string $email
200
     * @param  null|string $name
201
     *
202
     * @return MessageInterface
203
     */
204 2 View Code Duplication
    public function addReplyTo($email, $name = null)
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...
205
    {
206
        /** @var AddressList $header */
207 2
        $header = $this->getHeader('Reply-To', new AddressList('Reply-To'));
208 2
        $header->add(new AddressList\Address($email, $name));
0 ignored issues
show
Bug introduced by
It seems like $name defined by parameter $name on line 204 can also be of type string; however, Slick\Mail\Header\Addres...\Address::__construct() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
209 2
        return $this;
210
    }
211
212
    /**
213
     * Set the message subject header value
214
     *
215
     * @param  string $subject
216
     *
217
     * @return MessageInterface
218
     */
219 6
    public function setSubject($subject)
220
    {
221 6
        $subject = new GenericHeader('Subject', $subject);
222 6
        $this->addHeader('Subject', $subject);
223 6
        return $this;
224
    }
225
226
    /**
227
     * Get the message subject header value
228
     *
229
     * @return null|string
230
     */
231 4
    public function getSubject()
232
    {
233 4
        $subject = $this->getHeader('Subject');
234 4
        return $subject->getValue();
235
    }
236
237
    /**
238
     * Set the message body
239
     *
240
     * @param  MessageBodyInterface $body
241
     *
242
     * @return MessageInterface
243
     */
244 8
    public function setBody(MessageBodyInterface $body)
245
    {
246 8
        $body->setMailMessage($this);
247 8
        $this->body = $body;
248 8
        return $this;
249
    }
250
251
    /**
252
     * Get the string-serialized message body text
253
     *
254
     * @return string
255
     */
256 6
    public function getBodyText()
257
    {
258 6
        $text = '';
259 6
        if (null != $this->body) {
260 6
            $text = $this->body->getBodyText();
261 3
        }
262 6
        return $text;
263
    }
264
265
    /**
266
     * Gets the header with provided name.
267
     *
268
     * If no header was set with that name, the default value will be
269
     * assign to a new header and returned.
270
     *
271
     * @param string $name
272
     * @param mixed $default
273
     *
274
     * @return HeaderInterface
275
     */
276 20
    protected function getHeader($name, $default = '')
277
    {
278 20
        if (array_key_exists($name, $this->getHeaders())) {
279 10
            return $this->headers[$name];
280
        }
281
282 14
        $this->headers[$name] = $default;
283 14
        return $default;
284
    }
285
}