Skip to content

Commit

Permalink
fix: fixed style, run static analysis on push
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Nov 27, 2024
1 parent 1186c30 commit 5e125cf
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cscheck.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Check CS

on:
push:
pull_request_target:

jobs:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: PHPStan

on:
push:
pull_request_target:

jobs:
Expand Down
6 changes: 4 additions & 2 deletions data/Psr4WithClassMap/Map/ErrorStub.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php declare(strict_types=1);

class Error
{
if (!class_exists(Error::class, false)) {
class Error
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(Iterator $iterator, private readonly array $interfac
parent::__construct($iterator);

$this->getMetadata = method_exists(Class_::class, 'getMetadata') ?
static fn (object $reflector) => $reflector->getMetadata() :
static fn (object $reflector) => $reflector->getMetadata() : /** @phpstan-ignore-line */
static fn (object $reflector) => MetadataRegistry::getInstance()->getMetadata($reflector);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(Iterator $iterator, private readonly string $superCl
parent::__construct($iterator);

$this->getMetadata = method_exists(Class_::class, 'getMetadata') ?
static fn (object $reflector) => $reflector->getMetadata() :
static fn (object $reflector) => $reflector->getMetadata() : /** @phpstan-ignore-line */
static fn (object $reflector) => MetadataRegistry::getInstance()->getMetadata($reflector);
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Finder/FinderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public function notInNamespace(string|array $namespaces): static;
* Sets a custom callback for class filtering.
* The callback will receive the class name as the only argument.
*
* @param callable(object, string):bool|null $callback
*
* @return $this
*/
public function filter(callable|null $callback): static;
Expand Down Expand Up @@ -125,6 +127,8 @@ public function notPath(string $pattern): static;
* Sets a custom callback for file filtering.
* The callback will receive the full filepath as the only argument.
*
* @param callable(string):bool|null $callback
*
* @return $this
*/
public function pathFilter(callable|null $callback): static;
Expand Down
4 changes: 2 additions & 2 deletions lib/Finder/FinderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ trait FinderTrait
/** @var string[] */
private array|null $notPaths = null;

/** @var callable|null */
/** @var callable(object, string):bool|null */
private $filterCallback = null;

/** @var callable|null */
/** @var callable(string):bool|null */
private $pathFilterCallback = null;

private bool $skipNonInstantiable = false;
Expand Down
8 changes: 6 additions & 2 deletions lib/Iterator/PhpDocumentorIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use phpDocumentor\Reflection\File\LocalFile;
use phpDocumentor\Reflection\Fqsen;
use PhpDocumentor\Reflection\Metadata\Metadata as PhpDocMetadata;
use phpDocumentor\Reflection\Metadata\Metadata as PhpDocMetadata;
use phpDocumentor\Reflection\Php\Class_;
use phpDocumentor\Reflection\Php\Enum_;
use phpDocumentor\Reflection\Php\Factory;
Expand All @@ -31,6 +31,7 @@
use function array_values;
use function assert;
use function class_exists;
use function is_string;
use function ltrim;
use function method_exists;
use function Safe\preg_match;
Expand Down Expand Up @@ -128,6 +129,7 @@ protected function getGenerator(): Generator
};
} else {
$addMetadata = static function (object $target, PhpDocMetadata $metadata): void {
assert(method_exists($target, 'addMetadata'));
$target->addMetadata($metadata);
};
}
Expand All @@ -150,7 +152,7 @@ protected function getGenerator(): Generator
$factory->create($contextStack, new LocalFile($path), $this->strategies);
$reflector = $project->getFiles()[$path];
}
} catch (Throwable) {
} catch (Throwable) { /** @phpstan-ignore-line */
continue;
}

Expand All @@ -173,6 +175,8 @@ static function (object $class) use (&$symbols): object {
}

foreach ($files as $path => $fileSymbols) {
assert(is_string($path));

foreach ($fileSymbols as $fileSymbol) {
if ($fileSymbol instanceof Class_) {
$parents = [];
Expand Down
3 changes: 3 additions & 0 deletions lib/Iterator/PhpParserIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function assert;
use function class_exists;
use function file_get_contents;
use function is_string;
use function ltrim;
use function Safe\preg_match;

Expand Down Expand Up @@ -111,6 +112,8 @@ static function (Stmt\ClassLike $class) use (&$symbols): object {
}

foreach ($files as $path => $fileSymbols) {
assert(is_string($path));

foreach ($fileSymbols as $fileSymbol) {
if ($fileSymbol instanceof Stmt\Class_) {
$parents = [];
Expand Down
8 changes: 7 additions & 1 deletion lib/Util/BogonFilesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class BogonFilesFilter
// https://github.com/alekitto/class-finder/issues/13#issuecomment-2010509501
'(?:' .
'(?:symfony/(?:cache|symfony/Component/Cache)/Traits/(?:Redis(?:Cluster)?\dProxy|ValueWrapper)|' .
'symfony/polyfill-[^/]+/Resources/stubs/(?:Attribute|Normalizer)|' .
'php-coveralls/php-coveralls/src/Bundle/CoverallsBundle/Console/Application|' .
'dealerdirect/phpcodesniffer-composer-installer/src/Plugin|' .
'myclabs/php-enum/src/PHPUnit/Comparator|' .
Expand All @@ -22,9 +23,14 @@ final class BogonFilesFilter
')\.php$)' .
'#x';

/**
* @param Closure(string):bool|null $filter
*
* @return Closure(string):bool
*/
public static function getFileFilterFn(Closure|null $filter = null): Closure
{
$filter ??= static fn () => true;
$filter ??= static fn (string $unused) => true;

return static function (string $path) use ($filter): bool {
if (preg_match(self::BOGON_FILES_REGEX, $path) === 1) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Util/PhpDocumentor/MetadataRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kcs\ClassFinder\Util\PhpDocumentor;

use PhpDocumentor\Reflection\Metadata\Metadata;
use phpDocumentor\Reflection\Metadata\Metadata;
use WeakMap;

final class MetadataRegistry
Expand Down
2 changes: 1 addition & 1 deletion phpdoc-compat-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace PhpDocumentor\Reflection\Metadata;
namespace phpDocumentor\Reflection\Metadata;

use function interface_exists;

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/Finder/ComposerFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ public function testFinderShouldFilterByNotPathRegex(): void
], iterator_to_array($finder));
}

/** @runInSeparateProcess */
public function testFinderShouldFilterByPathCallback(): void
{
$finder = (new ComposerFinder())->useAutoloading(false);
Expand All @@ -271,4 +272,12 @@ public function testFinderShouldFilterByPathCallback(): void
Psr4\Foobarbar::class => new ReflectionClass(Psr4\Foobarbar::class),
], iterator_to_array($finder));
}

/** @runInSeparateProcess */
public function testBogonFilesFilter(): void
{
$finder = (new ComposerFinder())->skipBogonFiles();

self::assertNotEmpty(iterator_to_array($finder));
}
}

0 comments on commit 5e125cf

Please sign in to comment.