modifier.escape.php ➔ smarty_modifier_escape()   C
last analyzed

Complexity

Conditions 18
Paths 18

Size

Total Lines 71
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 222.0043
Metric Value
cc 18
eloc 44
nc 18
nop 3
dl 0
loc 71
ccs 6
cts 42
cp 0.1429
crap 222.0043
rs 5.5108

How to fix   Long Method    Complexity   

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:

1
<?php
2
/**
3
 * Smarty plugin
4
 * @package Smarty
5
 * @subpackage plugins
6
 */
7
8
9
/**
10
 * Smarty escape modifier plugin
11
 *
12
 * Type:     modifier<br>
13
 * Name:     escape<br>
14
 * Purpose:  Escape the string according to escapement type
15
 * @link http://smarty.php.net/manual/en/language.modifier.escape.php
16
 *          escape (Smarty online manual)
17
 * @author   Monte Ohrt <monte at ohrt dot com>
18
 * @param string
19
 * @param html|htmlall|url|quotes|hex|hexentity|javascript
20
 * @return string
21
 */
22 1
function smarty_modifier_escape($string, $esc_type = 'html', $char_set = 'ISO-8859-1')
23
{
24
    switch ($esc_type) {
25 2
        case 'html':
26 2
            return htmlspecialchars($string, ENT_QUOTES, $char_set);
27
28 2
        case 'html_entity_decode':
29 2
            return html_entity_decode($string, ENT_QUOTES, $char_set);
0 ignored issues
show
Unused Code introduced by
The call to html_entity_decode() has too many arguments starting with $char_set.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
30
31
        case 'htmlall':
32
            return htmlentities($string, ENT_QUOTES, $char_set);
33
34
        case 'url':
35
            return rawurlencode($string);
36
37
        case 'urlpathinfo':
38
            return str_replace('%2F','/',rawurlencode($string));
39
            
40
        case 'quotes':
41
            // escape unescaped single quotes
42
            return preg_replace("%(?<!\\\\)'%", "\\'", $string);
43
44
        case 'hex':
45
            // escape every character into hex
46
            $return = '';
47
            for ($x=0; $x < strlen($string); $x++) {
48
                $return .= '%' . bin2hex($string[$x]);
49
            }
50
            return $return;
51
            
52
        case 'hexentity':
53
            $return = '';
54
            for ($x=0; $x < strlen($string); $x++) {
55
                $return .= '&#x' . bin2hex($string[$x]) . ';';
56
            }
57
            return $return;
58
59
        case 'decentity':
60
            $return = '';
61
            for ($x=0; $x < strlen($string); $x++) {
62
                $return .= '&#' . ord($string[$x]) . ';';
63
            }
64
            return $return;
65
66
        case 'javascript':
67
            // escape quotes and backslashes, newlines, etc.
68
            return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
69
            
70
        case 'mail':
71
            // safe way to display e-mail address on a web page
72
            return str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $string);
73
            
74
        case 'nonstd':
75
           // escape non-standard chars, such as ms document quotes
76
           $_res = '';
77
           for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) {
78
               $_ord = ord(substr($string, $_i, 1));
79
               // non-standard char, escape it
80
               if($_ord >= 126){
81
                   $_res .= '&#' . $_ord . ';';
82
               }
83
               else {
84
                   $_res .= substr($string, $_i, 1);
85
               }
86
           }
87
           return $_res;
88
89
        default:
90
            return $string;
91
    }
92 1
}
93
94
/* vim: set expandtab: */
95
96
?>
97