Complex classes like BaseFileHelper 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 BaseFileHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class BaseFileHelper |
||
25 | { |
||
26 | const PATTERN_NODIR = 1; |
||
27 | const PATTERN_ENDSWITH = 4; |
||
28 | const PATTERN_MUSTBEDIR = 8; |
||
29 | const PATTERN_NEGATIVE = 16; |
||
30 | const PATTERN_CASE_INSENSITIVE = 32; |
||
31 | |||
32 | /** |
||
33 | * @var string the path (or alias) of a PHP file containing MIME type information. |
||
34 | */ |
||
35 | public static $mimeMagicFile = '@yii/helpers/mimeTypes.php'; |
||
36 | |||
37 | /** |
||
38 | * @var string the path (or alias) of a PHP file containing MIME aliases. |
||
39 | * @since 2.0.14 |
||
40 | */ |
||
41 | public static $mimeAliasesFile = '@yii/helpers/mimeAliases.php'; |
||
42 | |||
43 | |||
44 | /** |
||
45 | * Normalizes a file/directory path. |
||
46 | * |
||
47 | * The normalization does the following work: |
||
48 | * |
||
49 | * - Convert all directory separators into `DIRECTORY_SEPARATOR` (e.g. "\a/b\c" becomes "/a/b/c") |
||
50 | * - Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c") |
||
51 | * - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c") |
||
52 | * - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c") |
||
53 | * |
||
54 | * @param string $path the file/directory path to be normalized |
||
55 | * @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`. |
||
56 | * @return string the normalized file/directory path |
||
57 | */ |
||
58 | 34 | public static function normalizePath($path, $ds = DIRECTORY_SEPARATOR) |
|
78 | |||
79 | /** |
||
80 | * Returns the localized version of a specified file. |
||
81 | * |
||
82 | * The searching is based on the specified language code. In particular, |
||
83 | * a file with the same name will be looked for under the subdirectory |
||
84 | * whose name is the same as the language code. For example, given the file "path/to/view.php" |
||
85 | * and language code "zh-CN", the localized file will be looked for as |
||
86 | * "path/to/zh-CN/view.php". If the file is not found, it will try a fallback with just a language code that is |
||
87 | * "zh" i.e. "path/to/zh/view.php". If it is not found as well the original file will be returned. |
||
88 | * |
||
89 | * If the target and the source language codes are the same, |
||
90 | * the original file will be returned. |
||
91 | * |
||
92 | * @param string $file the original file |
||
93 | * @param string $language the target language that the file should be localized to. |
||
94 | * If not set, the value of [[\yii\base\Application::language]] will be used. |
||
95 | * @param string $sourceLanguage the language that the original file is in. |
||
96 | * If not set, the value of [[\yii\base\Application::sourceLanguage]] will be used. |
||
97 | * @return string the matching localized file, or the original file if the localized version is not found. |
||
98 | * If the target and the source language codes are the same, the original file will be returned. |
||
99 | */ |
||
100 | 72 | public static function localize($file, $language = null, $sourceLanguage = null) |
|
124 | |||
125 | /** |
||
126 | * Determines the MIME type of the specified file. |
||
127 | * This method will first try to determine the MIME type based on |
||
128 | * [finfo_open](http://php.net/manual/en/function.finfo-open.php). If the `fileinfo` extension is not installed, |
||
129 | * it will fall back to [[getMimeTypeByExtension()]] when `$checkExtension` is true. |
||
130 | * @param string $file the file name. |
||
131 | * @param string $magicFile name of the optional magic database file (or alias), usually something like `/path/to/magic.mime`. |
||
132 | * This will be passed as the second parameter to [finfo_open()](http://php.net/manual/en/function.finfo-open.php) |
||
133 | * when the `fileinfo` extension is installed. If the MIME type is being determined based via [[getMimeTypeByExtension()]] |
||
134 | * and this is null, it will use the file specified by [[mimeMagicFile]]. |
||
135 | * @param bool $checkExtension whether to use the file extension to determine the MIME type in case |
||
136 | * `finfo_open()` cannot determine it. |
||
137 | * @return string the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined. |
||
138 | * @throws InvalidConfigException when the `fileinfo` PHP extension is not installed and `$checkExtension` is `false`. |
||
139 | */ |
||
140 | 25 | public static function getMimeType($file, $magicFile = null, $checkExtension = true) |
|
165 | |||
166 | /** |
||
167 | * Determines the MIME type based on the extension name of the specified file. |
||
168 | * This method will use a local map between extension names and MIME types. |
||
169 | * @param string $file the file name. |
||
170 | * @param string $magicFile the path (or alias) of the file that contains all available MIME type information. |
||
171 | * If this is not set, the file specified by [[mimeMagicFile]] will be used. |
||
172 | * @return string|null the MIME type. Null is returned if the MIME type cannot be determined. |
||
173 | */ |
||
174 | 8 | public static function getMimeTypeByExtension($file, $magicFile = null) |
|
187 | |||
188 | /** |
||
189 | * Determines the extensions by given MIME type. |
||
190 | * This method will use a local map between extension names and MIME types. |
||
191 | * @param string $mimeType file MIME type. |
||
192 | * @param string $magicFile the path (or alias) of the file that contains all available MIME type information. |
||
193 | * If this is not set, the file specified by [[mimeMagicFile]] will be used. |
||
194 | * @return array the extensions corresponding to the specified MIME type |
||
195 | */ |
||
196 | 12 | public static function getExtensionsByMimeType($mimeType, $magicFile = null) |
|
206 | |||
207 | private static $_mimeTypes = []; |
||
208 | |||
209 | /** |
||
210 | * Loads MIME types from the specified file. |
||
211 | * @param string $magicFile the path (or alias) of the file that contains all available MIME type information. |
||
212 | * If this is not set, the file specified by [[mimeMagicFile]] will be used. |
||
213 | * @return array the mapping from file extensions to MIME types |
||
214 | */ |
||
215 | 20 | protected static function loadMimeTypes($magicFile) |
|
227 | |||
228 | private static $_mimeAliases = []; |
||
229 | |||
230 | /** |
||
231 | * Loads MIME aliases from the specified file. |
||
232 | * @param string $aliasesFile the path (or alias) of the file that contains MIME type aliases. |
||
233 | * If this is not set, the file specified by [[mimeAliasesFile]] will be used. |
||
234 | * @return array the mapping from file extensions to MIME types |
||
235 | * @since 2.0.14 |
||
236 | */ |
||
237 | 12 | protected static function loadMimeAliases($aliasesFile) |
|
249 | |||
250 | /** |
||
251 | * Copies a whole directory as another one. |
||
252 | * The files and sub-directories will also be copied over. |
||
253 | * @param string $src the source directory |
||
254 | * @param string $dst the destination directory |
||
255 | * @param array $options options for directory copy. Valid options are: |
||
256 | * |
||
257 | * - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775. |
||
258 | * - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting. |
||
259 | * - filter: callback, a PHP callback that is called for each directory or file. |
||
260 | * The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered. |
||
261 | * The callback can return one of the following values: |
||
262 | * |
||
263 | * * true: the directory or file will be copied (the "only" and "except" options will be ignored) |
||
264 | * * false: the directory or file will NOT be copied (the "only" and "except" options will be ignored) |
||
265 | * * null: the "only" and "except" options will determine whether the directory or file should be copied |
||
266 | * |
||
267 | * - only: array, list of patterns that the file paths should match if they want to be copied. |
||
268 | * A path matches a pattern if it contains the pattern string at its end. |
||
269 | * For example, '.php' matches all file paths ending with '.php'. |
||
270 | * Note, the '/' characters in a pattern matches both '/' and '\' in the paths. |
||
271 | * If a file path matches a pattern in both "only" and "except", it will NOT be copied. |
||
272 | * - except: array, list of patterns that the files or directories should match if they want to be excluded from being copied. |
||
273 | * A path matches a pattern if it contains the pattern string at its end. |
||
274 | * Patterns ending with '/' apply to directory paths only, and patterns not ending with '/' |
||
275 | * apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b'; |
||
276 | * and '.svn/' matches directory paths ending with '.svn'. Note, the '/' characters in a pattern matches |
||
277 | * both '/' and '\' in the paths. |
||
278 | * - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true. |
||
279 | * - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true. |
||
280 | * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. |
||
281 | * If the callback returns false, the copy operation for the sub-directory or file will be cancelled. |
||
282 | * The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or |
||
283 | * file to be copied from, while `$to` is the copy target. |
||
284 | * - afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied. |
||
285 | * The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or |
||
286 | * file copied from, while `$to` is the copy target. |
||
287 | * - copyEmptyDirectories: boolean, whether to copy empty directories. Set this to false to avoid creating directories |
||
288 | * that do not contain files. This affects directories that do not contain files initially as well as directories that |
||
289 | * do not contain files at the target destination because files have been filtered via `only` or `except`. |
||
290 | * Defaults to true. This option is available since version 2.0.12. Before 2.0.12 empty directories are always copied. |
||
291 | * @throws \yii\base\InvalidParamException if unable to open directory |
||
292 | */ |
||
293 | 17 | public static function copyDirectory($src, $dst, $options = []) |
|
349 | |||
350 | /** |
||
351 | * Removes a directory (and all its content) recursively. |
||
352 | * |
||
353 | * @param string $dir the directory to be deleted recursively. |
||
354 | * @param array $options options for directory remove. Valid options are: |
||
355 | * |
||
356 | * - traverseSymlinks: boolean, whether symlinks to the directories should be traversed too. |
||
357 | * Defaults to `false`, meaning the content of the symlinked directory would not be deleted. |
||
358 | * Only symlink would be removed in that default case. |
||
359 | * |
||
360 | * @throws ErrorException in case of failure |
||
361 | */ |
||
362 | 121 | public static function removeDirectory($dir, $options = []) |
|
390 | |||
391 | /** |
||
392 | * Removes a file or symlink in a cross-platform way |
||
393 | * |
||
394 | * @param string $path |
||
395 | * @return bool |
||
396 | * |
||
397 | * @since 2.0.14 |
||
398 | */ |
||
399 | 84 | public static function unlink($path) |
|
420 | |||
421 | /** |
||
422 | * Returns the files found under the specified directory and subdirectories. |
||
423 | * @param string $dir the directory under which the files will be looked for. |
||
424 | * @param array $options options for file searching. Valid options are: |
||
425 | * |
||
426 | * - `filter`: callback, a PHP callback that is called for each directory or file. |
||
427 | * The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered. |
||
428 | * The callback can return one of the following values: |
||
429 | * |
||
430 | * * `true`: the directory or file will be returned (the `only` and `except` options will be ignored) |
||
431 | * * `false`: the directory or file will NOT be returned (the `only` and `except` options will be ignored) |
||
432 | * * `null`: the `only` and `except` options will determine whether the directory or file should be returned |
||
433 | * |
||
434 | * - `except`: array, list of patterns excluding from the results matching file or directory paths. |
||
435 | * Patterns ending with slash ('/') apply to directory paths only, and patterns not ending with '/' |
||
436 | * apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b'; |
||
437 | * and `.svn/` matches directory paths ending with `.svn`. |
||
438 | * If the pattern does not contain a slash (`/`), it is treated as a shell glob pattern |
||
439 | * and checked for a match against the pathname relative to `$dir`. |
||
440 | * Otherwise, the pattern is treated as a shell glob suitable for consumption by `fnmatch(3)` |
||
441 | * with the `FNM_PATHNAME` flag: wildcards in the pattern will not match a `/` in the pathname. |
||
442 | * For example, `views/*.php` matches `views/index.php` but not `views/controller/index.php`. |
||
443 | * A leading slash matches the beginning of the pathname. For example, `/*.php` matches `index.php` but not `views/start/index.php`. |
||
444 | * An optional prefix `!` which negates the pattern; any matching file excluded by a previous pattern will become included again. |
||
445 | * If a negated pattern matches, this will override lower precedence patterns sources. Put a backslash (`\`) in front of the first `!` |
||
446 | * for patterns that begin with a literal `!`, for example, `\!important!.txt`. |
||
447 | * Note, the '/' characters in a pattern matches both '/' and '\' in the paths. |
||
448 | * - `only`: array, list of patterns that the file paths should match if they are to be returned. Directory paths |
||
449 | * are not checked against them. Same pattern matching rules as in the `except` option are used. |
||
450 | * If a file path matches a pattern in both `only` and `except`, it will NOT be returned. |
||
451 | * - `caseSensitive`: boolean, whether patterns specified at `only` or `except` should be case sensitive. Defaults to `true`. |
||
452 | * - `recursive`: boolean, whether the files under the subdirectories should also be looked for. Defaults to `true`. |
||
453 | * @return array files found under the directory, in no particular order. Ordering depends on the files system used. |
||
454 | * @throws InvalidParamException if the dir is invalid. |
||
455 | */ |
||
456 | 71 | public static function findFiles($dir, $options = []) |
|
489 | |||
490 | /** |
||
491 | * Checks if the given file path satisfies the filtering options. |
||
492 | * @param string $path the path of the file or directory to be checked |
||
493 | * @param array $options the filtering options. See [[findFiles()]] for explanations of |
||
494 | * the supported options. |
||
495 | * @return bool whether the file or directory satisfies the filtering options. |
||
496 | */ |
||
497 | 80 | public static function filterPath($path, $options) |
|
529 | |||
530 | /** |
||
531 | * Creates a new directory. |
||
532 | * |
||
533 | * This method is similar to the PHP `mkdir()` function except that |
||
534 | * it uses `chmod()` to set the permission of the created directory |
||
535 | * in order to avoid the impact of the `umask` setting. |
||
536 | * |
||
537 | * @param string $path path of the directory to be created. |
||
538 | * @param int $mode the permission to be set for the created directory. |
||
539 | * @param bool $recursive whether to create parent directories if they do not exist. |
||
540 | * @return bool whether the directory is created successfully |
||
541 | * @throws \yii\base\Exception if the directory could not be created (i.e. php error due to parallel changes) |
||
542 | */ |
||
543 | 173 | public static function createDirectory($path, $mode = 0775, $recursive = true) |
|
568 | |||
569 | /** |
||
570 | * Performs a simple comparison of file or directory names. |
||
571 | * |
||
572 | * Based on match_basename() from dir.c of git 1.8.5.3 sources. |
||
573 | * |
||
574 | * @param string $baseName file or directory name to compare with the pattern |
||
575 | * @param string $pattern the pattern that $baseName will be compared against |
||
576 | * @param int|bool $firstWildcard location of first wildcard character in the $pattern |
||
577 | * @param int $flags pattern flags |
||
578 | * @return bool whether the name matches against pattern |
||
579 | */ |
||
580 | 59 | private static function matchBasename($baseName, $pattern, $firstWildcard, $flags) |
|
601 | |||
602 | /** |
||
603 | * Compares a path part against a pattern with optional wildcards. |
||
604 | * |
||
605 | * Based on match_pathname() from dir.c of git 1.8.5.3 sources. |
||
606 | * |
||
607 | * @param string $path full path to compare |
||
608 | * @param string $basePath base of path that will not be compared |
||
609 | * @param string $pattern the pattern that path part will be compared against |
||
610 | * @param int|bool $firstWildcard location of first wildcard character in the $pattern |
||
611 | * @param int $flags pattern flags |
||
612 | * @return bool whether the path part matches against pattern |
||
613 | */ |
||
614 | 43 | private static function matchPathname($path, $basePath, $pattern, $firstWildcard, $flags) |
|
655 | |||
656 | /** |
||
657 | * Scan the given exclude list in reverse to see whether pathname |
||
658 | * should be ignored. The first match (i.e. the last on the list), if |
||
659 | * any, determines the fate. Returns the element which |
||
660 | * matched, or null for undecided. |
||
661 | * |
||
662 | * Based on last_exclude_matching_from_list() from dir.c of git 1.8.5.3 sources. |
||
663 | * |
||
664 | * @param string $basePath |
||
665 | * @param string $path |
||
666 | * @param array $excludes list of patterns to match $path against |
||
667 | * @return array|null null or one of $excludes item as an array with keys: 'pattern', 'flags' |
||
668 | * @throws InvalidParamException if any of the exclude patterns is not a string or an array with keys: pattern, flags, firstWildcard. |
||
669 | */ |
||
670 | 60 | private static function lastExcludeMatchingFromList($basePath, $path, $excludes) |
|
697 | |||
698 | /** |
||
699 | * Processes the pattern, stripping special characters like / and ! from the beginning and settings flags instead. |
||
700 | * @param string $pattern |
||
701 | * @param bool $caseSensitive |
||
702 | * @throws \yii\base\InvalidParamException |
||
703 | * @return array with keys: (string) pattern, (int) flags, (int|bool) firstWildcard |
||
704 | */ |
||
705 | 60 | private static function parseExcludePattern($pattern, $caseSensitive) |
|
744 | |||
745 | /** |
||
746 | * Searches for the first wildcard character in the pattern. |
||
747 | * @param string $pattern the pattern to search in |
||
748 | * @return int|bool position of first wildcard character or false if not found |
||
749 | */ |
||
750 | 60 | private static function firstWildcardInPattern($pattern) |
|
761 | |||
762 | /** |
||
763 | * @param array $options raw options |
||
764 | * @return array normalized options |
||
765 | * @since 2.0.12 |
||
766 | */ |
||
767 | 82 | protected static function normalizeOptions(array $options) |
|
789 | } |
||
790 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.