Completed
Push — development ( d2a06d...c1f21f )
by Thomas
06:37
created

modifier.rot13html.php ➔ smarty_modifier_rot13html()   C

Complexity

Conditions 8
Paths 13

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 13
nop 1
dl 0
loc 44
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package Smarty
6
 * @subpackage plugins
7
 *
8
 * Smarty plugin
9
 *
10
 * Type:     modifier<br>
11
 * Name:     rot13html<br>
12
 * Example:  {$text|rot13html}
13
 * @version  1.0
14
 *
15
 * @param string
16
 *
17
 * @return string
18
 */
19
function smarty_modifier_rot13html($str)
20
{
21
    $delimiter[0][0] = '&'; // start-char
0 ignored issues
show
Coding Style Comprehensibility introduced by
$delimiter was never initialized. Although not strictly required by PHP, it is generally a good practice to add $delimiter = 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...
22
    $delimiter[0][1] = ';'; // end-char
23
    $delimiter[1][0] = '<';
24
    $delimiter[1][1] = '>';
25
    $delimiter[2][0] = '[';
26
    $delimiter[2][1] = ']';
27
28
    $returnValue = '';
29
30
    while (mb_strlen($returnValue) < mb_strlen($str)) {
31
        $nNextStart = false;
32
        $sNextEndChar = '';
33
        foreach ($delimiter as $del) {
34
            $nThisStart = mb_strpos($str, $del[0], mb_strlen($returnValue));
35
36
            if ($nThisStart !== false) {
37
                if (($nNextStart > $nThisStart) || ($nNextStart === false)) {
38
                    $nNextStart = $nThisStart;
39
                    $sNextEndChar = $del[1];
40
                }
41
            }
42
        }
43
44
        if ($nNextStart === false) {
45
            $returnValue .= str_rot13(mb_substr($str, mb_strlen($returnValue), mb_strlen($str) - mb_strlen($returnValue)));
46
        } else {
47
            // crypted part
48
            $returnValue .= str_rot13(mb_substr($str, mb_strlen($returnValue), $nNextStart - mb_strlen($returnValue)));
49
50
            // uncrypted part
51
            $nNextEnd = mb_strpos($str, $sNextEndChar, $nNextStart);
52
53
            if ($nNextEnd === false) {
54
                $returnValue .= mb_substr($str, $nNextStart, mb_strlen($str) - mb_strlen($returnValue));
55
            } else {
56
                $returnValue .= mb_substr($str, $nNextStart, $nNextEnd - $nNextStart + 1);
57
            }
58
        }
59
    }
60
61
    return $returnValue;
62
}
63