Complex classes like Finder 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 Finder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Finder implements \IteratorAggregate, \Countable |
||
| 40 | { |
||
| 41 | const IGNORE_VCS_FILES = 1; |
||
| 42 | const IGNORE_DOT_FILES = 2; |
||
| 43 | const IGNORE_VCS_IGNORED_FILES = 4; |
||
| 44 | |||
| 45 | private $mode = 0; |
||
| 46 | private $names = []; |
||
| 47 | private $notNames = []; |
||
| 48 | private $exclude = []; |
||
| 49 | private $filters = []; |
||
| 50 | private $depths = []; |
||
| 51 | private $sizes = []; |
||
| 52 | private $followLinks = false; |
||
| 53 | private $reverseSorting = false; |
||
| 54 | private $sort = false; |
||
| 55 | private $ignore = 0; |
||
| 56 | private $dirs = []; |
||
| 57 | private $dates = []; |
||
| 58 | private $iterators = []; |
||
| 59 | private $contains = []; |
||
| 60 | private $notContains = []; |
||
| 61 | private $paths = []; |
||
| 62 | private $notPaths = []; |
||
| 63 | private $ignoreUnreadableDirs = false; |
||
| 64 | |||
| 65 | private static $vcsPatterns = ['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg']; |
||
| 66 | |||
| 67 | public function __construct() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Creates a new Finder. |
||
| 74 | * |
||
| 75 | * @return static |
||
| 76 | */ |
||
| 77 | public static function create() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Restricts the matching to directories only. |
||
| 84 | * |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | public function directories() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Restricts the matching to files only. |
||
| 96 | * |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | public function files() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Adds tests for the directory depth. |
||
| 108 | * |
||
| 109 | * Usage: |
||
| 110 | * |
||
| 111 | * $finder->depth('> 1') // the Finder will start matching at level 1. |
||
| 112 | * $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point. |
||
| 113 | * $finder->depth(['>= 1', '< 3']) |
||
| 114 | * |
||
| 115 | * @param string|int|string[]|int[] $levels The depth level expression or an array of depth levels |
||
| 116 | * |
||
| 117 | * @return $this |
||
| 118 | * |
||
| 119 | * @see DepthRangeFilterIterator |
||
| 120 | * @see NumberComparator |
||
| 121 | */ |
||
| 122 | public function depth($levels) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Adds tests for file dates (last modified). |
||
| 133 | * |
||
| 134 | * The date must be something that strtotime() is able to parse: |
||
| 135 | * |
||
| 136 | * $finder->date('since yesterday'); |
||
| 137 | * $finder->date('until 2 days ago'); |
||
| 138 | * $finder->date('> now - 2 hours'); |
||
| 139 | * $finder->date('>= 2005-10-15'); |
||
| 140 | * $finder->date(['>= 2005-10-15', '<= 2006-05-27']); |
||
| 141 | * |
||
| 142 | * @param string|string[] $dates A date range string or an array of date ranges |
||
| 143 | * |
||
| 144 | * @return $this |
||
| 145 | * |
||
| 146 | * @see strtotime |
||
| 147 | * @see DateRangeFilterIterator |
||
| 148 | * @see DateComparator |
||
| 149 | */ |
||
| 150 | public function date($dates) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Adds rules that files must match. |
||
| 161 | * |
||
| 162 | * You can use patterns (delimited with / sign), globs or simple strings. |
||
| 163 | * |
||
| 164 | * $finder->name('*.php') |
||
| 165 | * $finder->name('/\.php$/') // same as above |
||
| 166 | * $finder->name('test.php') |
||
| 167 | * $finder->name(['test.py', 'test.php']) |
||
| 168 | * |
||
| 169 | * @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | * |
||
| 173 | * @see FilenameFilterIterator |
||
| 174 | */ |
||
| 175 | public function name($patterns) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Adds rules that files must not match. |
||
| 184 | * |
||
| 185 | * @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | * |
||
| 189 | * @see FilenameFilterIterator |
||
| 190 | */ |
||
| 191 | public function notName($patterns) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Adds tests that file contents must match. |
||
| 200 | * |
||
| 201 | * Strings or PCRE patterns can be used: |
||
| 202 | * |
||
| 203 | * $finder->contains('Lorem ipsum') |
||
| 204 | * $finder->contains('/Lorem ipsum/i') |
||
| 205 | * $finder->contains(['dolor', '/ipsum/i']) |
||
| 206 | * |
||
| 207 | * @param string|string[] $patterns A pattern (string or regexp) or an array of patterns |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | * |
||
| 211 | * @see FilecontentFilterIterator |
||
| 212 | */ |
||
| 213 | public function contains($patterns) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Adds tests that file contents must not match. |
||
| 222 | * |
||
| 223 | * Strings or PCRE patterns can be used: |
||
| 224 | * |
||
| 225 | * $finder->notContains('Lorem ipsum') |
||
| 226 | * $finder->notContains('/Lorem ipsum/i') |
||
| 227 | * $finder->notContains(['lorem', '/dolor/i']) |
||
| 228 | * |
||
| 229 | * @param string|string[] $patterns A pattern (string or regexp) or an array of patterns |
||
| 230 | * |
||
| 231 | * @return $this |
||
| 232 | * |
||
| 233 | * @see FilecontentFilterIterator |
||
| 234 | */ |
||
| 235 | public function notContains($patterns) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Adds rules that filenames must match. |
||
| 244 | * |
||
| 245 | * You can use patterns (delimited with / sign) or simple strings. |
||
| 246 | * |
||
| 247 | * $finder->path('some/special/dir') |
||
| 248 | * $finder->path('/some\/special\/dir/') // same as above |
||
| 249 | * $finder->path(['some dir', 'another/dir']) |
||
| 250 | * |
||
| 251 | * Use only / as dirname separator. |
||
| 252 | * |
||
| 253 | * @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns |
||
| 254 | * |
||
| 255 | * @return $this |
||
| 256 | * |
||
| 257 | * @see FilenameFilterIterator |
||
| 258 | */ |
||
| 259 | public function path($patterns) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Adds rules that filenames must not match. |
||
| 268 | * |
||
| 269 | * You can use patterns (delimited with / sign) or simple strings. |
||
| 270 | * |
||
| 271 | * $finder->notPath('some/special/dir') |
||
| 272 | * $finder->notPath('/some\/special\/dir/') // same as above |
||
| 273 | * $finder->notPath(['some/file.txt', 'another/file.log']) |
||
| 274 | * |
||
| 275 | * Use only / as dirname separator. |
||
| 276 | * |
||
| 277 | * @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns |
||
| 278 | * |
||
| 279 | * @return $this |
||
| 280 | * |
||
| 281 | * @see FilenameFilterIterator |
||
| 282 | */ |
||
| 283 | public function notPath($patterns) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Adds tests for file sizes. |
||
| 292 | * |
||
| 293 | * $finder->size('> 10K'); |
||
| 294 | * $finder->size('<= 1Ki'); |
||
| 295 | * $finder->size(4); |
||
| 296 | * $finder->size(['> 10K', '< 20K']) |
||
| 297 | * |
||
| 298 | * @param string|int|string[]|int[] $sizes A size range string or an integer or an array of size ranges |
||
| 299 | * |
||
| 300 | * @return $this |
||
| 301 | * |
||
| 302 | * @see SizeRangeFilterIterator |
||
| 303 | * @see NumberComparator |
||
| 304 | */ |
||
| 305 | public function size($sizes) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Excludes directories. |
||
| 316 | * |
||
| 317 | * Directories passed as argument must be relative to the ones defined with the `in()` method. For example: |
||
| 318 | * |
||
| 319 | * $finder->in(__DIR__)->exclude('ruby'); |
||
| 320 | * |
||
| 321 | * @param string|array $dirs A directory path or an array of directories |
||
| 322 | * |
||
| 323 | * @return $this |
||
| 324 | * |
||
| 325 | * @see ExcludeDirectoryFilterIterator |
||
| 326 | */ |
||
| 327 | public function exclude($dirs) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Excludes "hidden" directories and files (starting with a dot). |
||
| 336 | * |
||
| 337 | * This option is enabled by default. |
||
| 338 | * |
||
| 339 | * @return $this |
||
| 340 | * |
||
| 341 | * @see ExcludeDirectoryFilterIterator |
||
| 342 | */ |
||
| 343 | public function ignoreDotFiles(bool $ignoreDotFiles) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Forces the finder to ignore version control directories. |
||
| 356 | * |
||
| 357 | * This option is enabled by default. |
||
| 358 | * |
||
| 359 | * @return $this |
||
| 360 | * |
||
| 361 | * @see ExcludeDirectoryFilterIterator |
||
| 362 | */ |
||
| 363 | public function ignoreVCS(bool $ignoreVCS) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Forces Finder to obey .gitignore and ignore files based on rules listed there. |
||
| 376 | * |
||
| 377 | * This option is disabled by default. |
||
| 378 | * |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | public function ignoreVCSIgnored(bool $ignoreVCSIgnored) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Adds VCS patterns. |
||
| 394 | * |
||
| 395 | * @see ignoreVCS() |
||
| 396 | * |
||
| 397 | * @param string|string[] $pattern VCS patterns to ignore |
||
| 398 | */ |
||
| 399 | public static function addVCSPattern($pattern) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Sorts files and directories by an anonymous function. |
||
| 410 | * |
||
| 411 | * The anonymous function receives two \SplFileInfo instances to compare. |
||
| 412 | * |
||
| 413 | * This can be slow as all the matching files and directories must be retrieved for comparison. |
||
| 414 | * |
||
| 415 | * @return $this |
||
| 416 | * |
||
| 417 | * @see SortableIterator |
||
| 418 | */ |
||
| 419 | public function sort(\Closure $closure) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Sorts files and directories by name. |
||
| 428 | * |
||
| 429 | * This can be slow as all the matching files and directories must be retrieved for comparison. |
||
| 430 | * |
||
| 431 | * @return $this |
||
| 432 | * |
||
| 433 | * @see SortableIterator |
||
| 434 | */ |
||
| 435 | public function sortByName(bool $useNaturalSort = false) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Sorts files and directories by type (directories before files), then by name. |
||
| 444 | * |
||
| 445 | * This can be slow as all the matching files and directories must be retrieved for comparison. |
||
| 446 | * |
||
| 447 | * @return $this |
||
| 448 | * |
||
| 449 | * @see SortableIterator |
||
| 450 | */ |
||
| 451 | public function sortByType() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Sorts files and directories by the last accessed time. |
||
| 460 | * |
||
| 461 | * This is the time that the file was last accessed, read or written to. |
||
| 462 | * |
||
| 463 | * This can be slow as all the matching files and directories must be retrieved for comparison. |
||
| 464 | * |
||
| 465 | * @return $this |
||
| 466 | * |
||
| 467 | * @see SortableIterator |
||
| 468 | */ |
||
| 469 | public function sortByAccessedTime() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Reverses the sorting. |
||
| 478 | * |
||
| 479 | * @return $this |
||
| 480 | */ |
||
| 481 | public function reverseSorting() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Sorts files and directories by the last inode changed time. |
||
| 490 | * |
||
| 491 | * This is the time that the inode information was last modified (permissions, owner, group or other metadata). |
||
| 492 | * |
||
| 493 | * On Windows, since inode is not available, changed time is actually the file creation time. |
||
| 494 | * |
||
| 495 | * This can be slow as all the matching files and directories must be retrieved for comparison. |
||
| 496 | * |
||
| 497 | * @return $this |
||
| 498 | * |
||
| 499 | * @see SortableIterator |
||
| 500 | */ |
||
| 501 | public function sortByChangedTime() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Sorts files and directories by the last modified time. |
||
| 510 | * |
||
| 511 | * This is the last time the actual contents of the file were last modified. |
||
| 512 | * |
||
| 513 | * This can be slow as all the matching files and directories must be retrieved for comparison. |
||
| 514 | * |
||
| 515 | * @return $this |
||
| 516 | * |
||
| 517 | * @see SortableIterator |
||
| 518 | */ |
||
| 519 | public function sortByModifiedTime() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Filters the iterator with an anonymous function. |
||
| 528 | * |
||
| 529 | * The anonymous function receives a \SplFileInfo and must return false |
||
| 530 | * to remove files. |
||
| 531 | * |
||
| 532 | * @return $this |
||
| 533 | * |
||
| 534 | * @see CustomFilterIterator |
||
| 535 | */ |
||
| 536 | public function filter(\Closure $closure) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Forces the following of symlinks. |
||
| 545 | * |
||
| 546 | * @return $this |
||
| 547 | */ |
||
| 548 | public function followLinks() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Tells finder to ignore unreadable directories. |
||
| 557 | * |
||
| 558 | * By default, scanning unreadable directories content throws an AccessDeniedException. |
||
| 559 | * |
||
| 560 | * @return $this |
||
| 561 | */ |
||
| 562 | public function ignoreUnreadableDirs(bool $ignore = true) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Searches files and directories which match defined rules. |
||
| 571 | * |
||
| 572 | * @param string|string[] $dirs A directory path or an array of directories |
||
| 573 | * |
||
| 574 | * @return $this |
||
| 575 | * |
||
| 576 | * @throws DirectoryNotFoundException if one of the directories does not exist |
||
| 577 | */ |
||
| 578 | public function in($dirs) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Returns an Iterator for the current Finder configuration. |
||
| 600 | * |
||
| 601 | * This method implements the IteratorAggregate interface. |
||
| 602 | * |
||
| 603 | * @return \Iterator|SplFileInfo[] An iterator |
||
| 604 | * |
||
| 605 | * @throws \LogicException if the in() method has not been called |
||
| 606 | */ |
||
| 607 | public function getIterator() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Appends an existing set of files/directories to the finder. |
||
| 631 | * |
||
| 632 | * The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array. |
||
| 633 | * |
||
| 634 | * @return $this |
||
| 635 | * |
||
| 636 | * @throws \InvalidArgumentException when the given argument is not iterable |
||
| 637 | */ |
||
| 638 | public function append(iterable $iterator) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Check if the any results were found. |
||
| 659 | * |
||
| 660 | * @return bool |
||
| 661 | */ |
||
| 662 | public function hasResults() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Counts all the results collected by the iterators. |
||
| 673 | * |
||
| 674 | * @return int |
||
| 675 | */ |
||
| 676 | public function count() |
||
| 680 | |||
| 681 | private function searchInDirectory(string $dir): \Iterator |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Normalizes given directory names by removing trailing slashes. |
||
| 780 | * |
||
| 781 | * Excluding: (s)ftp:// or ssh2.(s)ftp:// wrapper |
||
| 782 | */ |
||
| 783 | private function normalizeDir(string $dir): string |
||
| 797 | } |
||
| 798 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..