savinmikhail /
Comments-Density
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SavinMikhail\CommentsDensity\AnalyzeComments\File; |
||
| 6 | |||
| 7 | use RecursiveDirectoryIterator; |
||
| 8 | use RecursiveIteratorIterator; |
||
| 9 | use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config; |
||
| 10 | use SplFileInfo; |
||
| 11 | |||
| 12 | final readonly class FileFinder |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 13 | { |
||
| 14 | public function __construct( |
||
| 15 | private Config $config, |
||
| 16 | ) {} |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @return SplFileInfo[] |
||
| 20 | */ |
||
| 21 | public function __invoke(): iterable |
||
| 22 | { |
||
| 23 | foreach ($this->config->directories as $directory) { |
||
| 24 | $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); |
||
| 25 | foreach ($iterator as $file) { |
||
| 26 | if ($this->shouldSkipFile($file)) { |
||
| 27 | continue; |
||
| 28 | } |
||
| 29 | yield $file; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | private function shouldSkipFile(SplFileInfo $file): bool |
||
| 35 | { |
||
| 36 | return |
||
| 37 | $this->isInWhitelist($file->getRealPath()) |
||
| 38 | || $file->getSize() === 0 |
||
| 39 | || !$this->isPhpFile($file) |
||
| 40 | || !$file->isReadable(); |
||
| 41 | } |
||
| 42 | |||
| 43 | private function isPhpFile(SplFileInfo $file): bool |
||
| 44 | { |
||
| 45 | return $file->isFile() && $file->getExtension() === 'php'; |
||
| 46 | } |
||
| 47 | |||
| 48 | private function isInWhitelist(string $filePath): bool |
||
| 49 | { |
||
| 50 | foreach ($this->config->exclude as $whitelistedDir) { |
||
| 51 | if (str_contains($filePath, $whitelistedDir)) { |
||
| 52 | return true; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | return false; |
||
| 57 | } |
||
| 58 | } |
||
| 59 |