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:
Complex classes like ImportHelper 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 ImportHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class ImportHelper |
||
| 9 | {
|
||
| 10 | private static $urlCache; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @param $folder |
||
| 14 | * @param array $extensions |
||
| 15 | * |
||
| 16 | * @return null |
||
| 17 | * @internal param string $extension |
||
| 18 | */ |
||
| 19 | public static function getFileLastModified($folder, $extensions = array('csv'))
|
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param $folder |
||
| 36 | * @param array $extensions |
||
| 37 | * |
||
| 38 | * @return array |
||
| 39 | * @internal param string $extension |
||
| 40 | */ |
||
| 41 | public static function getFiles($folder, $extensions = array('csv'))
|
||
| 53 | |||
| 54 | /** |
||
| 55 | * Преобразует номер столбца в индекс колонки exсel таблици A - 0, B - 1 |
||
| 56 | * |
||
| 57 | * @param $letters |
||
| 58 | * @param bool $firstZero индексация начинается с 0 |
||
| 59 | * |
||
| 60 | * @return int |
||
| 61 | */ |
||
| 62 | public static function lettersToNumber($letters, $firstZero = true) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Преобразует индекс колонки exсel в номер столбца в 1 - A, 2 - B |
||
| 77 | * |
||
| 78 | * @param $number |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public static function numberToLetters($number) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Преобразует дивпазон индексов колонок exсel в массив |
||
| 100 | * например 'a-c' в array('A', 'B', 'C');
|
||
| 101 | * |
||
| 102 | * @param $string |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public static function getLettersRange($string) |
||
| 125 | |||
| 126 | public static function getModelWithoutBehaviors($className, $scenario = null) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $array |
||
| 143 | * |
||
| 144 | * @return mixed |
||
| 145 | */ |
||
| 146 | public static function convertColumnIndexes($array) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Оборачивает путь в слеши, исключаея дублирование |
||
| 168 | * |
||
| 169 | * @param $path |
||
| 170 | * @param string $wrapper |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public static function wrapInSlash($path, $wrapper = DIRECTORY_SEPARATOR) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Оборачивает начало путь в слеш, исключаея дублирование |
||
| 181 | * |
||
| 182 | * @param $path |
||
| 183 | * @param string $wrapper |
||
| 184 | * @param bool $clearEnd |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | View Code Duplication | public static function wrapInSlashBegin($path, $wrapper = DIRECTORY_SEPARATOR, $clearEnd = true) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Оборачивает конец путь в слеш, исключаея дублирование |
||
| 200 | * @param $path |
||
| 201 | * @param string $wrapper |
||
| 202 | * @param bool $clearBegin |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | View Code Duplication | public static function wrapInSlashEnd($path, $wrapper = DIRECTORY_SEPARATOR, $clearBegin = true) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $table |
||
| 218 | * @param $url |
||
| 219 | * @param bool|false $warmUpCache - прогреть |
||
| 220 | * |
||
| 221 | * @return mixed|string |
||
| 222 | */ |
||
| 223 | public static function createUniqueUrl($table, $url, $warmUpCache = false) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param $table|null "null" clears all |
||
| 263 | */ |
||
| 264 | public static function clearUrlCache($table) |
||
| 271 | |||
| 272 | private static function getUrls($table, $url = null) |
||
| 284 | } |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: