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 | /** |
||
39 | * Normalizes a file/directory path. |
||
40 | * The normalization does the following work: |
||
41 | * |
||
42 | * - Convert all directory separators into `DIRECTORY_SEPARATOR` (e.g. "\a/b\c" becomes "/a/b/c") |
||
43 | * - Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c") |
||
44 | * - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c") |
||
45 | * - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c") |
||
46 | * |
||
47 | * @param string $path the file/directory path to be normalized |
||
48 | * @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`. |
||
49 | * @return string the normalized file/directory path |
||
50 | */ |
||
51 | 14 | public static function normalizePath($path, $ds = DIRECTORY_SEPARATOR) |
|
52 | { |
||
53 | 14 | $path = rtrim(strtr($path, '/\\', $ds . $ds), $ds); |
|
54 | 14 | if (strpos($ds . $path, "{$ds}.") === false && strpos($path, "{$ds}{$ds}") === false) { |
|
55 | 13 | return $path; |
|
56 | } |
||
57 | // the path may contain ".", ".." or double slashes, need to clean them up |
||
58 | 1 | $parts = []; |
|
59 | 1 | foreach (explode($ds, $path) as $part) { |
|
60 | 1 | if ($part === '..' && !empty($parts) && end($parts) !== '..') { |
|
61 | 1 | array_pop($parts); |
|
62 | 1 | } elseif ($part === '.' || $part === '' && !empty($parts)) { |
|
63 | 1 | continue; |
|
64 | } else { |
||
65 | 1 | $parts[] = $part; |
|
66 | } |
||
67 | 1 | } |
|
68 | 1 | $path = implode($ds, $parts); |
|
69 | 1 | return $path === '' ? '.' : $path; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns the localized version of a specified file. |
||
74 | * |
||
75 | * The searching is based on the specified language code. In particular, |
||
76 | * a file with the same name will be looked for under the subdirectory |
||
77 | * whose name is the same as the language code. For example, given the file "path/to/view.php" |
||
78 | * and language code "zh-CN", the localized file will be looked for as |
||
79 | * "path/to/zh-CN/view.php". If the file is not found, it will try a fallback with just a language code that is |
||
80 | * "zh" i.e. "path/to/zh/view.php". If it is not found as well the original file will be returned. |
||
81 | * |
||
82 | * If the target and the source language codes are the same, |
||
83 | * the original file will be returned. |
||
84 | * |
||
85 | * @param string $file the original file |
||
86 | * @param string $language the target language that the file should be localized to. |
||
87 | * If not set, the value of [[\yii\base\Application::language]] will be used. |
||
88 | * @param string $sourceLanguage the language that the original file is in. |
||
89 | * If not set, the value of [[\yii\base\Application::sourceLanguage]] will be used. |
||
90 | * @return string the matching localized file, or the original file if the localized version is not found. |
||
91 | * If the target and the source language codes are the same, the original file will be returned. |
||
92 | */ |
||
93 | 44 | public static function localize($file, $language = null, $sourceLanguage = null) |
|
117 | |||
118 | /** |
||
119 | * Determines the MIME type of the specified file. |
||
120 | * This method will first try to determine the MIME type based on |
||
121 | * [finfo_open](http://php.net/manual/en/function.finfo-open.php). If the `fileinfo` extension is not installed, |
||
122 | * it will fall back to [[getMimeTypeByExtension()]] when `$checkExtension` is true. |
||
123 | * @param string $file the file name. |
||
124 | * @param string $magicFile name of the optional magic database file (or alias), usually something like `/path/to/magic.mime`. |
||
125 | * This will be passed as the second parameter to [finfo_open()](http://php.net/manual/en/function.finfo-open.php) |
||
126 | * when the `fileinfo` extension is installed. If the MIME type is being determined based via [[getMimeTypeByExtension()]] |
||
127 | * and this is null, it will use the file specified by [[mimeMagicFile]]. |
||
128 | * @param boolean $checkExtension whether to use the file extension to determine the MIME type in case |
||
129 | * `finfo_open()` cannot determine it. |
||
130 | * @return string the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined. |
||
131 | * @throws InvalidConfigException when the `fileinfo` PHP extension is not installed and `$checkExtension` is `false`. |
||
132 | */ |
||
133 | 12 | public static function getMimeType($file, $magicFile = null, $checkExtension = true) |
|
158 | |||
159 | /** |
||
160 | * Determines the MIME type based on the extension name of the specified file. |
||
161 | * This method will use a local map between extension names and MIME types. |
||
162 | * @param string $file the file name. |
||
163 | * @param string $magicFile the path (or alias) of the file that contains all available MIME type information. |
||
164 | * If this is not set, the file specified by [[mimeMagicFile]] will be used. |
||
165 | * @return string the MIME type. Null is returned if the MIME type cannot be determined. |
||
166 | */ |
||
167 | 8 | public static function getMimeTypeByExtension($file, $magicFile = null) |
|
180 | |||
181 | /** |
||
182 | * Determines the extensions by given MIME type. |
||
183 | * This method will use a local map between extension names and MIME types. |
||
184 | * @param string $mimeType file MIME type. |
||
185 | * @param string $magicFile the path (or alias) of the file that contains all available MIME type information. |
||
186 | * If this is not set, the file specified by [[mimeMagicFile]] will be used. |
||
187 | * @return array the extensions corresponding to the specified MIME type |
||
188 | */ |
||
189 | public static function getExtensionsByMimeType($mimeType, $magicFile = null) |
||
194 | |||
195 | private static $_mimeTypes = []; |
||
196 | |||
197 | /** |
||
198 | * Loads MIME types from the specified file. |
||
199 | * @param string $magicFile the path (or alias) of the file that contains all available MIME type information. |
||
200 | * If this is not set, the file specified by [[mimeMagicFile]] will be used. |
||
201 | * @return array the mapping from file extensions to MIME types |
||
202 | */ |
||
203 | 8 | protected static function loadMimeTypes($magicFile) |
|
214 | |||
215 | /** |
||
216 | * Copies a whole directory as another one. |
||
217 | * The files and sub-directories will also be copied over. |
||
218 | * @param string $src the source directory |
||
219 | * @param string $dst the destination directory |
||
220 | * @param array $options options for directory copy. Valid options are: |
||
221 | * |
||
222 | * - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775. |
||
223 | * - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting. |
||
224 | * - filter: callback, a PHP callback that is called for each directory or file. |
||
225 | * The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered. |
||
226 | * The callback can return one of the following values: |
||
227 | * |
||
228 | * * true: the directory or file will be copied (the "only" and "except" options will be ignored) |
||
229 | * * false: the directory or file will NOT be copied (the "only" and "except" options will be ignored) |
||
230 | * * null: the "only" and "except" options will determine whether the directory or file should be copied |
||
231 | * |
||
232 | * - only: array, list of patterns that the file paths should match if they want to be copied. |
||
233 | * A path matches a pattern if it contains the pattern string at its end. |
||
234 | * For example, '.php' matches all file paths ending with '.php'. |
||
235 | * Note, the '/' characters in a pattern matches both '/' and '\' in the paths. |
||
236 | * If a file path matches a pattern in both "only" and "except", it will NOT be copied. |
||
237 | * - except: array, list of patterns that the files or directories should match if they want to be excluded from being copied. |
||
238 | * A path matches a pattern if it contains the pattern string at its end. |
||
239 | * Patterns ending with '/' apply to directory paths only, and patterns not ending with '/' |
||
240 | * apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b'; |
||
241 | * and '.svn/' matches directory paths ending with '.svn'. Note, the '/' characters in a pattern matches |
||
242 | * both '/' and '\' in the paths. |
||
243 | * - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true. |
||
244 | * - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true. |
||
245 | * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. |
||
246 | * If the callback returns false, the copy operation for the sub-directory or file will be cancelled. |
||
247 | * The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or |
||
248 | * file to be copied from, while `$to` is the copy target. |
||
249 | * - afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied. |
||
250 | * The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or |
||
251 | * file copied from, while `$to` is the copy target. |
||
252 | * @throws \yii\base\InvalidParamException if unable to open directory |
||
253 | */ |
||
254 | 13 | public static function copyDirectory($src, $dst, $options = []) |
|
255 | { |
||
256 | 13 | $src = static::normalizePath($src); |
|
257 | 13 | $dst = static::normalizePath($dst); |
|
258 | |||
259 | 13 | if ($src === $dst || strpos($dst, $src . DIRECTORY_SEPARATOR) === 0) { |
|
260 | 2 | throw new InvalidParamException('Trying to copy a directory to itself or a subdirectory.'); |
|
261 | } |
||
262 | 11 | if (!is_dir($dst)) { |
|
263 | 9 | static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true); |
|
264 | 9 | } |
|
265 | |||
266 | 11 | $handle = opendir($src); |
|
267 | 11 | if ($handle === false) { |
|
268 | throw new InvalidParamException("Unable to open directory: $src"); |
||
269 | } |
||
270 | 11 | if (!isset($options['basePath'])) { |
|
271 | // this should be done only once |
||
272 | 11 | $options['basePath'] = realpath($src); |
|
273 | 11 | $options = self::normalizeOptions($options); |
|
274 | 11 | } |
|
275 | 11 | while (($file = readdir($handle)) !== false) { |
|
276 | 11 | if ($file === '.' || $file === '..') { |
|
277 | 11 | continue; |
|
278 | } |
||
279 | 9 | $from = $src . DIRECTORY_SEPARATOR . $file; |
|
280 | 9 | $to = $dst . DIRECTORY_SEPARATOR . $file; |
|
281 | 9 | if (static::filterPath($from, $options)) { |
|
282 | 9 | if (isset($options['beforeCopy']) && !call_user_func($options['beforeCopy'], $from, $to)) { |
|
283 | 2 | continue; |
|
284 | } |
||
285 | 7 | if (is_file($from)) { |
|
286 | 7 | copy($from, $to); |
|
287 | 7 | if (isset($options['fileMode'])) { |
|
288 | 1 | @chmod($to, $options['fileMode']); |
|
289 | 1 | } |
|
290 | 7 | } else { |
|
291 | // recursive copy, defaults to true |
||
292 | 5 | if (!isset($options['recursive']) || $options['recursive']) { |
|
293 | 4 | static::copyDirectory($from, $to, $options); |
|
294 | 4 | } |
|
295 | } |
||
296 | 7 | if (isset($options['afterCopy'])) { |
|
297 | call_user_func($options['afterCopy'], $from, $to); |
||
298 | } |
||
299 | 7 | } |
|
300 | 7 | } |
|
301 | 11 | closedir($handle); |
|
302 | 11 | } |
|
303 | |||
304 | /** |
||
305 | * Removes a directory (and all its content) recursively. |
||
306 | * |
||
307 | * @param string $dir the directory to be deleted recursively. |
||
308 | * @param array $options options for directory remove. Valid options are: |
||
309 | * |
||
310 | * - traverseSymlinks: boolean, whether symlinks to the directories should be traversed too. |
||
311 | * Defaults to `false`, meaning the content of the symlinked directory would not be deleted. |
||
312 | * Only symlink would be removed in that default case. |
||
313 | * |
||
314 | * @throws ErrorException in case of failure |
||
315 | */ |
||
316 | 86 | public static function removeDirectory($dir, $options = []) |
|
354 | |||
355 | /** |
||
356 | * Returns the files found under the specified directory and subdirectories. |
||
357 | * @param string $dir the directory under which the files will be looked for. |
||
358 | * @param array $options options for file searching. Valid options are: |
||
359 | * |
||
360 | * - `filter`: callback, a PHP callback that is called for each directory or file. |
||
361 | * The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered. |
||
362 | * The callback can return one of the following values: |
||
363 | * |
||
364 | * * `true`: the directory or file will be returned (the `only` and `except` options will be ignored) |
||
365 | * * `false`: the directory or file will NOT be returned (the `only` and `except` options will be ignored) |
||
366 | * * `null`: the `only` and `except` options will determine whether the directory or file should be returned |
||
367 | * |
||
368 | * - `except`: array, list of patterns excluding from the results matching file or directory paths. |
||
369 | * Patterns ending with slash ('/') apply to directory paths only, and patterns not ending with '/' |
||
370 | * apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b'; |
||
371 | * and `.svn/` matches directory paths ending with `.svn`. |
||
372 | * If the pattern does not contain a slash (`/`), it is treated as a shell glob pattern |
||
373 | * and checked for a match against the pathname relative to `$dir`. |
||
374 | * Otherwise, the pattern is treated as a shell glob suitable for consumption by `fnmatch(3)` |
||
375 | * `with the `FNM_PATHNAME` flag: wildcards in the pattern will not match a `/` in the pathname. |
||
376 | * For example, `views/*.php` matches `views/index.php` but not `views/controller/index.php`. |
||
377 | * A leading slash matches the beginning of the pathname. For example, `/*.php` matches `index.php` but not `views/start/index.php`. |
||
378 | * An optional prefix `!` which negates the pattern; any matching file excluded by a previous pattern will become included again. |
||
379 | * If a negated pattern matches, this will override lower precedence patterns sources. Put a backslash (`\`) in front of the first `!` |
||
380 | * for patterns that begin with a literal `!`, for example, `\!important!.txt`. |
||
381 | * Note, the '/' characters in a pattern matches both '/' and '\' in the paths. |
||
382 | * - `only`: array, list of patterns that the file paths should match if they are to be returned. Directory paths |
||
383 | * are not checked against them. Same pattern matching rules as in the `except` option are used. |
||
384 | * If a file path matches a pattern in both `only` and `except`, it will NOT be returned. |
||
385 | * - `caseSensitive`: boolean, whether patterns specified at `only` or `except` should be case sensitive. Defaults to `true`. |
||
386 | * - `recursive`: boolean, whether the files under the subdirectories should also be looked for. Defaults to `true`. |
||
387 | * @return array files found under the directory, in no particular order. Ordering depends on the files system used. |
||
388 | * @throws InvalidParamException if the dir is invalid. |
||
389 | */ |
||
390 | 44 | public static function findFiles($dir, $options = []) |
|
423 | |||
424 | /** |
||
425 | * Checks if the given file path satisfies the filtering options. |
||
426 | * @param string $path the path of the file or directory to be checked |
||
427 | * @param array $options the filtering options. See [[findFiles()]] for explanations of |
||
428 | * the supported options. |
||
429 | * @return boolean whether the file or directory satisfies the filtering options. |
||
430 | */ |
||
431 | 51 | public static function filterPath($path, $options) |
|
463 | |||
464 | /** |
||
465 | * Creates a new directory. |
||
466 | * |
||
467 | * This method is similar to the PHP `mkdir()` function except that |
||
468 | * it uses `chmod()` to set the permission of the created directory |
||
469 | * in order to avoid the impact of the `umask` setting. |
||
470 | * |
||
471 | * @param string $path path of the directory to be created. |
||
472 | * @param integer $mode the permission to be set for the created directory. |
||
473 | * @param boolean $recursive whether to create parent directories if they do not exist. |
||
474 | * @return boolean whether the directory is created successfully |
||
475 | * @throws \yii\base\Exception if the directory could not be created (i.e. php error due to parallel changes) |
||
476 | */ |
||
477 | 120 | public static function createDirectory($path, $mode = 0775, $recursive = true) |
|
502 | |||
503 | /** |
||
504 | * Performs a simple comparison of file or directory names. |
||
505 | * |
||
506 | * Based on match_basename() from dir.c of git 1.8.5.3 sources. |
||
507 | * |
||
508 | * @param string $baseName file or directory name to compare with the pattern |
||
509 | * @param string $pattern the pattern that $baseName will be compared against |
||
510 | * @param integer|boolean $firstWildcard location of first wildcard character in the $pattern |
||
511 | * @param integer $flags pattern flags |
||
512 | * @return boolean whether the name matches against pattern |
||
513 | */ |
||
514 | 34 | private static function matchBasename($baseName, $pattern, $firstWildcard, $flags) |
|
535 | |||
536 | /** |
||
537 | * Compares a path part against a pattern with optional wildcards. |
||
538 | * |
||
539 | * Based on match_pathname() from dir.c of git 1.8.5.3 sources. |
||
540 | * |
||
541 | * @param string $path full path to compare |
||
542 | * @param string $basePath base of path that will not be compared |
||
543 | * @param string $pattern the pattern that path part will be compared against |
||
544 | * @param integer|boolean $firstWildcard location of first wildcard character in the $pattern |
||
545 | * @param integer $flags pattern flags |
||
546 | * @return boolean whether the path part matches against pattern |
||
547 | */ |
||
548 | 19 | private static function matchPathname($path, $basePath, $pattern, $firstWildcard, $flags) |
|
589 | |||
590 | /** |
||
591 | * Scan the given exclude list in reverse to see whether pathname |
||
592 | * should be ignored. The first match (i.e. the last on the list), if |
||
593 | * any, determines the fate. Returns the element which |
||
594 | * matched, or null for undecided. |
||
595 | * |
||
596 | * Based on last_exclude_matching_from_list() from dir.c of git 1.8.5.3 sources. |
||
597 | * |
||
598 | * @param string $basePath |
||
599 | * @param string $path |
||
600 | * @param array $excludes list of patterns to match $path against |
||
601 | * @return string null or one of $excludes item as an array with keys: 'pattern', 'flags' |
||
602 | * @throws InvalidParamException if any of the exclude patterns is not a string or an array with keys: pattern, flags, firstWildcard. |
||
603 | */ |
||
604 | 34 | private static function lastExcludeMatchingFromList($basePath, $path, $excludes) |
|
631 | |||
632 | /** |
||
633 | * Processes the pattern, stripping special characters like / and ! from the beginning and settings flags instead. |
||
634 | * @param string $pattern |
||
635 | * @param boolean $caseSensitive |
||
636 | * @throws \yii\base\InvalidParamException |
||
637 | * @return array with keys: (string) pattern, (int) flags, (int|boolean) firstWildcard |
||
638 | */ |
||
639 | 34 | private static function parseExcludePattern($pattern, $caseSensitive) |
|
678 | |||
679 | /** |
||
680 | * Searches for the first wildcard character in the pattern. |
||
681 | * @param string $pattern the pattern to search in |
||
682 | * @return integer|boolean position of first wildcard character or false if not found |
||
683 | */ |
||
684 | 34 | private static function firstWildcardInPattern($pattern) |
|
695 | |||
696 | /** |
||
697 | * @param array $options raw options |
||
698 | * @return array normalized options |
||
699 | */ |
||
700 | 53 | private static function normalizeOptions(array $options) |
|
721 | } |
||
722 |
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.