unicode::utf8convert()   C
last analyzed

Complexity

Conditions 15
Paths 72

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
cc 15
nc 72
nop 3
dl 0
loc 57
rs 5.9166
c 0
b 0
f 0
ccs 0
cts 54
cp 0
crap 240

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
	/* code by lars(at)ioflux.net, reproduced from www.php.net */
3
	/* todo: add a list of common unicode characters (&euro, etc.) */
4
5
	class unicode {
6
7
		function utf8convert($string, $maxchar=0x7F, $entities=true) {
8
9
			$returns = "";
10
			$UTF8len = array(	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11
								1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
12
								0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2,
13
								2, 2, 3, 3, 3, 3, 4, 4, 5, 6);
14
			$pos = 0;
15
			$total = strlen($string);
16
17
			do {
18
				$c = ord($string[$pos]);
19
				$len = $UTF8len[($c >> 2) & 0x3F];
20
				switch ($len)
21
				{
22
					case 6:
23
						$u = $c & 0x01;
24
						break;
25
					case 5:
26
						$u = $c & 0x03;
27
						break;
28
					case 4:
29
						$u = $c & 0x07;
30
						break;
31
					case 3:
32
						$u = $c & 0x0F;
33
						break;
34
					case 2:
35
						$u = $c & 0x1F;
36
						break;
37
					case 1:
38
						$u = $c & 0x7F;
39
						break;
40
					case 0:	/* unexpected start of a new character */
41
						$u = $c & 0x3F;
42
						$len = 5;
43
						break;
44
				}
45
				while (--$len && (++$pos < $total && $c = ord($string[$pos]))) {
46
					if (($c & 0xC0) == 0x80) {
47
						$u = ($u << 6) | ($c & 0x3F);
0 ignored issues
show
Bug introduced by
The variable $u does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
48
					} else {
49
						/* unexpected start of a new character */
50
						$pos--;
51
						break;
52
					}
53
				}
54
				if ($u <= $maxchar) {
55
					$returns .= chr($u);
56
				} else if ($entities) {
57
					$returns .= '&#'.$u.';';
58
				} else {
59
					$returns .= '?';
60
				}
61
			} while (++$pos < $total);
62
			return $returns;
63
		}
64
65
		function utf8toiso8859($string, $entities=true) {
66
			return unicode::utf8convert($string, 0xFF, $entities);
67
		}
68
69
70
		function convertToUTF8($charset, $string) {
71
		global $charset_table;
72
			if (!$charset_table[$charset]) {
73
				$tablename = preg_replace('/[^a-z0-9_-]*/', '', strtolower($charset));
74
				include("mod_unicode.$tablename.php");
75
				$charset_table[$charset] = $table;
0 ignored issues
show
Bug introduced by
The variable $table does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
76
			}
77
			if (is_string($string)) {
78
				$result = "";
79
				$strlen = strlen($string);
80
				for ($i=0; $i<$strlen; $i++) {
81
					$result .= $charset_table[$charset][ord($string[$i])];
82
				}
83
			} else {
84
				$result = $string;
85
			}
86
			return $result;
87
		}
88
	}
89
90
	class pinp_unicode extends unicode {
91
92
		function _utf8convert($string, $maxchar=0x7F, $entities=true) {
93
			return pinp_unicode::utf8convert($string, $maxchar, $entities);
94
		}
95
96
		function _utf8toiso8859($string, $entities=true) {
97
			return pinp_unicode::utf8toiso8859($string, $entities);
98
		}
99
100
		function _convertToUTF8($charset, $string) {
101
			return pinp_unicode::convertToUTF8($charset, $string);
102
		}
103
104
	}
105