TestMailerToFile   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 47.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 32
loc 68
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendPlain() 15 20 2
A sendHTML() 17 22 2
A writeToFile() 0 8 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
0 ignored issues
show
Coding Style introduced by
File has mixed line endings; this may cause incorrect results
Loading history...
2
/**
3
 *@author nicolaas [at] sunnysideup.co.nz
4
 * @package sapphire
5
 * @subpackage email
6
 */
7
8
class TestMailerToFile extends Mailer
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    private static $show_all_details = false;
0 ignored issues
show
Unused Code introduced by
The property $show_all_details is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
12
    private static $file_to_write_to = "assets/emails.txt";
13
14
    private static $separation_string = "
15
-------------------------------------------------------------------------------------------------------------------------------------";
16
17
    /**
18
     * Send a plain-text email
19
     */
20
    public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customheaders = false)
21
    {
22 View Code Duplication
        if ($this->Config()->get("show_all_details")) {
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...
23
            $string = "
24
~TO: $to ~
25
~FROM: $from ~
26
~SUBJECT: $subject ~
27
~PLAINTEXT: $plainContent ~
28
~ATTACHEDFILES: ".print_r($attachedFiles, 1)." ~
29
~CUSTOMHEADERS: ".print_r($customheaders, 1)." ~";
30
        } else {
31
            $string = "
32
~PLAINTEXT: TRUE
33
~TO: $to ~
34
~FROM: $from ~
35
~SUBJECT: $subject ~";
36
        }
37
        $this->writeToFile($string);
38
        return true;
39
    }
40
41
    /**
42
     * Send a multi-part HTML email
43
     */
44
    public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false, $plainContent = false, $inlineImages = false)
45
    {
46 View Code Duplication
        if ($this->Config()->get("show_all_details")) {
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...
47
            $string = "
48
~TO: $to ~
49
~FROM: $from ~
50
~SUBJECT: $subject ~
51
~HTMLCONTENT: $htmlContent ~
52
~ATTACHEDFILES: ".print_r($attachedFiles, 1)." ~
53
~CUSTOMHEADERS: ".print_r($customheaders, 1)." ~
54
~PLAINTEXT: $plainContent ~
55
~INLINEIMAGES: $inlineImages ~";
56
        } else {
57
            $string = "
58
~PLAINTEXT: FALSE
59
~TO: $to ~
60
~FROM: $from ~
61
~SUBJECT: $subject ~";
62
        }
63
        $this->writeToFile($string);
64
        return true;
65
    }
66
67
    protected function writeToFile($string)
68
    {
69
        $myFile = Director::baseFolder()."/".self::$file_to_write_to;
70
        $fh = fopen($myFile, 'a') or die("can't open file $myFile");
0 ignored issues
show
Coding Style Compatibility introduced by
The method writeToFile() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
71
        $stringData = self::$separation_string.$string;
72
        fwrite($fh, $stringData);
73
        fclose($fh);
74
    }
75
}
76