Skip to content

Commit

Permalink
catch errors and undefined interfaces while traversing composer class…
Browse files Browse the repository at this point in the history
… map
  • Loading branch information
alekitto committed Mar 30, 2021
1 parent 1394507 commit e8b9c81
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 80 deletions.
161 changes: 88 additions & 73 deletions composer.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions data/Psr4WithClassMap/NonExistentImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace Kcs\ClassFinder\Fixtures\Psr4WithClassMap;

class NonExistentImplementation implements NonExistentInterface
{
}
4 changes: 2 additions & 2 deletions lib/Finder/FinderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function filter(?callable $callback): self;
*
* @return $this
*/
public function path($pattern): self;
public function path(string $pattern): self;

/**
* Adds rules that filenames must not match.
Expand All @@ -120,5 +120,5 @@ public function path($pattern): self;
*
* @return $this
*/
public function notPath($pattern): self;
public function notPath(string $pattern): self;
}
6 changes: 4 additions & 2 deletions lib/Iterator/ComposerIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ private function searchInClassMap(): Generator
foreach ($this->classLoader->getClassMap() as $class => $file) {
ErrorHandler::register();
try {
yield $class => new ReflectionClass($class);
$reflectionClass = new ReflectionClass($class);
} catch (Throwable $e) { /** @phpstan-ignore-line */
// @ignoreException
continue;
} finally {
ErrorHandler::unregister();
}

yield $class => $reflectionClass;
}
}

Expand Down
13 changes: 12 additions & 1 deletion lib/Iterator/FilteredComposerIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Composer\Autoload\ClassLoader;
use Generator;
use Kcs\ClassFinder\PathNormalizer;
use Kcs\ClassFinder\Util\ErrorHandler;
use ReflectionClass;
use Throwable;

use function array_map;
use function array_unique;
Expand Down Expand Up @@ -82,7 +84,16 @@ private function searchInClassMap(): Generator
continue;
}

yield $class => new ReflectionClass($class);
ErrorHandler::register();
try {
$reflectionClass = new ReflectionClass($class);
} catch (Throwable $e) { /** @phpstan-ignore-line */
continue;
} finally {
ErrorHandler::unregister();
}

yield $class => $reflectionClass;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Iterator/Psr0Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(string $namespace, string $path, int $flags = 0, arr
protected function getGenerator(): Generator
{
$pattern = defined('HHVM_VERSION') ? '/\\.(php|hh)$/i' : '/\\.php$/i';
$include = Closure::bind(static function (string $path) {
$include = Closure::bind(static function (string $path): void {
include_once $path;
}, null, null);

Expand Down
2 changes: 1 addition & 1 deletion lib/Iterator/Psr4Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(string $namespace, string $path, int $flags = 0, arr
protected function getGenerator(): Generator
{
$pattern = defined('HHVM_VERSION') ? '/\\.(php|hh)$/i' : '/\\.php$/i';
$include = Closure::bind(static function (string $path) {
$include = Closure::bind(static function (string $path): void {
include_once $path;
}, null, null);

Expand Down

0 comments on commit e8b9c81

Please sign in to comment.