Skip to content

Parser classifier #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ finder:
exclude:
- docs
- vendor
not-name:
- wrong_file.php

enabled:
- alpha_ordered_traits
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
[![Latest Stable Version](https://poser.pugx.org/yiisoft/classifier/v/stable.png)](https://packagist.org/packages/yiisoft/classifier)
[![Total Downloads](https://poser.pugx.org/yiisoft/classifier/downloads.png)](https://packagist.org/packages/yiisoft/classifier)
[![Build status](https://github.com/yiisoft/classifier/workflows/build/badge.svg)](https://github.com/yiisoft/classifier/actions?query=workflow%3Abuild)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yiisoft/classifier/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yiisoft/classifier/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/yiisoft/classifier/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/yiisoft/classifier/?branch=master)
[![Code Coverage](https://codecov.io/gh/yiisoft/classifier/branch/master/graph/badge.svg)](https://codecov.io/gh/yiisoft/classifier)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fclassifier%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/classifier/master)
[![static analysis](https://github.com/yiisoft/classifier/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/classifier/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/classifier/coverage.svg)](https://shepherd.dev/github/yiisoft/classifier)
[![psalm-level](https://shepherd.dev/github/yiisoft/classifier/level.svg)](https://shepherd.dev/github/yiisoft/classifier)

Classifier traverses file system to find classes by a certain criteria.

Expand Down
11 changes: 11 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"symbol-whitelist": [
"PhpParser\\Node",
"PhpParser\\NodeTraverser",
"PhpParser\\NodeVisitorAbstract",
"PhpParser\\NodeVisitor\\NameResolver",
"PhpParser\\Node\\Stmt\\Class_",
"PhpParser\\Parser",
"PhpParser\\ParserFactory"
]
}
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"require-dev": {
"maglnet/composer-require-checker": "^4.2",
"nikic/php-parser": "^4.17",
"phpunit/phpunit": "^9.5",
"rector/rector": "^1.0.0",
"roave/infection-static-analysis-plugin": "^1.16",
Expand All @@ -51,6 +52,9 @@
"Yiisoft\\Classifier\\Tests\\": "tests"
}
},
"suggest": {
"nikic/php-parser": "Need for ParserClassifier implementation"
},
"config": {
"sort-packages": true,
"allow-plugins": {
Expand Down
4 changes: 4 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
__DIR__ . '/tests',
]);

$rectorConfig->skip([
__DIR__ . '/tests/Support/wrong_file.php',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

Expand Down
93 changes: 93 additions & 0 deletions src/AbstractClassifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Classifier;

use Symfony\Component\Finder\Finder;

/**
* Base implementation for {@see ClassifierInterface} with common filters.
*/
abstract class AbstractClassifier implements ClassifierInterface
{
/**
* @var string[]
*/
protected array $interfaces = [];
/**
* @var string[]
*/
protected array $attributes = [];
/**
* @psalm-var class-string
*/
protected ?string $parentClass = null;
/**
* @var string[]
*/
protected array $directories;

public function __construct(string $directory, string ...$directories)
{
$this->directories = [$directory, ...array_values($directories)];

Check warning on line 33 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayValues": --- Original +++ New @@ @@ protected array $directories; public function __construct(string $directory, string ...$directories) { - $this->directories = [$directory, ...array_values($directories)]; + $this->directories = [$directory, ...$directories]; } /** * @psalm-param class-string ...$interfaces

Check warning on line 33 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayValues": --- Original +++ New @@ @@ protected array $directories; public function __construct(string $directory, string ...$directories) { - $this->directories = [$directory, ...array_values($directories)]; + $this->directories = [$directory, ...$directories]; } /** * @psalm-param class-string ...$interfaces
}

/**
* @psalm-param class-string ...$interfaces
*/
public function withInterface(string ...$interfaces): self
{
$new = clone $this;

Check warning on line 41 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function withInterface(string ...$interfaces) : self { - $new = clone $this; + $new = $this; array_push($new->interfaces, ...array_values($interfaces)); return $new; }

Check warning on line 41 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function withInterface(string ...$interfaces) : self { - $new = clone $this; + $new = $this; array_push($new->interfaces, ...array_values($interfaces)); return $new; }
array_push($new->interfaces, ...array_values($interfaces));

Check warning on line 42 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayValues": --- Original +++ New @@ @@ public function withInterface(string ...$interfaces) : self { $new = clone $this; - array_push($new->interfaces, ...array_values($interfaces)); + array_push($new->interfaces, ...$interfaces); return $new; } /**

Check warning on line 42 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayValues": --- Original +++ New @@ @@ public function withInterface(string ...$interfaces) : self { $new = clone $this; - array_push($new->interfaces, ...array_values($interfaces)); + array_push($new->interfaces, ...$interfaces); return $new; } /**

return $new;
}

/**
* @psalm-param class-string $parentClass
*/
public function withParentClass(string $parentClass): self
{
$new = clone $this;

Check warning on line 52 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function withParentClass(string $parentClass) : self { - $new = clone $this; + $new = $this; $new->parentClass = $parentClass; return $new; }

Check warning on line 52 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function withParentClass(string $parentClass) : self { - $new = clone $this; + $new = $this; $new->parentClass = $parentClass; return $new; }
$new->parentClass = $parentClass;
return $new;
}

/**
* @psalm-param class-string ...$attributes
*/
public function withAttribute(string ...$attributes): self
{
$new = clone $this;

Check warning on line 62 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function withAttribute(string ...$attributes) : self { - $new = clone $this; + $new = $this; array_push($new->attributes, ...array_values($attributes)); return $new; }

Check warning on line 62 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function withAttribute(string ...$attributes) : self { - $new = clone $this; + $new = $this; array_push($new->attributes, ...array_values($attributes)); return $new; }
array_push($new->attributes, ...array_values($attributes));

Check warning on line 63 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayValues": --- Original +++ New @@ @@ public function withAttribute(string ...$attributes) : self { $new = clone $this; - array_push($new->attributes, ...array_values($attributes)); + array_push($new->attributes, ...$attributes); return $new; } /**

Check warning on line 63 in src/AbstractClassifier.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayValues": --- Original +++ New @@ @@ public function withAttribute(string ...$attributes) : self { $new = clone $this; - array_push($new->attributes, ...array_values($attributes)); + array_push($new->attributes, ...$attributes); return $new; } /**

return $new;
}

/**
* @psalm-return iterable<class-string>
*/
public function find(): iterable
{
if (empty($this->interfaces) && empty($this->attributes) && $this->parentClass === null) {
return [];
}

yield from $this->getAvailableClasses();
}

protected function getFiles(): Finder
{
return (new Finder())
->in($this->directories)
->name('*.php')
->sortByName()
->files();
}

/**
* @return iterable<class-string>
*/
abstract protected function getAvailableClasses(): iterable;
}
163 changes: 0 additions & 163 deletions src/Classifier.php

This file was deleted.

19 changes: 19 additions & 0 deletions src/ClassifierInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Classifier;

/**
* `Classifier` is a class finder that represents the classes found.
*/
interface ClassifierInterface
{
/**
* Returns all the class names found.
*
* @return iterable List of class names.
* @psalm-return iterable<class-string>
*/
public function find(): iterable;
}
Loading
Loading