| Conditions | 1 |
| Paths | 1 |
| Total Lines | 61 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 18 | public function highlight($s) |
||
| 19 | { |
||
| 20 | $s = htmlspecialchars($s, ENT_COMPAT); |
||
| 21 | |||
| 22 | // Workaround for escaped backslashes |
||
| 23 | $s = str_replace('\\\\', '\\\\<e>', $s); |
||
| 24 | |||
| 25 | $regexp = array( |
||
| 26 | // Numbers (also look for Hex) |
||
| 27 | '/(?<!\w)( |
||
| 28 | (0x|\#)[\da-f]+| |
||
| 29 | \d+| |
||
| 30 | \d+(px|em|cm|mm|rem|s|\%) |
||
| 31 | )(?!\w)/ix' |
||
| 32 | => '<span style="color:#8CD0D3;">$1</span>', |
||
| 33 | |||
| 34 | // Make the bold assumption that an |
||
| 35 | // all uppercase word has a special meaning |
||
| 36 | '/(?<!\w|>|\#)( |
||
| 37 | [A-Z_0-9]{2,} |
||
| 38 | )(?!\w)/x' |
||
| 39 | => '<span style="color:#FFFFFF">$1</span>', |
||
| 40 | |||
| 41 | // Keywords |
||
| 42 | '/(?<!\w|\$|\%|\@|>)( |
||
| 43 | and|or|xor|for|do|while|foreach|as|return|die|exit|if|then|else| |
||
| 44 | elseif|new|delete|try|throw|catch|finally|class|function|string| |
||
| 45 | array|object|resource|var|bool|boolean|int|integer|float|double| |
||
| 46 | real|string|array|global|const|static|public|private|protected| |
||
| 47 | published|extends|switch|true|false|null|void|this|self|struct| |
||
| 48 | char|signed|unsigned|short|long |
||
| 49 | )(?!\w|=")/ix' |
||
| 50 | => '<span style="color:#DFC47D">$1</span>', |
||
| 51 | |||
| 52 | // PHP/Perl-Style Vars: $var, %var, @var |
||
| 53 | '/(?<!\w)( |
||
| 54 | (\$|\%|\@)(\->|\w)+ |
||
| 55 | )(?!\w)/ix' |
||
| 56 | => '<span style="color:#CEDF99">$1</span>', |
||
| 57 | ); |
||
| 58 | |||
| 59 | // Comments/Strings |
||
| 60 | $s = preg_replace_callback('/( |
||
| 61 | \/\*.*?\*\/| |
||
| 62 | \/\/.*?\n| |
||
| 63 | \#.[^a-fA-F0-9]+?\n| |
||
| 64 | \<\!\-\-[\s\S]+\-\-\>| |
||
| 65 | (?<!\\\)".*?(?<!\\\)"| |
||
| 66 | (?<!\\\)\'(.*?)(?<!\\\)\' |
||
| 67 | )/isx', [$this, 'replaceId'], $s); |
||
| 68 | |||
| 69 | $s = preg_replace(array_keys($regexp), array_values($regexp), $s); |
||
| 70 | |||
| 71 | // Paste the comments and strings back in again |
||
| 72 | $s = str_replace(array_keys($this->tokens), array_values($this->tokens), $s); |
||
| 73 | |||
| 74 | // Delete the "Escaped Backslash Workaround Token" (TM) |
||
| 75 | // and replace tabs with four spaces. |
||
| 76 | $s = str_replace(array('<e>', "\t"), array('', ' '), $s); |
||
| 77 | |||
| 78 | return $s; |
||
| 79 | } // end highlight |
||
| 102 |