Completed
Pull Request — master (#39)
by Patrick
02:37
created

DBEmail::getRawBodyText()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Email;
3
require_once('Autoload.php');
4
5
abstract class DBEmail extends \Email\Email
6
{
7
    protected $dbData;
8
9
    public function __construct($datasetName, $datatableName, $dataRowName)
10
    {
11
        parent::__construct();
12
        $dataTable = \DataSetFactory::getDataTableByNames($datasetName, $datatableName);
13
        $this->dbData = $dataTable->read(new \Data\Filter('id eq '.$dataRowName));
14
        if($this->dbData === false)
15
        {
16
            throw new \Exception('Unknown dataRow identified by ID '.$dataRowName);
17
        }
18
    }
19
20
    public function getFromAddress()
21
    {
22
        if(isset($this->dbData['from']))
23
        {
24
            return $this->dbData['from'];;
25
        }
26
        return parent::getFromAddress();
27
    }
28
29
    public function getReplyTo()
30
    {
31
        if(isset($this->dbData['replyTo']))
32
        {
33
            return $this->dbData['replyTo'];;
34
        }
35
        return parent::getReplyTo();
36
    }
37
38
    public function getSubject()
39
    {
40
        if(isset($this->dbData['subject']))
41
        {
42
            return $this->dbData['subject'];
43
        }
44
        return parent::getSubject();
45
    }
46
47
    public abstract function getSubstituteVars();
48
49
    protected function getRawBodyText($html)
50
    {
51
        if(isset($this->dbData['body']))
52
        {
53
            return $this->dbData;
54
        }
55
        if($html === true)
56
        {
57
            return $this->htmlBody;
58
        }
59
        return $this->textBody;
60
    }
61
62
    protected function getBodyFromDB($html=true)
63
    {
64
        $vars = $this->getSubstituteVars();
65
        $rawText = $this->getRawBodyText($html);
0 ignored issues
show
Unused Code introduced by
$rawText is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
        if($html === true)
67
        {
68
            $text = strtr($raw_text, $vars);
0 ignored issues
show
Bug introduced by
The variable $raw_text seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
69
            return $text;
70
        }
71
        $index = strpos($raw_text, "<script");
0 ignored issues
show
Bug introduced by
The variable $raw_text seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
72
        if($index !== false)
73
        {
74
            $end = strpos($raw_text, "</script>");
0 ignored issues
show
Bug introduced by
The variable $raw_text seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
75
            if($index === 0)
76
            {
77
                $raw_text = substr($raw_text, $end+9);
0 ignored issues
show
Bug introduced by
The variable $raw_text seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
78
            }
79
        }
80
        return strtr(strip_tags($raw_text), $vars);
0 ignored issues
show
Bug introduced by
The variable $raw_text does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
81
    }
82
83
    public function getHTMLBody()
84
    {
85
        return $this->getBodyFromDB();
86
    }
87
88
    public function getTextBody()
89
    {
90
        return $this->getBodyFromDB(false);
91
    }
92
}
93
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
94