Conditions | 19 |
Paths | 9 |
Total Lines | 90 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
26 | public function transliterate($string, $unknown = '?', $source_langcode = null) |
||
27 | { |
||
28 | // ASCII is always valid NFC! If we're only ever given plain ASCII, we can |
||
29 | // avoid the overhead of initializing the decomposition tables by skipping |
||
30 | // out early. |
||
31 | if (!preg_match('/[\x80-\xff]/', $string)) { |
||
32 | return $string; |
||
33 | } |
||
34 | |||
35 | $tail_bytes = $this->getTailBytes(); |
||
36 | $areas = $this->getAreas($string); |
||
37 | $result = ''; |
||
38 | foreach ($areas[0] as $str) { |
||
39 | |||
40 | if ($str[0] < "\x80") { |
||
41 | // ASCII chunk: guaranteed to be valid UTF-8 and in normal form C, so |
||
42 | // skip over it. |
||
43 | $result .= $str; |
||
44 | continue; |
||
45 | } |
||
46 | |||
47 | // We'll have to examine the chunk byte by byte to ensure that it consists |
||
48 | // of valid UTF-8 sequences, and to see if any of them might not be normalized. |
||
49 | // Since PHP is not the fastest language on earth, some of this code is a |
||
50 | // little ugly with inner loop optimizations. |
||
51 | $head = ''; |
||
52 | $chunk = strlen($str); |
||
53 | |||
54 | // Counting down is faster. I'm *so* sorry. |
||
55 | $len = $chunk + 1; |
||
56 | for ($i = -1; --$len;) { |
||
57 | $c = $str[++$i]; |
||
58 | if ($remaining = $tail_bytes[$c]) { |
||
59 | // UTF-8 head byte! |
||
60 | $sequence = $head = $c; |
||
61 | do { |
||
62 | // Look for the defined number of tail bytes... |
||
63 | if (--$len && ($c = $str[++$i]) >= "\x80" && $c < "\xc0") { |
||
64 | // Legal tail bytes are nice. |
||
65 | $sequence .= $c; |
||
66 | } else { |
||
67 | if ($len == 0) { |
||
68 | // Premature end of string! Drop a replacement character into |
||
69 | // output to represent the invalid UTF-8 sequence. |
||
70 | $result .= $unknown; |
||
71 | break 2; |
||
72 | } else { |
||
73 | // Illegal tail byte; abandon the sequence. |
||
74 | $result .= $unknown; |
||
75 | // Back up and reprocess this byte; it may itself be a legal |
||
76 | // ASCII or UTF-8 sequence head. |
||
77 | --$i; |
||
78 | ++$len; |
||
79 | continue 2; |
||
80 | } |
||
81 | } |
||
82 | } while (--$remaining); |
||
83 | $n = ord($head); |
||
84 | if ($n <= 0xdf) { |
||
85 | $ord = ($n - 192) * 64 + (ord($sequence[1]) - 128); |
||
86 | } elseif ($n <= 0xef) { |
||
87 | $ord = ($n - 224) * 4096 + (ord($sequence[1]) - 128) * 64 + (ord($sequence[2]) - 128); |
||
88 | } elseif ($n <= 0xf7) { |
||
89 | $ord = ($n - 240) * 262144 + (ord($sequence[1]) - 128) * 4096 + (ord($sequence[2]) - 128) * 64 + (ord($sequence[3]) - 128); |
||
90 | } elseif ($n <= 0xfb) { |
||
91 | $ord = ($n - 248) * 16777216 + (ord($sequence[1]) - 128) * 262144 + (ord($sequence[2]) - 128) * 4096 + (ord($sequence[3]) - 128) * 64 + (ord($sequence[4]) - 128); |
||
92 | } elseif ($n <= 0xfd) { |
||
93 | $ord = ($n - 252) * 1073741824 + (ord($sequence[1]) - 128) * 16777216 + (ord($sequence[2]) - 128) * 262144 + (ord($sequence[3]) - 128) * 4096 + (ord($sequence[4]) - 128) * 64 + (ord($sequence[5]) - 128); |
||
94 | } |
||
95 | $result .= $this->replace($ord, $unknown, $source_langcode); |
||
|
|||
96 | $head = ''; |
||
97 | } elseif ($c < "\x80") { |
||
98 | // ASCII byte. |
||
99 | $result .= $c; |
||
100 | $head = ''; |
||
101 | } elseif ($c < "\xc0") { |
||
102 | // Illegal tail bytes. |
||
103 | if ($head == '') { |
||
104 | $result .= $unknown; |
||
105 | } |
||
106 | } else { |
||
107 | // Miscellaneous freaks. |
||
108 | $result .= $unknown; |
||
109 | $head = ''; |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | return $result; |
||
115 | } |
||
116 | |||
206 |
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: