Skip to content

Commit e86a06a

Browse files
committed
refactor: adhere to max phpstan
1 parent 92ca323 commit e86a06a

9 files changed

+64
-18
lines changed

phpstan.neon.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ parameters:
44
- src/
55

66
# Level 10 is the highest level
7-
level: 6
7+
level: max

src/Commands/Index.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Swis\Laravel\Fulltext\Commands;
44

55
use Illuminate\Console\Command;
6+
use InvalidArgumentException;
67
use Swis\Laravel\Fulltext\Indexer;
78

89
class Index extends Command
@@ -21,8 +22,13 @@ class Index extends Command
2122
*/
2223
public function handle(): int
2324
{
25+
$modelClass = $this->argument('model_class');
26+
if (!is_string($modelClass)) {
27+
throw new InvalidArgumentException('Model class must be a string');
28+
}
29+
2430
$indexer = new Indexer;
25-
$indexer->indexAllByClass($this->argument('model_class'));
31+
$indexer->indexAllByClass($modelClass);
2632

2733
return self::SUCCESS;
2834
}

src/Commands/IndexOne.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Swis\Laravel\Fulltext\Commands;
44

55
use Illuminate\Console\Command;
6+
use InvalidArgumentException;
67
use Swis\Laravel\Fulltext\Indexer;
78

89
class IndexOne extends Command
@@ -21,8 +22,18 @@ class IndexOne extends Command
2122
*/
2223
public function handle(): int
2324
{
25+
$modelClass = $this->argument('model_class');
26+
if (!is_string($modelClass)) {
27+
throw new InvalidArgumentException('Model class must be a string');
28+
}
29+
30+
$id = $this->argument('id');
31+
if (!is_string($id)) {
32+
throw new InvalidArgumentException('ID must be a string or an integer');
33+
}
34+
2435
$indexer = new Indexer;
25-
$indexer->indexOneByClass($this->argument('model_class'), $this->argument('id'));
36+
$indexer->indexOneByClass($modelClass, $id);
2637

2738
return self::SUCCESS;
2839
}

src/Commands/UnindexOne.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Swis\Laravel\Fulltext\Commands;
44

55
use Illuminate\Console\Command;
6+
use InvalidArgumentException;
67
use Swis\Laravel\Fulltext\Indexer;
78

89
class UnindexOne extends Command
@@ -21,8 +22,18 @@ class UnindexOne extends Command
2122
*/
2223
public function handle(): int
2324
{
25+
$modelClass = $this->argument('model_class');
26+
if (!is_string($modelClass)) {
27+
throw new InvalidArgumentException('Model class must be a string');
28+
}
29+
30+
$id = $this->argument('id');
31+
if (!is_string($id)) {
32+
throw new InvalidArgumentException('ID must be a string or an integer');
33+
}
34+
2435
$indexer = new Indexer;
25-
$indexer->unIndexOneByClass($this->argument('model_class'), $this->argument('id'));
36+
$indexer->unIndexOneByClass($modelClass, $id);
2637

2738
return self::SUCCESS;
2839
}

src/IndexedRecord.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class IndexedRecord extends Model
1919
*/
2020
public function __construct(array $attributes = [])
2121
{
22-
$this->connection = Config::get('laravel-fulltext.db_connection');
22+
$this->connection = Config::string('laravel-fulltext.db_connection');
2323

2424
parent::__construct($attributes);
2525
}

src/Indexer.php

+19-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Swis\Laravel\Fulltext;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Swis\Laravel\Fulltext\Contracts\Indexable;
67

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

1415
public function unIndexOneByClass(string $class, string|int $id): void
1516
{
17+
if (!is_subclass_of($class, Model::class)) {
18+
return;
19+
}
20+
1621
$record = IndexedRecord::query()
17-
->where('indexable_id', $id)->where('indexable_type', (new $class)->getMorphClass());
22+
->where('indexable_id', $id)
23+
->where('indexable_type', (new $class)->getMorphClass());
1824

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

2430
public function indexOneByClass(string $class, string|int $id): void
2531
{
32+
if (!is_subclass_of($class, Model::class)) {
33+
return;
34+
}
35+
2636
$model = call_user_func([$class, 'find'], $id);
27-
if (in_array(Indexable::class, class_uses($model), true)) {
37+
if ($model instanceof Indexable) {
2838
$this->indexModel($model);
2939
}
3040
}
3141

3242
public function indexAllByClass(string $class): void
3343
{
44+
if (!is_subclass_of($class, Model::class)) {
45+
return;
46+
}
47+
3448
$model = new $class;
35-
$self = $this;
36-
if (in_array(Indexable::class, class_uses($model), true)) {
37-
$model->chunk(100, function ($chunk) use ($self) {
49+
if ($model instanceof Indexable) {
50+
$model->newQuery()->chunk(100, function ($chunk) {
3851
foreach ($chunk as $modelRecord) {
39-
$self->indexModel($modelRecord);
52+
$this->indexModel($modelRecord);
4053
}
4154
});
4255
}

src/ModelObserver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ModelObserver
99
/**
1010
* The class names that syncing is disabled for.
1111
*
12-
* @var list<string>
12+
* @var array<string, bool>
1313
*/
1414
protected static array $syncingDisabledFor = [];
1515

src/Search.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Swis\Laravel\Fulltext;
44

55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Facades\Config;
89

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

1819
public function runForClass(string $search, string $class): Collection
1920
{
21+
if (!is_subclass_of($class, Model::class)) {
22+
return new Collection();
23+
}
24+
2025
$query = $this->searchQuery($search);
2126
$query->where('indexable_type', (new $class)->getMorphClass());
2227

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

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

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

50-
if (Config::get('laravel-fulltext.exclude_feature_enabled')) {
55+
if (Config::boolean('laravel-fulltext.exclude_feature_enabled')) {
5156
$query->with(['indexable' => function ($query) {
5257
$query->where(Config::get('laravel-fulltext.exclude_records_column_name'), '=', true);
5358
}]);

src/TermBuilder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public static function terms(string $search): Collection
1616

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

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

2323
if ($wildcards === true) {
2424
$terms->transform(function ($term) {

0 commit comments

Comments
 (0)