Skip to content
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
],
"require": {
"php": "^8.0",
"nikic/php-parser": "^4.15",
"phpbench/phpbench": "^1.2",
"symfony/finder": "^5.4|^6.0|^7.1"
},
"require-dev": {
Expand Down
12 changes: 12 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "tests/Benchmark",
"runner.retry_threshold": 3,
"report.outputs": {
"csv_file": {
"extends": "delimited",
"delimiter": ",",
"file": "benchmarks.csv"
}
}
}
108 changes: 108 additions & 0 deletions src/PhpParserClassifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Classifier;

use PhpParser\Node;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
use Symfony\Component\Finder\Finder;

final class PhpParserClassifier
{
/**
* @var string[]
*/
private array $interfaces = [];
/**
* @var string[]
*/
private array $attributes = [];
private \PhpParser\Parser $parser;
private NodeTraverser $traverser;
private NodeFinder $nodeFinder;

public function __construct(private string $directory)
{
$traverser = new NodeTraverser();
$nameResolver = new NameResolver();
$traverser->addVisitor($nameResolver);
$this->parser = (new ParserFactory())
->create(ParserFactory::PREFER_PHP7);
$this->traverser = $traverser;
$this->nodeFinder = new NodeFinder();
}

public function withInterface(string|array $interfaces): self
{
$new = clone $this;
foreach ((array) $interfaces as $interface) {
$new->interfaces[] = $interface;
}
return $new;
}

public function withAttribute(string|array $attributes): self
{
$new = clone $this;
foreach ((array) $attributes as $attribute) {
$new->attributes[] = $attribute;
}
return $new;
}

public function find(): iterable
{
$countInterfaces = count($this->interfaces);
$countAttributes = count($this->attributes);

if ($countInterfaces === 0 && $countAttributes === 0) {
return [];
}

$files = (new Finder())
->in($this->directory)
->name('*.php')
->sortByName()
->files();

$interfaces = $this->interfaces;
$attributes = $this->attributes;

foreach ($files as $file) {
$nodes = $this->parser->parse(file_get_contents($file->getRealPath()));
$this->traverser->traverse($nodes);
/**
* @var $result Node\Stmt\Class_[]
*/
$result = $this->nodeFinder->find(
$nodes,
function (Node $node) use ($interfaces, $countInterfaces, $attributes, $countAttributes) {
if (!$node instanceof Node\Stmt\Class_) {
return false;
}
$interfacesNames = array_map(fn (Node\Name $name) => $name->toString(), $node->implements);
if (count(array_intersect($interfaces, $interfacesNames)) !== $countInterfaces) {
return false;
}
$attributesNames = [];
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
$attributesNames[] = $attr->name->toString();
}
}
return !(count(array_intersect($attributes, $attributesNames)) !== $countAttributes)


;
}
);
foreach ($result as $class) {
yield $class->namespacedName->toString();
}
}
}
}
120 changes: 120 additions & 0 deletions tests/BaseClassifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Classifier\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Classifier\Classifier;
use Yiisoft\Classifier\PhpParserClassifier;
use Yiisoft\Classifier\Tests\Support\Attributes\AuthorAttribute;
use Yiisoft\Classifier\Tests\Support\Author;
use Yiisoft\Classifier\Tests\Support\AuthorPost;
use Yiisoft\Classifier\Tests\Support\Interfaces\PostInterface;
use Yiisoft\Classifier\Tests\Support\Interfaces\UserInterface;
use Yiisoft\Classifier\Tests\Support\Post;
use Yiisoft\Classifier\Tests\Support\PostUser;
use Yiisoft\Classifier\Tests\Support\User;
use Yiisoft\Classifier\Tests\Support\UserSubclass;

abstract class BaseClassifierTest extends TestCase
{
/**
* @dataProvider dataProviderInterfaces
*/
public function testInterfaces(string|array $interfaces, array $expectedClasses)
{
$finder = $this->createClassifier(__DIR__);
$finder = $finder->withInterface($interfaces);

$result = $finder->find();

$this->assertEquals($expectedClasses, iterator_to_array($result));
}

/**
* @dataProvider attributesDataProvider
*/
public function testAttributes(string|array $attributes, array $expectedClasses)
{
$finder = $this->createClassifier(__DIR__);
$finder = $finder->withAttribute($attributes);

$result = $finder->find();

$this->assertEquals($expectedClasses, iterator_to_array($result));
}

/**
* @dataProvider mixedDataProvider
*/
public function testMixed(array $attributes, array $interfaces, array $expectedClasses)
{
$finder = $this->createClassifier(__DIR__);
$finder = $finder
->withAttribute($attributes)
->withInterface($interfaces);

$result = $finder->find();

$this->assertEquals($expectedClasses, iterator_to_array($result));
}

public function dataProviderInterfaces(): array
{
return [
// [
// [],
// [],
// ],
// [
// PostInterface::class,
// [AuthorPost::class, Post::class, PostUser::class],
// ],
// [
// [PostInterface::class],
// [AuthorPost::class, Post::class, PostUser::class],
// ],
[
[UserInterface::class],
[PostUser::class, User::class, UserSubclass::class],
],
// [
// [PostInterface::class, UserInterface::class],
// [PostUser::class],
// ],
];
}

public function attributesDataProvider(): array
{
return [
[
[],
[],
],
[
AuthorAttribute::class,
[Author::class, AuthorPost::class],
],
];
}

public function mixedDataProvider(): array
{
return [
[
[],
[],
[],
],
[
[AuthorAttribute::class],
[PostInterface::class],
[AuthorPost::class],
],
];
}

abstract protected function createClassifier(string $directory): Classifier|PhpParserClassifier;
}
42 changes: 42 additions & 0 deletions tests/Benchmark/Engine2Bench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Classifier\Tests\Benchmark;

use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use Yiisoft\Classifier\PhpParserClassifier;
use Yiisoft\Classifier\Tests\Support\Interfaces\PostInterface;
use Yiisoft\Classifier\Tests\Support\Interfaces\UserInterface;

final class Engine2Bench
{
private PhpParserClassifier $finder;

public function beforeTest()
{
$this->finder = new PhpParserClassifier(__DIR__);
}

/**
* @BeforeMethods("beforeTest")
* @ParamProviders("dataProviderInterfaces")
* @Revs(1000)
*/
public function benchTest(array $interfaces): void
{
$finder = $this->finder->withInterface($interfaces);

$finder->find();
}

public function dataProviderInterfaces(): array
{
return [
[PostInterface::class],
[UserInterface::class, PostInterface::class],
];
}
}
42 changes: 42 additions & 0 deletions tests/Benchmark/EngineBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Classifier\Tests\Benchmark;

use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use Yiisoft\Classifier\Classifier;
use Yiisoft\Classifier\Tests\Support\Interfaces\PostInterface;
use Yiisoft\Classifier\Tests\Support\Interfaces\UserInterface;

final class EngineBench
{
private Classifier $finder;

public function beforeTest()
{
$this->finder = new Classifier(__DIR__);
}

/**
* @BeforeMethods("beforeTest")
* @ParamProviders("dataProviderInterfaces")
* @Revs(1000)
*/
public function benchTest(array $interfaces): void
{
$finder = $this->finder->withInterface($interfaces);

$finder->find();
}

public function dataProviderInterfaces(): array
{
return [
[PostInterface::class],
[UserInterface::class],
];
}
}
10 changes: 8 additions & 2 deletions tests/ClassifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Yiisoft\Classifier\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Classifier\Classifier;
use Yiisoft\Classifier\Tests\Support\Attributes\AuthorAttribute;
use Yiisoft\Classifier\Tests\Support\Author;
Expand All @@ -15,13 +14,20 @@
use Yiisoft\Classifier\Tests\Support\Interfaces\UserInterface;
use Yiisoft\Classifier\Tests\Support\Post;
use Yiisoft\Classifier\Tests\Support\PostUser;
use Yiisoft\Classifier\Tests\Support\User;
use Yiisoft\Classifier\Tests\Support\UserSubclass;
use Yiisoft\Classifier\Tests\Support\SuperSuperUser;
use Yiisoft\Classifier\Tests\Support\SuperUser;
use Yiisoft\Classifier\Tests\Support\User;
use Yiisoft\Classifier\Tests\Support\UserSubclass;

final class ClassifierTest extends TestCase
final class ClassifierTest extends BaseClassifierTest
{
protected function createClassifier(string $directory): Classifier
{
return new Classifier($directory);
}

public function testMultipleDirectories()
{
$dirs = [__DIR__ . '/Support/Dir1', __DIR__ . '/Support/Dir2'];
Expand Down
15 changes: 15 additions & 0 deletions tests/PhpParserClassifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Classifier\Tests;

use Yiisoft\Classifier\PhpParserClassifier;

final class PhpParserClassifierTest extends BaseClassifierTest
{
protected function createClassifier(string $directory): PhpParserClassifier
{
return new PhpParserClassifier($directory);
}
}