Conditions | 8 |
Paths | 13 |
Total Lines | 44 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
19 | function smarty_modifier_rot13html($str) |
||
20 | { |
||
21 | $delimiter[0][0] = '&'; // start-char |
||
|
|||
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 |
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:
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 thebar
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.