Complex classes like Utils often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Utils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Utils |
||
| 10 | {
|
||
| 11 | public static function pre() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param string $str |
||
| 34 | * @param bool $methodFormat |
||
| 35 | * |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | 18 | public static function toCamelCase($str, $methodFormat = false) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @param string $class |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | 145 | public static function toSnakeCase($class) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Обрезаем текст до нужной длины по пробелу |
||
| 64 | * |
||
| 65 | * @param $str |
||
| 66 | * @param integer $n |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public static function stripText($str, $n = 150) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param $date |
||
| 84 | * @param string $defaultValue |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public static function dayBegin($date, $defaultValue = '') |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param $date |
||
| 95 | * @param string $defaultValue |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public static function dayEnd($date, $defaultValue = '31.12.2999') |
||
| 103 | |||
| 104 | 15 | public static function translite($text, $urlFormat = true) |
|
| 142 | |||
| 143 | public static function unserialize($string) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Удаляем из строки запроса набор get параметров |
||
| 150 | * |
||
| 151 | * @param $query |
||
| 152 | * @param array $params |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | 1 | public static function cutQueryParams($query, array $params) |
|
| 174 | |||
| 175 | 32 | public static function buildUrl(array $parts) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Преобразуем ссылку к стандартному формату |
||
| 190 | * |
||
| 191 | * @param $url |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | 22 | public static function normalizeUrl($url) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Преобразуем абсолютную ссылку в относительную |
||
| 213 | * |
||
| 214 | * @param string $url |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | 1 | public static function getRelativeUrl($url) |
|
| 225 | |||
| 226 | 2 | public static function generatePassword($length = 8) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Возводит первый символ строки в верхний регистр |
||
| 240 | * @param $string |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | 19 | public static function ucfirst($string) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Приводит первый символ строки к нижнему регистру |
||
| 250 | * @param $string |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | 1 | public static function lcfirst($string) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Возвращает домен |
||
| 260 | * @param integer $level если указан уровень, то домен обрезается до указанного уровня |
||
| 261 | * @return mixed|string |
||
| 262 | */ |
||
| 263 | public static function getDomain($level = null) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $number |
||
| 277 | * @param array|string $titles |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public static function plural($number, $titles = array()) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $date date in YYYY-MM-DD format |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public static function dateUntil($date) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $dateFrom date in YYYY-MM-DD format |
||
| 303 | * @param string $dateTo date in YYYY-MM-DD format |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | public static function dateBetween($dateFrom, $dateTo) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param CModel $class |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public static function modelToSnakeCase(CModel $class) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Результат сравнения 2-x объектов или массивов |
||
| 324 | * @param $a |
||
| 325 | * @param $b |
||
| 326 | * |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | public static function compareObjects($a, $b) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Отдает ответ клиенту с продолжением работы скрипта (работает тольео на php-fpm) |
||
| 342 | */ |
||
| 343 | public static function finishRequest() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Увеличивает время жизни скрипта |
||
| 357 | * @param int $timeLimitMinutes |
||
| 358 | * @param bool $ignoreUserAbort |
||
| 359 | */ |
||
| 360 | public static function longLife($timeLimitMinutes = 0, $ignoreUserAbort = true) |
||
| 365 | } |