Skip to content

Commit

Permalink
refactor: adhere to max phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocksheep committed Nov 15, 2024
1 parent 92ca323 commit e86a06a
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 18 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ parameters:
- src/

# Level 10 is the highest level
level: 6
level: max
8 changes: 7 additions & 1 deletion src/Commands/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swis\Laravel\Fulltext\Commands;

use Illuminate\Console\Command;
use InvalidArgumentException;
use Swis\Laravel\Fulltext\Indexer;

class Index extends Command
Expand All @@ -21,8 +22,13 @@ class Index extends Command
*/
public function handle(): int
{
$modelClass = $this->argument('model_class');
if (!is_string($modelClass)) {
throw new InvalidArgumentException('Model class must be a string');
}

$indexer = new Indexer;
$indexer->indexAllByClass($this->argument('model_class'));
$indexer->indexAllByClass($modelClass);

return self::SUCCESS;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Commands/IndexOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swis\Laravel\Fulltext\Commands;

use Illuminate\Console\Command;
use InvalidArgumentException;
use Swis\Laravel\Fulltext\Indexer;

class IndexOne extends Command
Expand All @@ -21,8 +22,18 @@ class IndexOne extends Command
*/
public function handle(): int
{
$modelClass = $this->argument('model_class');
if (!is_string($modelClass)) {
throw new InvalidArgumentException('Model class must be a string');
}

$id = $this->argument('id');
if (!is_string($id)) {
throw new InvalidArgumentException('ID must be a string or an integer');
}

$indexer = new Indexer;
$indexer->indexOneByClass($this->argument('model_class'), $this->argument('id'));
$indexer->indexOneByClass($modelClass, $id);

return self::SUCCESS;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Commands/UnindexOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swis\Laravel\Fulltext\Commands;

use Illuminate\Console\Command;
use InvalidArgumentException;
use Swis\Laravel\Fulltext\Indexer;

class UnindexOne extends Command
Expand All @@ -21,8 +22,18 @@ class UnindexOne extends Command
*/
public function handle(): int
{
$modelClass = $this->argument('model_class');
if (!is_string($modelClass)) {
throw new InvalidArgumentException('Model class must be a string');
}

$id = $this->argument('id');
if (!is_string($id)) {
throw new InvalidArgumentException('ID must be a string or an integer');
}

$indexer = new Indexer;
$indexer->unIndexOneByClass($this->argument('model_class'), $this->argument('id'));
$indexer->unIndexOneByClass($modelClass, $id);

return self::SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion src/IndexedRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IndexedRecord extends Model
*/
public function __construct(array $attributes = [])
{
$this->connection = Config::get('laravel-fulltext.db_connection');
$this->connection = Config::string('laravel-fulltext.db_connection');

parent::__construct($attributes);
}
Expand Down
25 changes: 19 additions & 6 deletions src/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Swis\Laravel\Fulltext;

use Illuminate\Database\Eloquent\Model;
use Swis\Laravel\Fulltext\Contracts\Indexable;

class Indexer
Expand All @@ -13,8 +14,13 @@ public function indexModel(Indexable $indexable): void

public function unIndexOneByClass(string $class, string|int $id): void
{
if (!is_subclass_of($class, Model::class)) {
return;
}

$record = IndexedRecord::query()
->where('indexable_id', $id)->where('indexable_type', (new $class)->getMorphClass());
->where('indexable_id', $id)
->where('indexable_type', (new $class)->getMorphClass());

if ($record->exists()) {
$record->delete();
Expand All @@ -23,20 +29,27 @@ public function unIndexOneByClass(string $class, string|int $id): void

public function indexOneByClass(string $class, string|int $id): void
{
if (!is_subclass_of($class, Model::class)) {
return;
}

$model = call_user_func([$class, 'find'], $id);
if (in_array(Indexable::class, class_uses($model), true)) {
if ($model instanceof Indexable) {
$this->indexModel($model);
}
}

public function indexAllByClass(string $class): void
{
if (!is_subclass_of($class, Model::class)) {
return;
}

$model = new $class;
$self = $this;
if (in_array(Indexable::class, class_uses($model), true)) {
$model->chunk(100, function ($chunk) use ($self) {
if ($model instanceof Indexable) {
$model->newQuery()->chunk(100, function ($chunk) {
foreach ($chunk as $modelRecord) {
$self->indexModel($modelRecord);
$this->indexModel($modelRecord);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/ModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ModelObserver
/**
* The class names that syncing is disabled for.
*
* @var list<string>
* @var array<string, bool>
*/
protected static array $syncingDisabledFor = [];

Expand Down
13 changes: 9 additions & 4 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swis\Laravel\Fulltext;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;

Expand All @@ -17,6 +18,10 @@ public function run(string $search): Collection

public function runForClass(string $search, string $class): Collection
{
if (!is_subclass_of($class, Model::class)) {
return new Collection();
}

$query = $this->searchQuery($search);
$query->where('indexable_type', (new $class)->getMorphClass());

Expand All @@ -35,8 +40,8 @@ public function searchQuery(string $search): Builder
$termsMatch = $terms->implode(' ');
}

$titleWeight = str_replace(',', '.', sprintf('%f', Config::get('laravel-fulltext.weight.title', 1.5)));
$contentWeight = str_replace(',', '.', sprintf('%f', Config::get('laravel-fulltext.weight.content', 1.0)));
$titleWeight = str_replace(',', '.', sprintf('%f', Config::float('laravel-fulltext.weight.title', 1.5)));
$contentWeight = str_replace(',', '.', sprintf('%f', Config::float('laravel-fulltext.weight.content', 1.0)));

$query = IndexedRecord::query()
->whereRaw('MATCH (indexed_title, indexed_content) AGAINST (? IN BOOLEAN MODE)', [$termsBool])
Expand All @@ -45,9 +50,9 @@ public function searchQuery(string $search): Builder
'.$contentWeight.' * (MATCH (indexed_title, indexed_content) AGAINST (?))
) DESC',
[$termsMatch, $termsMatch])
->limit(Config::get('laravel-fulltext.limit-results'));
->limit(Config::integer('laravel-fulltext.limit-results'));

if (Config::get('laravel-fulltext.exclude_feature_enabled')) {
if (Config::boolean('laravel-fulltext.exclude_feature_enabled')) {
$query->with(['indexable' => function ($query) {
$query->where(Config::get('laravel-fulltext.exclude_records_column_name'), '=', true);
}]);
Expand Down
4 changes: 2 additions & 2 deletions src/TermBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public static function terms(string $search): Collection

// Remove every boolean operator (+, -, > <, ( ), ~, *, ", @distance) from the search query
// else we will break the MySQL query.
$search = trim(preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $search));
$search = trim(preg_replace('/[+\-><\(\)~*\"@]+/', ' ', $search) ?: '');

$terms = collect(preg_split('/[\s,]+/', $search))->filter();
$terms = collect(preg_split('/[\s,]+/', $search) ?: [])->filter();

if ($wildcards === true) {
$terms->transform(function ($term) {
Expand Down

0 comments on commit e86a06a

Please sign in to comment.