SwiftMailer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 35.62 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 7
dl 26
loc 73
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C send() 26 47 7

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 WZRD\Mail;
4
5
use Swift_Attachment;
6
use Swift_EmbeddedFile;
7
use Swift_Mailer;
8
use Swift_Message;
9
use WZRD\Contracts\Mail\Mailer;
10
use WZRD\Contracts\Mail\Message as MessageContract;
11
12
class SwiftMailer implements Mailer
13
{
14
    /**
15
     * Swift.
16
     *
17
     * @var Swift_Mailer
18
     */
19
    private $swift;
20
21
    /**
22
     * Construct with SwiftMailer instance.
23
     *
24
     * @param Swift_Mailer $swift
25
     */
26
    public function __construct(Swift_Mailer $swift)
27
    {
28
        $this->swift = $swift;
29
    }
30
31
    /**
32
     * Send a mail.
33
     *
34
     * @param WZRD\Contracts\Mail\Message $message
35
     * @param array                       $options
36
     */
37
    public function send(MessageContract $message, $options = [])
38
    {
39
        // Initialisation d'un nouveau message
40
        $swift_message = Swift_Message::newInstance();
41
42
        // Compose
43
        $swift_message->setFrom($message->getFrom());
0 ignored issues
show
Documentation introduced by
$message->getFrom() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        $swift_message->setTo($message->getTo());
45
        $swift_message->setCc($message->getCc());
46
        $swift_message->setBcc($message->getBcc());
47
        $swift_message->setSubject($message->getSubject());
48
        $swift_message->addPart($message->getText(), 'text/plain');
49
        $swift_message->addPart($message->getHtml(), 'text/html');
50
51
        // Attachments
52 View Code Duplication
        foreach ($message->getAttachments() as $attachment) {
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...
53
            list($file, $options) = $attachment;
54
55
            $content_type = empty($options['content-type']) ? null : $options['content-type'];
56
57
            $attachment = Swift_Attachment::fromPath($file, $content_type);
58
59
            if (!empty($options['filename'])) {
60
                $attachment->setFilename($options['filename']);
61
            }
62
63
            $swift_message->attach($attachment);
64
        }
65
66
        // Inline attachments
67 View Code Duplication
        foreach ($message->getInlines() as $inline) {
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...
68
            list($file, $options) = $inline;
69
70
            $content_type = empty($options['content-type']) ? null : $options['content-type'];
71
72
            $attachment = Swift_EmbeddedFile::fromPath($file, $content_type);
0 ignored issues
show
Unused Code introduced by
The call to Swift_EmbeddedFile::fromPath() has too many arguments starting with $content_type.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
74
            if (!empty($options['filename'])) {
75
                $attachment->setFilename($options['filename']);
76
            }
77
78
            $swift_message->embed($attachment);
79
        }
80
81
        // Send !
82
        $this->swift->send($swift_message);
83
    }
84
}
85