Completed
Branch master (b65d76)
by David
04:29
created

PluginMailto.php ➔ PluginMailto()   F

Complexity

Conditions 23
Paths 1729

Size

Total Lines 92
Code Lines 58

Duplication

Lines 24
Ratio 26.09 %

Importance

Changes 0
Metric Value
cc 23
eloc 58
nc 1729
nop 10
dl 24
loc 92
rs 2
c 0
b 0
f 0

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-2016
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-2016 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.0
13
 * @date      2016-09-19
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Functions;
18
19
use Dwoo\Core;
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
function PluginMailto(Core $dwoo, $address, $text = null, $subject = null, $encode = null, $cc = null, $bcc = null, $newsgroups = null, $followupto = null, $extra = null)
38
{
39
    if (empty($address)) {
40
        return '';
41
    }
42
    if (empty($text)) {
43
        $text = $address;
44
    }
45
46
    // build address string
47
    $address .= '?';
48
49
    if (!empty($subject)) {
50
        $address .= 'subject=' . rawurlencode($subject) . '&';
51
    }
52
    if (!empty($cc)) {
53
        $address .= 'cc=' . rawurlencode($cc) . '&';
54
    }
55
    if (!empty($bcc)) {
56
        $address .= 'bcc=' . rawurlencode($bcc) . '&';
57
    }
58
    if (!empty($newsgroups)) {
59
        $address .= 'newsgroups=' . rawurlencode($newsgroups) . '&';
60
    }
61
    if (!empty($followupto)) {
62
        $address .= 'followupto=' . rawurlencode($followupto) . '&';
63
    }
64
65
    $address = rtrim($address, '?&');
66
67
    // output
68
    switch ($encode) {
69
70
        case 'none':
71
        case null:
72
            return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
73
74
        case 'js':
75 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...
76
            $str = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
77
            $len = strlen($str);
78
79
            $out = '';
80
            for ($i = 0; $i < $len; ++ $i) {
81
                $out .= '%' . bin2hex($str[$i]);
82
            }
83
84
            return '<script type="text/javascript">eval(unescape(\'' . $out . '\'));</script>';
85
86
            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...
87
        case 'javascript_charcode':
88
        case 'js_charcode':
89
        case 'jscharcode':
90 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...
91
            $str = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
92
            $len = strlen($str);
93
94
            $out = '<script type="text/javascript">' . "\n<!--\ndocument.write(String.fromCharCode(";
95
            for ($i = 0; $i < $len; ++ $i) {
96
                $out .= ord($str[$i]) . ',';
97
            }
98
99
            return rtrim($out, ',') . "));\n-->\n</script>\n";
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
103
        case 'hex':
104
            if (strpos($address, '?') !== false) {
105
                $dwoo->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING);
106
            }
107
108
            $out = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
109
            $len = strlen($address);
110
            for ($i = 0; $i < $len; ++ $i) {
111
                if (preg_match('#\w#', $address[$i])) {
112
                    $out .= '%' . bin2hex($address[$i]);
113
                } else {
114
                    $out .= $address[$i];
115
                }
116
            }
117
            $out .= '" ' . $extra . '>';
118
            $len = strlen($text);
119
            for ($i = 0; $i < $len; ++ $i) {
120
                $out .= '&#x' . bin2hex($text[$i]);
121
            }
122
123
            return $out . '</a>';
124
125
        default:
126
            $dwoo->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);
127
    }
128
}
129