Skip to content

Commit 6b62401

Browse files
committed
WIP: max level fixes
1 parent 18a55e3 commit 6b62401

13 files changed

+156
-108
lines changed

phpstan.neon.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ parameters:
66
paths:
77
- src
88

9-
level: 8
9+
level: max
1010

1111
ignoreErrors:
12+
- '#Cannot cast mixed to int.#'
13+
- '#Parameter \#1 \$string of function strtolower expects string, mixed given.#'
14+
- '#Parameter \#1 \$format of function sprintf expects string, mixed given.#'
1215

1316
excludePaths:
1417
- 'src/ApiResourceDataTable.php'

src/CollectionDataTable.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CollectionDataTable extends DataTableAbstract
1313
/**
1414
* Collection object.
1515
*
16-
* @var \Illuminate\Support\Collection
16+
* @var \Illuminate\Support\Collection<mixed>
1717
*/
1818
public Collection $collection;
1919

@@ -75,9 +75,9 @@ public function __construct(Collection $collection)
7575
* Serialize collection.
7676
*
7777
* @param mixed $collection
78-
* @return mixed|null
78+
* @return array
7979
*/
80-
protected function serialize($collection)
80+
protected function serialize($collection): array
8181
{
8282
return $collection instanceof Arrayable ? $collection->toArray() : (array) $collection;
8383
}
@@ -102,6 +102,7 @@ public function count(): int
102102
*/
103103
public function columnSearch()
104104
{
105+
/** @var array $columns */
105106
$columns = $this->request->get('columns', []);
106107
for ($i = 0, $c = count($columns); $i < $c; $i++) {
107108
$column = $this->getColumnName($i);
@@ -119,6 +120,7 @@ public function columnSearch()
119120
function ($row) use ($column, $keyword, $regex) {
120121
$data = $this->serialize($row);
121122

123+
/** @var string $value */
122124
$value = Arr::get($data, $column);
123125

124126
if ($this->config->isCaseInsensitive()) {
@@ -146,10 +148,10 @@ function ($row) use ($column, $keyword, $regex) {
146148
*/
147149
public function paging()
148150
{
149-
$this->collection = $this->collection->slice(
150-
$this->request->input('start') - $this->offset,
151-
(int) $this->request->input('length') > 0 ? $this->request->input('length') : 10
152-
);
151+
$offset = (int) $this->request->input('start') - $this->offset;
152+
$length = (int) $this->request->input('length') > 0 ? (int) $this->request->input('length') : 10;
153+
154+
$this->collection = $this->collection->slice($offset, $length);
153155
}
154156

155157
/**
@@ -195,7 +197,7 @@ public function totalCount()
195197
/**
196198
* Get results.
197199
*
198-
* @return mixed
200+
* @return iterable
199201
*/
200202
public function results()
201203
{
@@ -211,9 +213,12 @@ public function results()
211213
private function revertIndexColumn($mDataSupport): void
212214
{
213215
if ($this->columnDef['index']) {
214-
$index = $mDataSupport ? config('datatables.index_column', 'DT_RowIndex') : 0;
216+
$indexColumn = config('datatables.index_column', 'DT_RowIndex');
217+
$index = $mDataSupport ? $indexColumn : 0;
215218
$start = (int) $this->request->input('start');
219+
216220
$this->collection->transform(function ($data) use ($index, &$start) {
221+
// @phpstan-ignore-next-line
217222
$data[$index] = ++$start;
218223

219224
return $data;
@@ -227,7 +232,7 @@ private function revertIndexColumn($mDataSupport): void
227232
* @param string $keyword
228233
* @return void
229234
*/
230-
protected function globalSearch($keyword): void
235+
protected function globalSearch(string $keyword): void
231236
{
232237
$keyword = $this->config->isCaseInsensitive() ? Str::lower($keyword) : $keyword;
233238

@@ -238,15 +243,12 @@ protected function globalSearch($keyword): void
238243
foreach ($this->request->searchableColumnIndex() as $index) {
239244
$column = $this->getColumnName($index);
240245
$value = Arr::get($data, $column);
241-
if (! $value || is_array($value)) {
242-
if (! is_numeric($value)) {
243-
continue;
244-
}
245-
246-
$value = (string) $value;
246+
if (! is_string($value)) {
247+
continue;
248+
} else {
249+
$value = $this->config->isCaseInsensitive() ? Str::lower($value) : $value;
247250
}
248251

249-
$value = $this->config->isCaseInsensitive() ? Str::lower($value) : $value;
250252
if (Str::contains($value, $keyword)) {
251253
return true;
252254
}
@@ -267,6 +269,7 @@ protected function defaultOrdering(): void
267269

268270
$this->collection = $this->collection
269271
->map(function ($data) {
272+
// @phpstan-ignore-next-line
270273
return Arr::dot($data);
271274
})
272275
->sort($sorter)

src/Contracts/DataTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface DataTable
77
/**
88
* Get results.
99
*
10-
* @return mixed
10+
* @return iterable
1111
*/
1212
public function results();
1313

src/DataTableAbstract.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public function removeColumn(): self
262262
*/
263263
protected function getColumnsDefinition(): array
264264
{
265-
$config = $this->config->get('datatables.columns');
265+
$config = (array) $this->config->get('datatables.columns');
266266
$allowed = ['excess', 'escape', 'raw', 'blacklist', 'whitelist'];
267267

268268
return array_replace_recursive(Arr::only($config, $allowed), $this->columnDef);
@@ -302,7 +302,8 @@ public function escapeColumns($columns = '*'): self
302302
*/
303303
public function makeHidden(array $attributes = []): self
304304
{
305-
$this->columnDef['hidden'] = array_merge_recursive(Arr::get($this->columnDef, 'hidden', []), $attributes);
305+
$hidden = (array) Arr::get($this->columnDef, 'hidden', []);
306+
$this->columnDef['hidden'] = array_merge_recursive($hidden, $attributes);
306307

307308
return $this;
308309
}
@@ -315,7 +316,8 @@ public function makeHidden(array $attributes = []): self
315316
*/
316317
public function makeVisible(array $attributes = []): self
317318
{
318-
$this->columnDef['visible'] = array_merge_recursive(Arr::get($this->columnDef, 'visible', []), $attributes);
319+
$visible = (array) Arr::get($this->columnDef, 'visible', []);
320+
$this->columnDef['visible'] = array_merge_recursive($visible, $attributes);
319321

320322
return $this;
321323
}
@@ -331,6 +333,7 @@ public function makeVisible(array $attributes = []): self
331333
public function rawColumns(array $columns, $merge = false): self
332334
{
333335
if ($merge) {
336+
/** @var array[] $config */
334337
$config = $this->config->get('datatables.columns');
335338

336339
$this->columnDef['raw'] = array_merge($config['raw'], $columns);
@@ -699,7 +702,7 @@ public function searchPane($column, $options, callable $builder = null): self
699702
*/
700703
public function toArray(): array
701704
{
702-
return $this->make()->getData(true);
705+
return (array) $this->make()->getData(true);
703706
}
704707

705708
/**
@@ -764,7 +767,7 @@ protected function smartGlobalSearch($keyword): void
764767
* @param string $keyword
765768
* @return void
766769
*/
767-
abstract protected function globalSearch($keyword): void;
770+
abstract protected function globalSearch(string $keyword): void;
768771

769772
/**
770773
* Perform search using search pane values.
@@ -801,8 +804,8 @@ protected function paginate(): void
801804
/**
802805
* Transform output.
803806
*
804-
* @param mixed $results
805-
* @param mixed $processed
807+
* @param iterable $results
808+
* @param array $processed
806809
* @return array
807810
*/
808811
protected function transform($results, $processed): array
@@ -821,17 +824,18 @@ protected function transform($results, $processed): array
821824
/**
822825
* Get processed data.
823826
*
824-
* @param mixed $results
827+
* @param iterable $results
825828
* @param bool $object
826829
* @return array
827830
*/
828831
protected function processResults($results, $object = false): array
829832
{
833+
$start = (int) $this->request->input('start');
830834
$processor = new DataProcessor(
831835
$results,
832836
$this->getColumnsDefinition(),
833837
$this->templates,
834-
$this->request->input('start') ?? 0
838+
$start
835839
);
836840

837841
return $processor->process($object);
@@ -860,11 +864,14 @@ protected function render(array $data): JsonResponse
860864
$output['searchPanes']['options'][$column] = $searchPane['options'];
861865
}
862866

867+
$headers = (array) $this->config->get('datatables.json.header', []);
868+
$options = (int) $this->config->get('datatables.json.options', 0);
869+
863870
return new JsonResponse(
864871
$output,
865872
200,
866-
$this->config->get('datatables.json.header', []),
867-
$this->config->get('datatables.json.options', 0)
873+
$headers,
874+
$options
868875
);
869876
}
870877

@@ -902,6 +909,7 @@ protected function showDebugger(array $output): array
902909
*/
903910
protected function errorResponse(\Exception $exception)
904911
{
912+
/** @var string $error */
905913
$error = $this->config->get('datatables.error');
906914
$debug = $this->config->get('app.debug');
907915

src/DataTables.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class DataTables
3131
* Make a DataTable instance from source.
3232
* Alias of make for backward compatibility.
3333
*
34-
* @param mixed $source
35-
* @return mixed
34+
* @param object $source
35+
* @return DataTableAbstract
3636
*
3737
* @throws \Exception
3838
*/
@@ -44,22 +44,23 @@ public static function of($source)
4444
/**
4545
* Make a DataTable instance from source.
4646
*
47-
* @param mixed $source
48-
* @return mixed
47+
* @param object $source
48+
* @return DataTableAbstract
4949
*
5050
* @throws \Yajra\DataTables\Exceptions\Exception
5151
*/
5252
public static function make($source)
5353
{
54-
$engines = config('datatables.engines');
55-
$builders = config('datatables.builders');
54+
$engines = (array) config('datatables.engines');
55+
$builders = (array) config('datatables.builders');
5656

5757
$args = func_get_args();
5858
foreach ($builders as $class => $engine) {
5959
if ($source instanceof $class) {
6060
$callback = [$engines[$engine], 'create'];
6161

6262
if (is_callable($callback)) {
63+
// @phpstan-ignore-next-line
6364
return call_user_func_array($callback, $args);
6465
}
6566
}
@@ -71,6 +72,7 @@ public static function make($source)
7172
$create = [$engines[$engine], 'create'];
7273

7374
if (is_callable($create)) {
75+
// @phpstan-ignore-next-line
7476
return call_user_func_array($create, $args);
7577
}
7678
}
@@ -135,7 +137,7 @@ public function collection($collection): CollectionDataTable
135137
/**
136138
* DataTables using Collection.
137139
*
138-
* @param mixed $resource
140+
* @param array|\Illuminate\Support\Collection $resource
139141
* @return ApiResourceDataTable
140142
*/
141143
public function resource($resource): ApiResourceDataTable

src/EloquentDataTable.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Yajra\DataTables;
44

55
use Illuminate\Contracts\Database\Eloquent\Builder as EloquentBuilder;
6+
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Database\Eloquent\Relations\BelongsTo;
78
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
89
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
@@ -18,11 +19,22 @@ class EloquentDataTable extends QueryDataTable
1819
/**
1920
* EloquentEngine constructor.
2021
*
21-
* @param mixed $model
22+
* @param \Illuminate\Database\Eloquent\Model|EloquentBuilder $model
23+
* @throws \Yajra\DataTables\Exceptions\Exception
2224
*/
2325
public function __construct($model)
2426
{
25-
$builder = $model instanceof EloquentBuilder ? $model : $model->getQuery();
27+
switch ($model) {
28+
case $model instanceof Model:
29+
$builder = $model->newQuery();
30+
break;
31+
case $model instanceof EloquentBuilder:
32+
$builder = $model;
33+
break;
34+
default:
35+
throw new Exception('Invalid model type. Must be an instance of Eloquent Model or Eloquent Builder.');
36+
}
37+
2638
parent::__construct($builder->getQuery());
2739

2840
$this->query = $builder;
@@ -138,7 +150,7 @@ protected function isMorphRelation($relation)
138150
*
139151
* @throws \Yajra\DataTables\Exceptions\Exception
140152
*/
141-
protected function resolveRelationColumn($column)
153+
protected function resolveRelationColumn(string $column): string
142154
{
143155
$parts = explode('.', $column);
144156
$columnName = array_pop($parts);

0 commit comments

Comments
 (0)