alekitto / class-finder

Utility classes to help discover other classes/namespaces
https://alekitto.github.io/class-finder/
MIT License
27 stars 5 forks source link

FilteredComposerIterator doesn't accept "inside" of psr-4 directory #3

Closed mpyw closed 4 years ago

mpyw commented 4 years ago

Problem

Assume that there is composer.json of Laravel project.

{
  "autoload": {
    "psr-4": {
      "App\\": "app/"
    }
  }
}

We need to find Eloquent Model classes inside of app/Models.

$finder = (new ComposerFinder())
    ->subclassOf(Model::class)
    ->in(base_path('app/Models'));

But it can't find any files.

Reason

Look at this class:

class-finder/FilteredComposerIterator.php at 60ba72245f9ae5eb0d670d49e3457150c47d19bc · alekitto/class-finder

If I fix the following line, it works as expected.

    private function validDir($path): bool
    {
        if (null === $this->dirs) {
            return true;
        }

        $path = PathNormalizer::resolvePath($path);
        foreach ($this->dirs as $dir) {
-           if (0 === \strpos($path, $dir)) {
+           if (0 === \strpos($dir, $path)) {
                return true;
            }
        }

        return false;
    }

Note that there are more similar statements in other filters that need to be fixed.

alekitto commented 4 years ago

Should be fixed in https://github.com/alekitto/class-finder/commit/ecbb7153814d81dccf3f29f78835ad8db45f17c9. Could you please test the master version and confirm?

mpyw commented 4 years ago

@alekitto LGTM! Please release it!

alekitto commented 4 years ago

Fix released in 0.1.5.