Swift5   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 9.46 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 9
dl 7
loc 74
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMailer() 7 19 4
B getMessage() 0 26 6
A send() 0 4 1
A __construct() 0 7 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
 * Jaeger
4
 *
5
 * @copyright	Copyright (c) 2015-2016, mithra62
6
 * @link		http://jaeger-app.com
7
 * @version		1.0
8
 * @filesource 	./Email/Swift5.php
9
 */
10
namespace JaegerApp\Email;
11
12
use Swift_SmtpTransport;
13
use Swift_MailTransport;
14
use Swift_Message;
15
use Swift_Attachment;
16
use Swift_Mailer;
17
use Swift_Plugins_Loggers_ArrayLogger;
18
use Swift_Plugins_LoggerPlugin;
19
20
/**
21
 * Jaeger - Swift5 Email Abstraction
22
 *
23
 * Defines what email objects should contain
24
 *
25
 * @package Email
26
 * @author Eric Lamb <[email protected]>
27
 */
28
class Swift5 extends SwiftAbstract
29
{
30
    /**
31
     * Set it up
32
     * @param unknown $config
33
     */
34
    public function __construct($config = array())
35
    {
36
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config can also be of type object<JaegerApp\Email\unknown>. However, the property $config is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
37
        if( !class_exists('\Swift_Mailer') ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
38
            //require_once dirname(__FILE__).'../../../vendor/swiftmailer/swiftmailer/lib/swift_required.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
        }
40
    }
41
    
42
    /**
43
     * (non-PHPdoc)
44
     * @see \JaegerApp\Email\SwiftAbstract::getMailer()
45
     */
46
    public function getMailer()
47
    {
48
        if(is_null($this->mailer))
49
        {
50 View Code Duplication
            if (isset($this->config['type']) && $this->config['type'] == 'smtp') {
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...
51
                $transport = \Swift_SmtpTransport::newInstance($this->config['smtp_options']['host'], $this->config['smtp_options']['port']);
52
                $transport->setUsername($this->config['smtp_options']['connection_config']['username']);
53
                $transport->setPassword($this->config['smtp_options']['connection_config']['password']);
54
            } else {
55
                $transport = \Swift_MailTransport::newInstance();
56
            }
57
            
58
            $this->mailer = \Swift_Mailer::newInstance($transport);
59
            $this->mailer_logger = new \Swift_Plugins_Loggers_ArrayLogger();
0 ignored issues
show
Bug introduced by
The property mailer_logger does not seem to exist. Did you mean mailer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
60
            $this->mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($this->mailer_logger));
0 ignored issues
show
Bug introduced by
The property mailer_logger does not seem to exist. Did you mean mailer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
61
        }
62
        
63
        return $this->mailer;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->mailer; (Swift_Mailer) is incompatible with the return type declared by the abstract method JaegerApp\Email\SwiftAbstract::getMailer of type JaegerApp\Email\Swift_Mailer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
64
    }
65
    
66
    /**
67
     * (non-PHPdoc)
68
     * @see \JaegerApp\Email\SwiftAbstract::getMessage()
69
     */
70
    public function getMessage(array $to, $from_email, $from_name, $subject, $message_body, array $attachments, $mail_type='html')
71
    {
72
        $message = \Swift_Message::newInstance();
73
        $message->setTo($to);
74
        if ($attachments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attachments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
75
            foreach ($attachments as $attachment) {
76
                foreach ($attachment as $file => $alt_name) {
77
                    if ($alt_name == '') {
78
                        $message->attach(\Swift_Attachment::fromPath($file));
79
                    } else {
80
                        $message->attach(\Swift_Attachment::fromPath($file)->setFilename($alt_name));
81
                    }
82
                }
83
            }
84
        }
85
        
86
        $message->setFrom($from_email, $from_name);
87
        $message->setSubject($subject);
88
        if ($mail_type == 'html') {
89
            $message->setBody($message_body, 'text/html');
90
        } else {
91
            $message->setBody($message_body);
92
        }
93
        
94
        return $message;
95
    }
96
    
97
    public function send($message, $extra = null)
98
    {
99
        return $this->getMailer()->send($message);
100
    }
101
}