Completed
Push — master ( 6d1a51...be646c )
by Lars
02:36
created

callback_csv.php ➔ callbackAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 2
Metric Value
cc 1
eloc 8
c 5
b 3
f 2
nc 1
nop 12
dl 0
loc 40
rs 8.8571

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/* This is a sample callback function for PHPMailer-BMH (Bounce Mail Handler).
4
 * This callback function will echo the results of the BMH processing.
5
 */
6
7
/**
8
 * Callback (action) function
9
 *
10
 * @param int            $msgnum              the message number returned by Bounce Mail Handler
11
 * @param string         $bounceType          the bounce type:
12
 *                                            'antispam','autoreply','concurrent','content_reject','command_reject','internal_error','defer','delayed'
13
 *                                            =>
14
 *                                            array('remove'=>0,'bounce_type'=>'temporary'),'dns_loop','dns_unknown','full','inactive','latin_only','other','oversize','outofoffice','unknown','unrecognized','user_reject','warning'
15
 * @param string         $email               the target email address
16
 * @param string         $subject             the subject, ignore now
17
 * @param string         $xheader             the XBounceHeader from the mail
18
 * @param boolean        $remove              remove status, 1 means removed, 0 means not removed
19
 * @param string|boolean $ruleNo              Bounce Mail Handler detect rule no.
20
 * @param string|boolean $ruleCat             Bounce Mail Handler detect rule category.
21
 * @param int            $totalFetched        total number of messages in the mailbox
22
 * @param string         $body                Bounce Mail Body
23
 * @param string         $headerFull          Bounce Mail Header
24
 * @param string         $bodyFull            Bounce Mail Body (full)
25
 *
26
 * @return boolean
27
 */
28
function callbackAction($msgnum, $bounceType, $email, $subject, $xheader, $remove, $ruleNo = false, $ruleCat = false, $totalFetched = 0, $body = '', $headerFull = '', $bodyFull = '')
0 ignored issues
show
Unused Code introduced by
The parameter $xheader is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $totalFetched is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $body is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $headerFull is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $bodyFull is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
{
30
  $currentTime = date('Y-m-d H:i:s', time());
31
32
  $displayData = prepData($email, $bounceType, $remove);
33
  $bounceType = $displayData['bounce_type'];
34
  $emailName = $displayData['emailName'];
0 ignored issues
show
Unused Code introduced by
$emailName 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...
35
  $emailAddy = $displayData['emailAddy'];
0 ignored issues
show
Unused Code introduced by
$emailAddy 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...
36
  $remove = $displayData['remove'];
37
  $removeraw = $displayData['removestat'];
38
39
  $msg = $msgnum . ',' . $currentTime . ',' . $ruleNo . ',' . $ruleCat . ',' . $bounceType . ',' . $removeraw . ',' . $email . ',' . $subject;
40
41
  $filename = 'logs/bouncelog_' . date('m') . date('Y') . '.csv';
42
  if (!file_exists($filename)) {
43
    $tmsg = 'Msg#,Current Time,Rule Number,Rule Category,Bounce Type,Status,Email,Subject' . "\n" . $msg;
44
  } else {
45
    $fileContents = file_get_contents($filename);
46
47
    if (stripos($fileContents, "\n" . $msgnum . ',') !== false) {
48
      $doPutFile = false;
0 ignored issues
show
Unused Code introduced by
$doPutFile 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...
49
    }
50
    
51
    $tmsg = $msg;
52
  }
53
54
  $handle = fopen($filename, 'a');
55
  if ($handle) {
56
    if (fwrite($handle, $tmsg . "\n") === false) {
57
      echo 'Cannot write message<br />';
58
    }
59
    fclose($handle);
60
  } else {
61
    echo 'Cannot open file to append<br />';
62
  }
63
64
  echo $msgnum . ': ' . $ruleNo . ' | ' . $ruleCat . ' | ' . $bounceType . ' | ' . $remove . ' | ' . $email . ' | ' . $subject . "<br />\n";
65
66
  return true;
67
}
68
69
/**
70
 * Function to clean the data from the Callback Function for optimized display
71
 *
72
 * @param $email
73
 * @param $bounce_type
74
 * @param $remove
75
 *
76
 * @return mixed
77
 */
78 View Code Duplication
function prepData($email, $bounce_type, $remove)
0 ignored issues
show
Duplication introduced by
This function 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...
79
{
80
  $data['bounce_type'] = trim($bounce_type);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
81
  $data['email'] = '';
82
  $data['emailName'] = '';
83
  $data['emailAddy'] = '';
84
  $data['remove'] = '';
85
  if (strpos($email, '<') !== false) {
86
    $pos_start = strpos($email, '<');
87
    $data['emailName'] = trim(substr($email, 0, $pos_start));
88
    $data['emailAddy'] = substr($email, $pos_start + 1);
89
    $pos_end = strpos($data['emailAddy'], '>');
90
    if ($pos_end) {
91
      $data['emailAddy'] = substr($data['emailAddy'], 0, $pos_end);
92
    }
93
  }
94
95
  // replace the < and > able so they display on screen
96
  // replace the < and > able so they display on screen
97
  $email = str_replace(array('<', '>'), array('&lt;', '&gt;'), $email);
98
99
  // replace the "TO:<" with nothing
100
  $email = str_ireplace('TO:<', '', $email);
101
102
  $data['email'] = $email;
103
104
  // account for legitimate emails that have no bounce type
105
  if (trim($bounce_type) == '') {
106
    $data['bounce_type'] = 'none';
107
  }
108
109
  // change the remove flag from true or 1 to textual representation
110
  if (stripos($remove, 'moved') !== false && stripos($remove, 'hard') !== false) {
111
    $data['removestat'] = 'moved (hard)';
112
    $data['remove'] = '<span style="color:red;">' . 'moved (hard)' . '</span>';
113
  } elseif (stripos($remove, 'moved') !== false && stripos($remove, 'soft') !== false) {
114
    $data['removestat'] = 'moved (soft)';
115
    $data['remove'] = '<span style="color:gray;">' . 'moved (soft)' . '</span>';
116
  } elseif ($remove == true || $remove == '1') {
117
    $data['removestat'] = 'deleted';
118
    $data['remove'] = '<span style="color:red;">' . 'deleted' . '</span>';
119
  } else {
120
    $data['removestat'] = 'not deleted';
121
    $data['remove'] = '<span style="color:gray;">' . 'not deleted' . '</span>';
122
  }
123
124
  return $data;
125
}
126