Skip to content

Commit 3b5e668

Browse files
committed
fix: resolve PHPStan array key type errors
- Fix invalid array key type in CollectionDataTable::revertIndexColumn() - Fix invalid array key type in DataTableAbstract::with() - Fix instanceof and array key type issues in DataTables::make() - Fix invalid array key type in DataProcessor::process() - Fix invalid array key types in Helper::includeInArray() All fixes ensure array keys are properly typed as int|string to satisfy PHPStan's strict type checking.
1 parent 79c601a commit 3b5e668

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

src/CollectionDataTable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ private function revertIndexColumn($mDataSupport): void
185185
{
186186
if ($this->columnDef['index']) {
187187
$indexColumn = config('datatables.index_column', 'DT_RowIndex');
188-
$index = $mDataSupport ? $indexColumn : 0;
188+
/** @var int|string $index */
189+
$index = $mDataSupport ? (is_int($indexColumn) || is_string($indexColumn) ? $indexColumn : 'DT_RowIndex') : 0;
189190
$start = $this->request->start();
190191

191192
$this->collection->transform(function ($data) use ($index, &$start) {

src/DataTableAbstract.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,9 @@ public function with(mixed $key, mixed $value = ''): static
433433
if (is_array($key)) {
434434
$this->appends = $key;
435435
} else {
436-
$this->appends[$key] = value($value);
436+
/** @var int|string $arrayKey */
437+
$arrayKey = is_int($key) || is_string($key) ? $key : (string) $key;
438+
$this->appends[$arrayKey] = value($value);
437439
}
438440

439441
return $this;

src/DataTables.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ public static function make($source)
4747

4848
$args = func_get_args();
4949
foreach ($builders as $class => $engine) {
50-
if ($source instanceof $class) {
51-
$callback = [$engines[$engine], 'create'];
50+
if (is_string($class) && $source instanceof $class) {
51+
/** @var int|string $engineKey */
52+
$engineKey = is_int($engine) || is_string($engine) ? $engine : (string) $engine;
53+
$callback = [$engines[$engineKey], 'create'];
5254

5355
if (is_callable($callback)) {
5456
/** @var \Yajra\DataTables\DataTableAbstract $instance */

src/Processors/DataProcessor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public function process($object = false): array
6868
{
6969
$this->output = [];
7070
$indexColumn = config('datatables.index_column', 'DT_RowIndex');
71+
/** @var int|string $indexKey */
72+
$indexKey = is_int($indexColumn) || is_string($indexColumn) ? $indexColumn : 'DT_RowIndex';
7173

7274
foreach ($this->results as $row) {
7375
$data = Helper::convertToArray($row, ['hidden' => $this->makeHidden, 'visible' => $this->makeVisible, 'ignore_getters' => $this->ignoreGetters]);
@@ -78,7 +80,7 @@ public function process($object = false): array
7880
$value = $this->removeExcessColumns($value);
7981

8082
if ($this->includeIndex) {
81-
$value[$indexColumn] = ++$this->start;
83+
$value[$indexKey] = ++$this->start;
8284
}
8385

8486
$this->output[] = $object ? $value : $this->flatten($value);

src/Utilities/Helper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ class Helper
1818
*/
1919
public static function includeInArray(array $item, array $array): array
2020
{
21+
/** @var int|string $itemName */
22+
$itemName = is_int($item['name']) || is_string($item['name']) ? $item['name'] : (string) $item['name'];
23+
2124
if (self::isItemOrderInvalid($item, $array)) {
22-
return array_merge($array, [$item['name'] => $item['content']]);
25+
return array_merge($array, [$itemName => $item['content']]);
2326
}
2427

2528
$count = 0;
@@ -36,7 +39,7 @@ public static function includeInArray(array $item, array $array): array
3639
$count++;
3740
}
3841

39-
return array_merge($first, [$item['name'] => $item['content']], $last);
42+
return array_merge($first, [$itemName => $item['content']], $last);
4043
}
4144

4245
/**

0 commit comments

Comments
 (0)