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 |
||
| 17 | class Str |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Date placeholder replacement. |
||
| 21 | * Replaces %{somevalue} with date({somevalue}). |
||
| 22 | * |
||
| 23 | * @param string $string |
||
| 24 | * @param mixed <integer|null> $time |
||
| 25 | * @return string |
||
| 26 | */ |
||
| 27 | 70 | public static function replaceDatePlaceholders($string, $time = null) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Does a given string contain a date placeholder. |
||
| 41 | * |
||
| 42 | * @param string $string |
||
| 43 | * @return bool |
||
| 44 | */ |
||
| 45 | 43 | public static function isContainingPlaceholder($string) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Replaces %TARGET_DIR% and %TARGET_FILE% in given string. |
||
| 52 | * |
||
| 53 | * @param string $string |
||
| 54 | * @param string $target |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | 4 | public static function replaceTargetPlaceholders($string, $target) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Create a regex that matches the raw path considering possible date placeholders. |
||
| 67 | * |
||
| 68 | * @param string $stringWithDatePlaceholders |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | 9 | public static function datePlaceholdersToRegex($stringWithDatePlaceholders) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Converts a given value to boolean. |
||
| 79 | * |
||
| 80 | * @param string $value |
||
| 81 | * @param bool $default |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | 103 | public static function toBoolean($value, $default) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Return given size in bytes. |
||
| 99 | * Allowed units: |
||
| 100 | * B => byte |
||
| 101 | * K => kilo byte |
||
| 102 | * M => mega byte |
||
| 103 | * G => giga byte |
||
| 104 | * T => terra byte |
||
| 105 | * P => peta byte |
||
| 106 | * |
||
| 107 | * e.g. |
||
| 108 | * 1K => 1024 |
||
| 109 | * 2K => 2048 |
||
| 110 | * ... |
||
| 111 | * |
||
| 112 | * @param string $value |
||
| 113 | * @throws \RuntimeException |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | 10 | View Code Duplication | public static function toBytes($value) |
| 127 | |||
| 128 | /** |
||
| 129 | * Return time in seconds for a given value. |
||
| 130 | * Allowed units: |
||
| 131 | * S => second |
||
| 132 | * I => minute |
||
| 133 | * D => day |
||
| 134 | * W => week |
||
| 135 | * M => month |
||
| 136 | * Y => year |
||
| 137 | * |
||
| 138 | * e.g. |
||
| 139 | * 2I => 120 |
||
| 140 | * 10D => 864000 |
||
| 141 | * ... |
||
| 142 | * |
||
| 143 | * @param string $offset |
||
| 144 | * @throws \RuntimeException |
||
| 145 | * @return integer |
||
| 146 | */ |
||
| 147 | 9 | View Code Duplication | public static function toTime($offset) |
| 158 | |||
| 159 | /** |
||
| 160 | * Pads all given strings to given length. |
||
| 161 | * |
||
| 162 | * @param array $strings |
||
| 163 | * @param integer $length |
||
| 164 | * @param string $pad |
||
| 165 | * @param integer $mode |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | 1 | public static function padAll(array $strings, $length, $pad = ' ', $mode = STR_PAD_LEFT) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Explodes string to array but empty string results in empty array not array with empty string in it. |
||
| 179 | * |
||
| 180 | * @param string $separated |
||
| 181 | * @param string $separator |
||
| 182 | * @param boolean $trim |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | 65 | public static function toList($separated, $separator = ',', $trim = true) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Determine if the path has a trailing slash. |
||
| 196 | * |
||
| 197 | * @param string $string |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | 28 | public static function hasTrailingSlash(string $string) : bool |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Adds trailing slash to a string/path if not already there. |
||
| 207 | * |
||
| 208 | * @param string $string |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | 21 | public static function withTrailingSlash($string) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Removes the trailing slash from a string/path. |
||
| 218 | * |
||
| 219 | * @param string $string |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | 6 | public static function withoutTrailingSlash($string) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Determine if the path has a leading slash. |
||
| 229 | * |
||
| 230 | * @param string $string |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | 9 | public static function hasLeadingSlash(string $string) : bool |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Adds leading slash to a string/path if not already there. |
||
| 240 | * |
||
| 241 | * @param string $string |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | 6 | public static function withLeadingSlash($string) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Removes the leading slash from a string/path. |
||
| 251 | * |
||
| 252 | * @param string $string |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | 1 | public static function withoutLeadingSlash($string) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Appends a plural "s" or "'s". |
||
| 262 | * |
||
| 263 | * @param string $subject |
||
| 264 | * @param int $amount |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | 6 | public static function appendPluralS($subject, $amount) |
|
| 271 | } |
||
| 272 |