Skip to content

Commit b71189a

Browse files
committed
PHPStan level 9
1 parent dd786ec commit b71189a

File tree

4 files changed

+52
-31
lines changed

4 files changed

+52
-31
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ parameters:
66
paths:
77
- src
88

9-
level: 8
9+
level: max
1010

1111
ignoreErrors:
1212
- '#Unsafe usage of new static\(\).#'

src/Exports/DataTablesCollectionExport.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function collection(): Collection
3737
*/
3838
public function headings(): array
3939
{
40+
/** @var array $first */
4041
$first = $this->collection->first();
4142
if ($first) {
4243
return array_keys($first);

src/Generators/DataTablesMakeCommand.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,10 @@ protected function replaceTableId(string &$stub): static
153153
*/
154154
protected function replaceDOM(string &$stub): static
155155
{
156-
$stub = str_replace(
157-
'DummyDOM',
158-
$this->option('dom') ?: config('datatables-buttons.generator.dom', 'Bfrtip'),
159-
$stub
160-
);
156+
/** @var string $dom */
157+
$dom = $this->option('dom') ?: config('datatables-buttons.generator.dom', 'Bfrtip');
158+
159+
$stub = str_replace('DummyDOM', $dom, $stub);
161160

162161
return $this;
163162
}
@@ -191,12 +190,10 @@ protected function getButtons(): string
191190
return $this->parseButtons($buttons);
192191
}
193192

194-
return $this->parseButtons(
195-
config(
196-
'datatables-buttons.generator.buttons',
197-
'create,export,print,reset,reload'
198-
)
199-
);
193+
/** @var string $buttons */
194+
$buttons = config('datatables-buttons.generator.buttons', 'create,export,print,reset,reload');
195+
196+
return $this->parseButtons($buttons);
200197
}
201198

202199
/**
@@ -264,6 +261,7 @@ protected function getColumns(): string
264261
return $this->parseColumns($columns);
265262
}
266263

264+
/** @var string $columns */
267265
$columns = config('datatables-buttons.generator.columns', 'id,add your columns,created_at,updated_at');
268266

269267
return $this->parseColumns($columns);

src/Services/DataTable.php

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Yajra\DataTables\Services;
44

5+
use Closure;
56
use Generator;
67
use Illuminate\Contracts\Support\Renderable;
78
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
@@ -223,6 +224,7 @@ public function ajax(): JsonResponse
223224
{
224225
$query = null;
225226
if (method_exists($this, 'query')) {
227+
/** @var EloquentBuilder|QueryBuilder $query */
226228
$query = app()->call([$this, 'query']);
227229
$query = $this->applyScopes($query);
228230
}
@@ -332,7 +334,7 @@ public function builder(): Builder
332334
}
333335

334336
if (method_exists($this, 'htmlBuilder')) {
335-
return $this->htmlBuilder = app()->call([$this, 'htmlBuilder']);
337+
return $this->htmlBuilder = $this->htmlBuilder();
336338
}
337339

338340
return $this->htmlBuilder = app('datatables.html');
@@ -366,7 +368,10 @@ protected function getAjaxResponseData(): array
366368
'length' => -1,
367369
]);
368370

371+
/** @var JsonResponse $response */
369372
$response = app()->call([$this, 'ajax']);
373+
374+
/** @var array{data: array} $data */
370375
$data = $response->getData(true);
371376

372377
return $data['data'];
@@ -434,18 +439,24 @@ public function excel()
434439
{
435440
set_time_limit(3600);
436441

437-
$ext = '.'.strtolower($this->excelWriter);
438-
$callback = $this->fastExcel ?
439-
($this->fastExcelCallback ? $this->fastExcelCallback() : null)
440-
: $this->excelWriter;
442+
$path = $this->getFilename().'.'.strtolower($this->excelWriter);
443+
444+
$excelFile = $this->buildExcelFile();
445+
446+
if ($excelFile instanceof FastExcel) {
447+
$callback = $this->fastExcelCallback ? $this->fastExcelCallback() : null;
448+
449+
return $excelFile->download($path, $callback);
450+
}
441451

442-
return $this->buildExcelFile()->download($this->getFilename().$ext, $callback);
452+
// @phpstan-ignore-next-line
453+
return $excelFile->download($path, $this->excelWriter);
443454
}
444455

445456
/**
446457
* Build Excel file and prepare for export.
447458
*
448-
* @return mixed|\Rap2hpoutre\FastExcel\FastExcel
459+
* @return mixed|FastExcel
449460
* @throws \Exception
450461
*/
451462
protected function buildExcelFile()
@@ -517,7 +528,7 @@ protected function getDataForExport(): array
517528
/**
518529
* Get export columns definition.
519530
*
520-
* @return Collection
531+
* @return Collection<int, Column>
521532
*/
522533
protected function exportColumns(): Collection
523534
{
@@ -552,24 +563,30 @@ private function toColumnsCollection(array $columns): Collection
552563
/**
553564
* Export results to CSV file.
554565
*
555-
* @return string|\Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\StreamedResponse
566+
* @return string|\Symfony\Component\HttpFoundation\StreamedResponse
556567
* @throws \Exception
557568
*/
558569
public function csv()
559570
{
560571
set_time_limit(3600);
561-
$ext = '.'.strtolower($this->csvWriter);
562-
$callback = $this->fastExcel ?
563-
($this->fastExcelCallback ? $this->fastExcelCallback() : null)
564-
: $this->csvWriter;
572+
$path = $this->getFilename().'.'.strtolower($this->excelWriter);
573+
574+
$excelFile = $this->buildExcelFile();
565575

566-
return $this->buildExcelFile()->download($this->getFilename().$ext, $callback);
576+
if ($excelFile instanceof FastExcel) {
577+
$callback = $this->fastExcelCallback ? $this->fastExcelCallback() : null;
578+
579+
return $excelFile->download($path, $callback);
580+
}
581+
582+
// @phpstan-ignore-next-line
583+
return $this->buildExcelFile()->download($path, $this->csvWriter);
567584
}
568585

569586
/**
570587
* Export results to PDF file.
571588
*
572-
* @return \Illuminate\Http\Response|string|\Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\StreamedResponse
589+
* @return \Illuminate\Http\Response|string|\Symfony\Component\HttpFoundation\StreamedResponse
573590
* @throws \Box\Spout\Common\Exception\IOException
574591
* @throws \Box\Spout\Common\Exception\InvalidArgumentException
575592
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
@@ -582,6 +599,7 @@ public function pdf()
582599
return $this->snappyPdf();
583600
}
584601

602+
// @phpstan-ignore-next-line
585603
return $this->buildExcelFile()->download($this->getFilename().'.pdf', $this->pdfWriter);
586604
}
587605

@@ -594,7 +612,9 @@ public function snappyPdf(): Response
594612
{
595613
/** @var \Barryvdh\Snappy\PdfWrapper $snappy */
596614
$snappy = app('snappy.pdf.wrapper');
597-
$options = config('datatables-buttons.snappy.options');
615+
$options = (array) config('datatables-buttons.snappy.options');
616+
617+
/** @var string $orientation */
598618
$orientation = config('datatables-buttons.snappy.orientation');
599619

600620
$snappy->setOptions($options)->setOrientation($orientation);
@@ -721,15 +741,16 @@ protected function convertToLazyCollection(array|Collection $collection): LazyCo
721741
/**
722742
* @return \Closure
723743
*/
724-
public function fastExcelCallback(): \Closure
744+
public function fastExcelCallback(): Closure
725745
{
726746
return function ($row) {
727747
$mapped = [];
728-
foreach ($this->exportColumns() as $column) {
748+
749+
$this->exportColumns()->each(function (Column $column) use (&$mapped, $row) {
729750
if ($column['exportable']) {
730751
$mapped[$column['title']] = $row[$column['data']];
731752
}
732-
}
753+
});
733754

734755
return $mapped;
735756
};
@@ -742,6 +763,7 @@ protected function buildFastExcelFile(): FastExcel
742763
{
743764
$query = null;
744765
if (method_exists($this, 'query')) {
766+
/** @var EloquentBuilder|QueryBuilder $query */
745767
$query = app()->call([$this, 'query']);
746768
$query = $this->applyScopes($query);
747769
}

0 commit comments

Comments
 (0)