Skip to content

Commit

Permalink
Merge c455f86 into 3b25199
Browse files Browse the repository at this point in the history
  • Loading branch information
hkulekci committed Mar 7, 2022
2 parents 3b25199 + c455f86 commit a0a7055
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 53 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jobs:
max-parallel: 1 # all versions are using same elasticsearch and mysql service. They need to run 1 by 1.
matrix:
include:
- php-version: '7.3'
- php-version: '7.4'
- php-version: '8.0.16'
- php-version: '8.1'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.1
Expand Down
4 changes: 2 additions & 2 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ filter:
build:
environment:
php:
version: '7.3'
version: '8.0.16'
variables:
ELASTICSEARCH_HOST: 'localhost:9200'
DB_HOST: '127.0.0.1'
Expand All @@ -29,7 +29,7 @@ build:
override:
- php-scrutinizer-run
-
command: 'vendor/bin/phpunit --coverage-clover=coverage.clover'
command: 'vendor/bin/phpunit --coverage-clover /home/scrutinizer/build/coverage.clover'
coverage:
file: 'coverage.clover'
format: 'clover'
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
}
],
"require": {
"php": "^7.2|^7.3|^7.4",
"php": "^8.0.12|^8.1",
"elasticsearch/elasticsearch": "^7.2",
"laravel/scout": "^8.0|^9.0",
"ongr/elasticsearch-dsl": "^7.0",
"roave/better-reflection": "^3.3|^4.3"
"roave/better-reflection": "^4.3|^5.0"
},
"require-dev": {
"laravel/legacy-factories": "^1.0",
"nunomaduro/larastan": "~0.6",
"orchestra/testbench": "^6.18",
"phpunit/phpunit": "^8.0|~9.4.0"
"phpunit/phpunit": "~9.4.0"
},
"autoload-dev": {
"psr-4": {
Expand Down
13 changes: 9 additions & 4 deletions src/ElasticSearch/EloquentHitsIteratorAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ final class EloquentHitsIteratorAggregate implements IteratorAggregate
private $callback;

/**
* @param array $results
* @param callable|null $callback
* @param array $results
* @param callable|null $callback
*/
public function __construct(array $results, callable $callback = null)
{
Expand All @@ -33,9 +33,12 @@ public function __construct(array $results, callable $callback = null)

/**
* Retrieve an external iterator.
*
* @link https://php.net/manual/en/iteratoraggregate.getiterator.php
*
* @return Traversable An instance of an object implementing <b>Iterator</b> or
* <b>Traversable</b>
* <b>Traversable</b>
*
* @since 5.0.0
*/
public function getIterator()
Expand All @@ -45,12 +48,14 @@ public function getIterator()
$hits = $this->results['hits']['hits'];
$models = collect($hits)->groupBy('_source.__class_name')
->map(function ($results, $class) {
/** @var Searchable $model */
$model = new $class;
$model->setKeyType('string');
$builder = new Builder($model, '');
if (! empty($this->callback)) {
$builder->query($this->callback);
}
/* @var Searchable $model */

return $models = $model->getScoutModelsByIds(
$builder, $results->pluck('_id')->all()
);
Expand Down
4 changes: 2 additions & 2 deletions src/Mixed.php → src/MixedSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Laravel\Scout\Builder;
use Laravel\Scout\Searchable;

final class Mixed
final class MixedSearch
{
/**
* Perform a search against the model's indexed data.
Expand All @@ -15,7 +15,7 @@ final class Mixed
* @param \Closure $callback
* @return \Laravel\Scout\Builder
*/
public static function search($query = '', $callback = null)
public static function search(string $query = '', $callback = null): Builder
{
return new Builder(new class extends Model
{
Expand Down
58 changes: 23 additions & 35 deletions src/Searchable/SearchableListFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,36 @@
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\ParserFactory;
use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use Roave\BetterReflection\Reflector\Reflector;
use Symfony\Component\Finder\Finder;

final class SearchableListFactory
{
/**
* @var array
* @var array|null
*/
private static $searchableClasses;
private static ?array $searchableClasses = null;
/**
* @var string
*/
private $namespace;
private string $namespace;
/**
* @var string
*/
private $appPath;
private string $appPath;
/**
* @var array
*/
private $errors = [];
private array $errors = [];
/**
* @var ClassReflector
* @var Reflector|null
*/
private $classReflector;
private ?Reflector $reflector = null;

/**
* @param string $namespace
* @param string $namespace
* @param string $appPath
*/
public function __construct(string $namespace, string $appPath)
{
Expand Down Expand Up @@ -76,7 +77,7 @@ private function find(): array
{
$appNamespace = $this->namespace;

return array_values(array_filter($this->getSearchableClasses(), function (string $class) use ($appNamespace) {
return array_values(array_filter($this->getSearchableClasses(), static function (string $class) use ($appNamespace) {
return Str::startsWith($class, $appNamespace);
}));
}
Expand All @@ -87,9 +88,7 @@ private function find(): array
private function getSearchableClasses(): array
{
if (self::$searchableClasses === null) {
$projectClasses = $this->getProjectClasses();

self::$searchableClasses = $projectClasses->filter(function ($class) {
self::$searchableClasses = $this->getProjectClasses()->filter(function ($class) {
return $this->findSearchableTraitRecursively($class);
})->toArray();
}
Expand All @@ -102,9 +101,8 @@ private function getSearchableClasses(): array
*/
private function getProjectClasses(): Collection
{
$nodeFinder = new NodeFinder();
/** @var Class_[] $nodes */
$nodes = $nodeFinder->find($this->getStmts(), function (Node $node) {
$nodes = (new NodeFinder())->find($this->getStmts(), function (Node $node) {
return $node instanceof Class_;
});

Expand All @@ -123,31 +121,27 @@ private function getStmts(): array
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($nameResolverVisitor);
$stmts = [];
$finder = Finder::create()->files()->name('*.php')->in($this->appPath);

foreach ($finder as $file) {
foreach (Finder::create()->files()->name('*.php')->in($this->appPath) as $file) {
try {
$stmts[] = $parser->parse($file->getContents());
} catch (Error $e) {
$this->errors[] = $e->getMessage();
continue;
}
}

$stmts = Collection::make($stmts)->flatten(1)->toArray();
$stmts = $nodeTraverser->traverse($stmts);

return $stmts;
return $nodeTraverser->traverse($stmts);
}

/**
* @param string $class
* @param string $class
* @return bool
*/
private function findSearchableTraitRecursively(string $class): bool
{
try {
$reflection = $this->classReflector()->reflect($class);
$reflection = $this->reflector()->reflectClass($class);

if (in_array(Searchable::class, $traits = $reflection->getTraitNames())) {
return true;
Expand All @@ -159,13 +153,7 @@ private function findSearchableTraitRecursively(string $class): bool
}
}

if ($parent = $reflection->getParentClass()) {
if ($this->findSearchableTraitRecursively($parent->getName())) {
return true;
}
}

return false;
return ($parent = $reflection->getParentClass()) && $this->findSearchableTraitRecursively($parent->getName());
} catch (IdentifierNotFound $e) {
$this->errors[] = $e->getMessage();

Expand All @@ -174,14 +162,14 @@ private function findSearchableTraitRecursively(string $class): bool
}

/**
* @return ClassReflector
* @return Reflector
*/
private function classReflector(): ClassReflector
private function reflector(): Reflector
{
if (null === $this->classReflector) {
$this->classReflector = (new BetterReflection())->classReflector();
if (null === $this->reflector) {
$this->reflector = (new BetterReflection())->reflector();
}

return $this->classReflector;
return $this->reflector;
}
}
6 changes: 3 additions & 3 deletions tests/Feature/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Ticket;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Artisan;
use Matchish\ScoutElasticSearch\Mixed;
use Matchish\ScoutElasticSearch\MixedSearch;
use Tests\IntegrationTestCase;

final class SearchTest extends IntegrationTestCase
Expand Down Expand Up @@ -119,7 +119,7 @@ public function test_mixed()

Artisan::call('scout:import');

$mixed = Mixed::search('Barcelona')->within(
$mixed = MixedSearch::search('Barcelona')->within(
implode(',', [
(new Book)->searchableAs(),
(new Ticket())->searchableAs(),
Expand All @@ -132,7 +132,7 @@ public function test_mixed_no_results()
{
Artisan::call('scout:import');

$mixed = Mixed::search('lisbon')->within(
$mixed = MixedSearch::search('lisbon')->within(
implode(',', [(new Book)->searchableAs(),
(new Ticket())->searchableAs(),
]))->get();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/MixedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Tests\Unit;

use Laravel\Scout\Searchable;
use Matchish\ScoutElasticSearch\Mixed;
use Matchish\ScoutElasticSearch\MixedSearch;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

Expand All @@ -14,7 +14,7 @@ public function test_search_signature()
$searchable = new ReflectionClass(Searchable::class);
$searchableParameters = $searchable->getMethod('search')->getParameters();
$searchableDoc = $searchable->getMethod('search')->getDocComment();
$mixed = new ReflectionClass(Mixed::class);
$mixed = new ReflectionClass(MixedSearch::class);
$mixedParameters = $mixed->getMethod('search')->getParameters();
$mixedDoc = $mixed->getMethod('search')->getDocComment();

Expand Down

0 comments on commit a0a7055

Please sign in to comment.