Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 42 | class Colors |
||
|
|
|||
| 43 | { |
||
| 44 | // Ansi foreground colors |
||
| 45 | private static $foreground = [ |
||
| 46 | 'black' => '0;30', |
||
| 47 | 'dark_gray' => '1;30', |
||
| 48 | 'red' => '0;31', |
||
| 49 | 'bold_red' => '1;31', |
||
| 50 | 'green' => '0;32', |
||
| 51 | 'bold_green' => '1;32', |
||
| 52 | 'brown' => '0;33', |
||
| 53 | 'yellow' => '1;33', |
||
| 54 | 'blue' => '0;34', |
||
| 55 | 'bold_blue' => '1;34', |
||
| 56 | 'purple' => '0;35', |
||
| 57 | 'bold_purple' => '1;35', |
||
| 58 | 'cyan' => '0;36', |
||
| 59 | 'bold_cyan' => '1;36', |
||
| 60 | 'white' => '1;37', |
||
| 61 | 'bold_gray' => '0;37', |
||
| 62 | ]; |
||
| 63 | |||
| 64 | // Ansi background colors |
||
| 65 | private static $background = [ |
||
| 66 | 'black' => '40', |
||
| 67 | 'red' => '41', |
||
| 68 | 'magenta' => '45', |
||
| 69 | 'yellow' => '43', |
||
| 70 | 'green' => '42', |
||
| 71 | 'blue' => '44', |
||
| 72 | 'cyan' => '46', |
||
| 73 | 'grey' => '47', |
||
| 74 | ]; |
||
| 75 | |||
| 76 | // Ansi Modifiers |
||
| 77 | private static $modifier = [ |
||
| 78 | 'reset' => '0', |
||
| 79 | 'bold' => '1', |
||
| 80 | 'dark' => '2', |
||
| 81 | 'italic' => '3', |
||
| 82 | 'underline' => '4', |
||
| 83 | 'blink' => '5', |
||
| 84 | 'blinkfast' => '6', |
||
| 85 | 'inverse' => '7', |
||
| 86 | 'strikethrough' => '9', |
||
| 87 | ]; |
||
| 88 | |||
| 89 | // Unicode Symbol Name to Octal Escape Sequence |
||
| 90 | private static $unicode = [ |
||
| 91 | 'ok' => '✓', // "check mark" - \u221A |
||
| 92 | 'fail' => '✖', // "ballot x" - \u00D7 |
||
| 93 | 'big fail' => '✖', |
||
| 94 | 'big ok' => '✔', |
||
| 95 | ]; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $symbol |
||
| 99 | * @param string[] $options |
||
| 100 | */ |
||
| 101 | public static function unicodeSymbol($symbol, $options = null) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $background |
||
| 120 | * @param string $modifiers |
||
| 121 | */ |
||
| 122 | public static function write($text, $foreground = null, $background = null, $modifiers = null) |
||
| 159 | |||
| 160 | public static function setOptions($options) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Colorizes a specific parts of a text, which are matched by search_regexp. |
||
| 172 | * |
||
| 173 | * @param string |
||
| 174 | * @param string regexp |
||
| 175 | * @param mixed|string|array |
||
| 176 | * @param string $text |
||
| 177 | * @param string $search_regexp |
||
| 178 | * @param string $color |
||
| 179 | */ |
||
| 180 | public static function colorizePart($text, $search_regexp, $color) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param int $value |
||
| 193 | */ |
||
| 194 | public static function colorizeReturnValue($value) |
||
| 198 | } |
||
| 199 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.