PluginMailto::process()   F
last analyzed

Complexity

Conditions 23
Paths 1729

Size

Total Lines 92

Duplication

Lines 24
Ratio 26.09 %

Importance

Changes 0
Metric Value
dl 24
loc 92
rs 0
c 0
b 0
f 0
cc 23
nc 1729
nop 9

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * Copyright (c) 2013-2017
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Functions
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2017 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.2
13
 * @date      2017-01-06
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Functions;
18
19
use Dwoo\Plugin;
20
21
/**
22
 * Outputs a mailto link with optional spam-proof (okay probably not) encoding
23
 * <pre>
24
 *  * address : target email address
25
 *  * text : display text to show for the link, defaults to the address if not provided
26
 *  * subject : the email subject
27
 *  * encode : one of the available encoding (none, js, jscharcode or hex)
28
 *  * cc : address(es) to carbon copy, comma separated
29
 *  * bcc : address(es) to blind carbon copy, comma separated
30
 *  * newsgroups : newsgroup(s) to post to, comma separated
31
 *  * followupto : address(es) to follow up, comma separated
32
 *  * extra : additional attributes to add to the &lt;a&gt; tag
33
 * </pre>
34
 * This software is provided 'as-is', without any express or implied warranty.
35
 * In no event will the authors be held liable for any damages arising from the use of this software.
36
 */
37
class PluginMailto extends Plugin
38
{
39
    /**
40
     * @param      $address
41
     * @param null $text
42
     * @param null $subject
43
     * @param null $encode
44
     * @param null $cc
45
     * @param null $bcc
46
     * @param null $newsgroups
47
     * @param null $followupto
48
     * @param null $extra
49
     *
50
     * @return string
51
     */
52
    public function process($address, $text = null, $subject = null, $encode = null, $cc = null, $bcc = null, $newsgroups = null, $followupto = null, $extra = null)
53
    {
54
        if (empty($address)) {
55
            return '';
56
        }
57
        if (empty($text)) {
58
            $text = $address;
59
        }
60
61
        // build address string
62
        $address .= '?';
63
64
        if (!empty($subject)) {
65
            $address .= 'subject=' . rawurlencode($subject) . '&';
66
        }
67
        if (!empty($cc)) {
68
            $address .= 'cc=' . rawurlencode($cc) . '&';
69
        }
70
        if (!empty($bcc)) {
71
            $address .= 'bcc=' . rawurlencode($bcc) . '&';
72
        }
73
        if (!empty($newsgroups)) {
74
            $address .= 'newsgroups=' . rawurlencode($newsgroups) . '&';
75
        }
76
        if (!empty($followupto)) {
77
            $address .= 'followupto=' . rawurlencode($followupto) . '&';
78
        }
79
80
        $address = rtrim($address, '?&');
81
82
        // output
83
        switch ($encode) {
84
85
            case 'none':
86
            case null:
87
                return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
88
89
            case 'js':
90 View Code Duplication
            case 'javascript':
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...
91
                $str = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
92
                $len = strlen($str);
93
94
                $out = '';
95
                for ($i = 0; $i < $len; ++ $i) {
96
                    $out .= '%' . bin2hex($str[$i]);
97
                }
98
99
                return '<script type="text/javascript">eval(unescape(\'' . $out . '\'));</script>';
100
101
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
102
            case 'javascript_charcode':
103
            case 'js_charcode':
104
            case 'jscharcode':
105 View Code Duplication
            case 'jschar':
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...
106
                $str = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
107
                $len = strlen($str);
108
109
                $out = '<script type="text/javascript">' . "\n<!--\ndocument.write(Str.fromCharCode(";
110
                for ($i = 0; $i < $len; ++ $i) {
111
                    $out .= ord($str[$i]) . ',';
112
                }
113
114
                return rtrim($out, ',') . "));\n-->\n</script>\n";
115
116
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
117
118
            case 'hex':
119
                if (strpos($address, '?') !== false) {
120
                    $this->core->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING);
121
                }
122
123
                $out = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
124
                $len = strlen($address);
125
                for ($i = 0; $i < $len; ++ $i) {
126
                    if (preg_match('#\w#', $address[$i])) {
127
                        $out .= '%' . bin2hex($address[$i]);
128
                    } else {
129
                        $out .= $address[$i];
130
                    }
131
                }
132
                $out .= '" ' . $extra . '>';
133
                $len = strlen($text);
134
                for ($i = 0; $i < $len; ++ $i) {
135
                    $out .= '&#x' . bin2hex($text[$i]);
136
                }
137
138
                return $out . '</a>';
139
140
            default:
141
                $this->core->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING);
142
        }
143
    }
144
}