Repository: maglnet/ComposerRequireChecker (PHP)

Issue:
LocateASTFromFiles trying to read directories as files on PHP 7.4
Ón PHP 7.4 this new error appears:

```
Notice:  file_get_contents(): read of 8192 bytes failed with errno=21 Is a directory in phar:///usr/local/bin/composer-require-checker/src/ComposerRequireChecker/ASTLocator/LocateASTFromFiles.php on line 35
Stack trace:
  1. {main}() /usr/local/bin/composer-require-checker:0
  2. include() /usr/local/bin/composer-require-checker:10
  3. ComposerRequireChecker\Cli\Application->run() phar:///usr/local/bin/composer-require-checker/bin/composer-require-checker.php:32
  4. ComposerRequireChecker\Cli\Application->doRun() phar:///usr/local/bin/composer-require-checker/vendor/symfony/console/Application.php:145
  5. ComposerRequireChecker\Cli\Application->doRunCommand() phar:///usr/local/bin/composer-require-checker/vendor/symfony/console/Application.php:262
  6. ComposerRequireChecker\Cli\CheckCommand->run() phar:///usr/local/bin/composer-require-checker/vendor/symfony/console/Application.php:901
  7. ComposerRequireChecker\Cli\CheckCommand->execute() phar:///usr/local/bin/composer-require-checker/vendor/symfony/console/Command/Command.php:255
  8. ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromASTRoots->__invoke() phar:///usr/local/bin/composer-require-checker/src/ComposerRequireChecker/Cli/CheckCommand.php:79
  9. ComposerRequireChecker\ASTLocator\LocateASTFromFiles->__invoke() phar:///usr/local/bin/composer-require-checker/src/ComposerRequireChecker/DefinedSymbolsLocator/LocateDefinedSymbolsFromASTRoots.php:27
 10. file_get_contents() phar:///usr/local/bin/composer-require-checker/src/ComposerRequireChecker/ASTLocator/LocateASTFromFiles.php:35
```


Discussion:
This should be fixed with release 2.1.0

@Majkl578 could you please verify?
Not fixed in 2.1.0 unfortunately, still seeing `PHP Notice:  file_get_contents(): read of 8192 bytes failed with errno=21 Is a directory in phar:///usr/local/bin/composer-require-checker/src/ComposerRequireChecker/ASTLocator/LocateASTFromFiles.php` errors. :(
I just ran into the same problem. Looks like foreach now also lists "." and ".." as files in a directory, so when looping over them the loop variable $file also gets the values "." and "..".
@Photon89 do you have a code location to reference, or maybe a test case?
@Ocramius Yep, this loop: https://github.com/maglnet/ComposerRequireChecker/blob/6022d233935615c4454a462c0607aa5b834fd93c/src/ComposerRequireChecker/ASTLocator/LocateASTFromFiles.php#L34
For reference, I am still seeing the same issue as well on our CI.
A quick and dirty fix which I used in my app, just exclude "." and ".." by hand while looping: https://gitlab.com/Photon89/tpiq-experimental/-/commit/d6af93f5bf02c1b1fadbbffc842a92ce424c506d
Would probably check which iterator is passed in instead, instead of fixing the symptom
I'm a rookie in programming, would be grateful for some documentation reference to a better fix. :) 



Patch:
diff --git a/src/ComposerRequireChecker/ASTLocator/LocateASTFromFiles.php b/src/ComposerRequireChecker/ASTLocator/LocateASTFromFiles.php
index 825c009e..4b126a8c 100644
--- a/src/ComposerRequireChecker/ASTLocator/LocateASTFromFiles.php
+++ b/src/ComposerRequireChecker/ASTLocator/LocateASTFromFiles.php
@@ -11,6 +11,7 @@
 use Traversable;
 
 use function file_get_contents;
+use function is_file;
 use function sprintf;
 
 final class LocateASTFromFiles
@@ -32,6 +33,10 @@ public function __construct(Parser $parser, ?ErrorHandler $errorHandler)
     public function __invoke(Traversable $files): Traversable
     {
         foreach ($files as $file) {
+            if (! is_file($file)) {
+                continue;
+            }
+
             $stmts = $this->parser->parse(file_get_contents($file), $this->errorHandler);
 
             if ($stmts === null) {


Test patch:
diff --git a/test/ComposerRequireCheckerTest/ASTLocator/LocateASTFromFilesTest.php b/test/ComposerRequireCheckerTest/ASTLocator/LocateASTFromFilesTest.php
index 257f6d91..e377e04e 100644
--- a/test/ComposerRequireCheckerTest/ASTLocator/LocateASTFromFilesTest.php
+++ b/test/ComposerRequireCheckerTest/ASTLocator/LocateASTFromFilesTest.php
@@ -39,6 +39,8 @@ public function testLocate(): void
         $files = [
             $this->createFile('MyClassA', '<?php class MyClassA {}'),
             $this->createFile('MyClassB', '<?php class MyClassB {}'),
+            __DIR__ . '/.',
+            __DIR__ . '/..',
         ];
 
         $roots = $this->locate($files);