Skip to content

Commit 2862b3f

Browse files
committed
chore: rector and pint
1 parent 2a704d2 commit 2862b3f

26 files changed

+225
-360
lines changed

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"laravel/pint": "^1.14",
2929
"laravel/scout": "^10.8.3",
3030
"meilisearch/meilisearch-php": "^1.6.1",
31-
"orchestra/testbench": "^9"
31+
"orchestra/testbench": "^9",
32+
"rector/rector": "^1.0"
3233
},
3334
"suggest": {
3435
"yajra/laravel-datatables-export": "Plugin for server-side exporting using livewire and queue worker.",
@@ -72,7 +73,14 @@
7273
"scripts": {
7374
"test": "./vendor/bin/phpunit",
7475
"pint": "./vendor/bin/pint",
75-
"stan": "./vendor/bin/phpstan analyse --memory-limit=2G --ansi --no-progress --no-interaction --configuration=phpstan.neon.dist"
76+
"rector": "./vendor/bin/rector",
77+
"stan": "./vendor/bin/phpstan analyse --memory-limit=2G --ansi --no-progress --no-interaction --configuration=phpstan.neon.dist",
78+
"pr": [
79+
"@rector",
80+
"@pint",
81+
"@stan",
82+
"@test"
83+
]
7684
},
7785
"minimum-stability": "dev",
7886
"prefer-stable": true,

rector.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
6+
use Rector\Config\RectorConfig;
7+
use Rector\Set\ValueObject\LevelSetList;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->paths([
11+
__DIR__.'/src',
12+
__DIR__.'/tests',
13+
]);
14+
15+
// register a single rule
16+
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
17+
18+
// define sets of rules
19+
$rectorConfig->sets([
20+
LevelSetList::UP_TO_PHP_82,
21+
]);
22+
};

src/CollectionDataTable.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@
1313

1414
class CollectionDataTable extends DataTableAbstract
1515
{
16-
/**
17-
* Collection object.
18-
*
19-
* @var \Illuminate\Support\Collection<array-key, array>
20-
*/
21-
public Collection $collection;
22-
2316
/**
2417
* Collection object.
2518
*
@@ -37,21 +30,18 @@ class CollectionDataTable extends DataTableAbstract
3730
*
3831
* @param \Illuminate\Support\Collection<array-key, array> $collection
3932
*/
40-
public function __construct(Collection $collection)
33+
public function __construct(public Collection $collection)
4134
{
4235
$this->request = app('datatables.request');
4336
$this->config = app('datatables.config');
44-
$this->collection = $collection;
45-
$this->original = $collection;
46-
$this->columns = array_keys($this->serialize($collection->first()));
37+
$this->original = $this->collection;
38+
$this->columns = array_keys($this->serialize($this->collection->first()));
4739
}
4840

4941
/**
5042
* Serialize collection.
51-
*
52-
* @param mixed $collection
5343
*/
54-
protected function serialize($collection): array
44+
protected function serialize(mixed $collection): array
5545
{
5646
return $collection instanceof Arrayable ? $collection->toArray() : (array) $collection;
5747
}
@@ -257,9 +247,7 @@ protected function defaultOrdering(): void
257247
$sorter = $this->getSorter($criteria);
258248

259249
$this->collection = $this->collection
260-
->map(function ($data) {
261-
return Arr::dot($data);
262-
})
250+
->map(fn ($data) => Arr::dot($data))
263251
->sort($sorter)
264252
->map(function ($data) {
265253
foreach ($data as $key => $value) {

src/Contracts/Formatter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
interface Formatter
66
{
77
/**
8-
* @param mixed $value
98
* @param array|\Illuminate\Database\Eloquent\Model|object $row
109
* @return string
1110
*/
12-
public function format($value, $row);
11+
public function format(mixed $value, $row);
1312
}

src/DataTableAbstract.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,19 @@ abstract class DataTableAbstract implements DataTable
120120
/**
121121
* Can the DataTable engine be created with these parameters.
122122
*
123-
* @param mixed $source
124123
* @return bool
125124
*/
126-
public static function canCreate($source)
125+
public static function canCreate(mixed $source)
127126
{
128127
return false;
129128
}
130129

131130
/**
132131
* Factory method, create and return an instance for the DataTable engine.
133132
*
134-
* @param mixed $source
135133
* @return static
136134
*/
137-
public static function create($source)
135+
public static function create(mixed $source)
138136
{
139137
return new static($source);
140138
}
@@ -162,9 +160,7 @@ public function formatColumn($columns, $formatter): static
162160
foreach ((array) $columns as $column) {
163161
$this->addColumn(
164162
$column.'_formatted',
165-
function ($row) use ($column, $formatter) {
166-
return $formatter(data_get($row, $column), $row);
167-
}
163+
fn ($row) => $formatter(data_get($row, $column), $row)
168164
);
169165
}
170166

@@ -174,9 +170,7 @@ function ($row) use ($column, $formatter) {
174170
foreach ((array) $columns as $column) {
175171
$this->addColumn(
176172
$column.'_formatted',
177-
function ($row) use ($column) {
178-
return data_get($row, $column);
179-
}
173+
fn ($row) => data_get($row, $column)
180174
);
181175
}
182176

@@ -425,11 +419,9 @@ public function addRowAttr($key, $value): static
425419
/**
426420
* Append data on json response.
427421
*
428-
* @param mixed $key
429-
* @param mixed $value
430422
* @return $this
431423
*/
432-
public function with($key, $value = ''): static
424+
public function with(mixed $key, mixed $value = ''): static
433425
{
434426
if (is_array($key)) {
435427
$this->appends = $key;
@@ -680,10 +672,9 @@ public function toJson($options = 0)
680672
* Add a search pane options on response.
681673
*
682674
* @param string $column
683-
* @param mixed $options
684675
* @return $this
685676
*/
686-
public function searchPane($column, $options, ?callable $builder = null): static
677+
public function searchPane($column, mixed $options, ?callable $builder = null): static
687678
{
688679
$options = value($options);
689680

@@ -748,9 +739,7 @@ public function filtering(): void
748739
protected function smartGlobalSearch($keyword): void
749740
{
750741
collect(explode(' ', $keyword))
751-
->reject(function ($keyword) {
752-
return trim($keyword) === '';
753-
})
742+
->reject(fn ($keyword) => trim((string) $keyword) === '')
754743
->each(function ($keyword) {
755744
$this->globalSearch($keyword);
756745
});

src/DataTables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function make($source)
7474
}
7575
}
7676

77-
throw new Exception('No available engine for '.get_class($source));
77+
throw new Exception('No available engine for '.$source::class);
7878
}
7979

8080
/**

src/DataTablesServiceProvider.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@ public function register()
2323
$this->setupAssets();
2424

2525
$this->app->alias('datatables', DataTables::class);
26-
$this->app->singleton('datatables', function () {
27-
return new DataTables;
28-
});
26+
$this->app->singleton('datatables', fn () => new DataTables);
2927

30-
$this->app->singleton('datatables.request', function () {
31-
return new Request;
32-
});
28+
$this->app->singleton('datatables.request', fn () => new Request);
3329

3430
$this->app->singleton('datatables.config', Config::class);
3531
}

src/EloquentDataTable.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ public function addColumns(array $names, $order = false)
5656
$name = $attribute;
5757
}
5858

59-
$this->addColumn($name, function ($model) use ($attribute) {
60-
return $model->getAttribute($attribute);
61-
}, is_int($order) ? $order++ : $order);
59+
$this->addColumn($name, fn ($model) => $model->getAttribute($attribute), is_int($order) ? $order++ : $order);
6260
}
6361

6462
return $this;
@@ -237,7 +235,7 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
237235
break;
238236

239237
default:
240-
throw new Exception('Relation '.get_class($model).' is not yet supported.');
238+
throw new Exception('Relation '.$model::class.' is not yet supported.');
241239
}
242240
$this->performJoin($table, $foreign, $other);
243241
$lastQuery = $model->getQuery();

src/Processors/DataProcessor.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
class DataProcessor
1111
{
12-
protected int $start;
13-
1412
protected array $output = [];
1513

1614
/**
@@ -23,8 +21,6 @@ class DataProcessor
2321
*/
2422
protected array $editColumns = [];
2523

26-
protected array $templates = [];
27-
2824
protected array $rawColumns = [];
2925

3026
/**
@@ -45,18 +41,12 @@ class DataProcessor
4541
*/
4642
protected mixed $escapeColumns = [];
4743

48-
protected iterable $results;
49-
5044
protected bool $includeIndex = false;
5145

5246
protected bool $ignoreGetters = false;
5347

54-
/**
55-
* @param iterable $results
56-
*/
57-
public function __construct($results, array $columnDef, array $templates, int $start = 0)
48+
public function __construct(protected iterable $results, array $columnDef, protected array $templates, protected int $start = 0)
5849
{
59-
$this->results = $results;
6050
$this->appendColumns = $columnDef['append'] ?? [];
6151
$this->editColumns = $columnDef['edit'] ?? [];
6252
$this->excessColumns = $columnDef['excess'] ?? [];
@@ -67,8 +57,6 @@ public function __construct($results, array $columnDef, array $templates, int $s
6757
$this->makeHidden = $columnDef['hidden'] ?? [];
6858
$this->makeVisible = $columnDef['visible'] ?? [];
6959
$this->ignoreGetters = $columnDef['ignore_getters'] ?? false;
70-
$this->templates = $templates;
71-
$this->start = $start;
7260
}
7361

7462
/**

0 commit comments

Comments
 (0)