Skip to content

Commit b8104ae

Browse files
committed
chore: type hint and doc blocks
1 parent c018737 commit b8104ae

File tree

6 files changed

+43
-92
lines changed

6 files changed

+43
-92
lines changed

src/CollectionDataTable.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,9 @@ public function paging(): void
138138
/**
139139
* Organizes works.
140140
*
141-
* @param bool $mDataSupport
142-
*
143141
* @throws \Exception
144142
*/
145-
public function make($mDataSupport = true): JsonResponse
143+
public function make(bool $mDataSupport = true): JsonResponse
146144
{
147145
try {
148146
$this->totalRecords = $this->totalCount();

src/Contracts/DataTable.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ public function totalCount(): int;
2828
* Set auto filter off and run your own filter.
2929
* Overrides global search.
3030
*
31-
* @param bool $globalSearch
3231
* @return static
3332
*/
34-
public function filter(callable $callback, $globalSearch = false): self;
33+
public function filter(callable $callback, bool $globalSearch = false): self;
3534

3635
/**
3736
* Perform global search.
@@ -55,8 +54,6 @@ public function ordering(): void;
5554

5655
/**
5756
* Organizes works.
58-
*
59-
* @param bool $mDataSupport
6057
*/
61-
public function make($mDataSupport = true): JsonResponse;
58+
public function make(bool $mDataSupport = true): JsonResponse;
6259
}

src/DataTableAbstract.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,9 @@ abstract protected function defaultOrdering(): void;
642642
* Set auto filter off and run your own filter.
643643
* Overrides global search.
644644
*
645-
* @param bool $globalSearch
646645
* @return $this
647646
*/
648-
public function filter(callable $callback, $globalSearch = false): static
647+
public function filter(callable $callback, bool $globalSearch = false): self
649648
{
650649
$this->autoFilter = $globalSearch;
651650
$this->filterCallback = $callback;
@@ -696,6 +695,21 @@ public function toArray(): array
696695
return (array) $this->make()->getData(true);
697696
}
698697

698+
/**
699+
* Count total items.
700+
*/
701+
public function totalCount(): int
702+
{
703+
return $this->totalRecords ??= $this->count();
704+
}
705+
706+
public function editOnlySelectedColumns(): static
707+
{
708+
$this->editOnlySelectedColumns = true;
709+
710+
return $this;
711+
}
712+
699713
/**
700714
* Perform necessary filters.
701715
*/
@@ -758,14 +772,6 @@ protected function searchPanesSearch(): void
758772
// Add support for search pane.
759773
}
760774

761-
/**
762-
* Count total items.
763-
*/
764-
public function totalCount(): int
765-
{
766-
return $this->totalRecords ??= $this->count();
767-
}
768-
769775
/**
770776
* Count filtered items.
771777
*/
@@ -872,11 +878,9 @@ protected function showDebugger(array $output): array
872878
/**
873879
* Return an error json response.
874880
*
875-
* @return \Illuminate\Http\JsonResponse
876-
*
877881
* @throws \Yajra\DataTables\Exceptions\Exception|\Exception
878882
*/
879-
protected function errorResponse(\Exception $exception)
883+
protected function errorResponse(\Exception $exception): JsonResponse
880884
{
881885
/** @var string $error */
882886
$error = $this->config->get('datatables.error');
@@ -893,7 +897,7 @@ protected function errorResponse(\Exception $exception)
893897
'recordsTotal' => $this->totalRecords,
894898
'recordsFiltered' => 0,
895899
'data' => [],
896-
'error' => $error ? __($error) : "Exception Message:\n\n".$exception->getMessage(),
900+
'error' => $error ? __($error) : 'Exception Message:'.PHP_EOL.PHP_EOL.$exception->getMessage(),
897901
]);
898902
}
899903

@@ -940,7 +944,7 @@ protected function setupKeyword(string $value): string
940944
}
941945

942946
/**
943-
* Get column name to be use for filtering and sorting.
947+
* Get column name to be used for filtering and sorting.
944948
*/
945949
protected function getColumnName(int $index, bool $wantsAlias = false): ?string
946950
{
@@ -981,11 +985,4 @@ protected function getPrimaryKeyName(): string
981985
{
982986
return 'id';
983987
}
984-
985-
public function editOnlySelectedColumns(): static
986-
{
987-
$this->editOnlySelectedColumns = true;
988-
989-
return $this;
990-
}
991988
}

src/Processors/RowProcessor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public function __construct(protected array $data, protected $row)
2020
* @param string $attribute
2121
* @param string|callable $template
2222
* @return $this
23+
*
24+
* @throws \ReflectionException
2325
*/
2426
public function rowValue($attribute, $template)
2527
{
@@ -39,6 +41,8 @@ public function rowValue($attribute, $template)
3941
*
4042
* @param string $attribute
4143
* @return $this
44+
*
45+
* @throws \ReflectionException
4246
*/
4347
public function rowData($attribute, array $template)
4448
{

src/QueryDataTable.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ class QueryDataTable extends DataTableAbstract
8080
*/
8181
protected bool $disableUserOrdering = false;
8282

83-
public function __construct(/**
84-
* Builder object.
85-
*/
86-
protected QueryBuilder $query)
83+
public function __construct(protected QueryBuilder $query)
8784
{
8885
$this->request = app('datatables.request');
8986
$this->config = app('datatables.config');
@@ -115,11 +112,9 @@ public static function canCreate($source): bool
115112
/**
116113
* Organizes works.
117114
*
118-
* @param bool $mDataSupport
119-
*
120115
* @throws \Exception
121116
*/
122-
public function make($mDataSupport = true): JsonResponse
117+
public function make(bool $mDataSupport = true): JsonResponse
123118
{
124119
try {
125120
$results = $this->prepareQuery()->results();

src/Utilities/Helper.php

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ class Helper
1414
{
1515
/**
1616
* Places item of extra columns into results by care of their order.
17-
*
18-
* @param array $item
19-
* @param array $array
20-
* @return array
2117
*/
22-
public static function includeInArray($item, $array)
18+
public static function includeInArray(array $item, array $array): array
2319
{
2420
if (self::isItemOrderInvalid($item, $array)) {
2521
return array_merge($array, [$item['name'] => $item['content']]);
@@ -44,12 +40,8 @@ public static function includeInArray($item, $array)
4440

4541
/**
4642
* Check if item order is valid.
47-
*
48-
* @param array $item
49-
* @param array $array
50-
* @return bool
5143
*/
52-
protected static function isItemOrderInvalid($item, $array)
44+
protected static function isItemOrderInvalid(array $item, array $array): bool
5345
{
5446
return $item['order'] === false || $item['order'] >= count($array);
5547
}
@@ -122,11 +114,10 @@ public static function compileContent(mixed $content, array $data, array|object
122114
/**
123115
* Parses and compiles strings by using Blade Template System.
124116
*
125-
* @param string $str
126-
* @param array $data
127-
* @return false|string
117+
*
118+
* @throws \Throwable
128119
*/
129-
public static function compileBlade($str, $data = [])
120+
public static function compileBlade(string $str, array $data = []): false|string
130121
{
131122
if (view()->exists($str)) {
132123
/** @var view-string $str */
@@ -143,10 +134,8 @@ public static function compileBlade($str, $data = [])
143134

144135
/**
145136
* Get a mixed value of custom data and the parameters.
146-
*
147-
* @return array
148137
*/
149-
public static function getMixedValue(array $data, array|object $param)
138+
public static function getMixedValue(array $data, array|object $param): array
150139
{
151140
$casted = self::castToArray($param);
152141

@@ -175,11 +164,8 @@ public static function castToArray(array|object $param): array
175164

176165
/**
177166
* Get equivalent or method of query builder.
178-
*
179-
* @param string $method
180-
* @return string
181167
*/
182-
public static function getOrMethod($method)
168+
public static function getOrMethod(string $method): string
183169
{
184170
if (! Str::contains(Str::lower($method), 'or')) {
185171
return 'or'.ucfirst($method);
@@ -190,11 +176,8 @@ public static function getOrMethod($method)
190176

191177
/**
192178
* Converts array object values to associative array.
193-
*
194-
* @param array $filters
195-
* @return array
196179
*/
197-
public static function convertToArray(mixed $row, $filters = [])
180+
public static function convertToArray(mixed $row, array $filters = []): array
198181
{
199182
if (Arr::get($filters, 'ignore_getters') && is_object($row) && method_exists($row, 'getAttributes')) {
200183
$data = $row->getAttributes();
@@ -230,10 +213,7 @@ public static function convertToArray(mixed $row, $filters = [])
230213
return $data;
231214
}
232215

233-
/**
234-
* @return array
235-
*/
236-
public static function transform(array $data)
216+
public static function transform(array $data): array
237217
{
238218
return array_map(fn ($row) => self::transformRow($row), $data);
239219
}
@@ -242,9 +222,8 @@ public static function transform(array $data)
242222
* Transform row data into an array.
243223
*
244224
* @param array $row
245-
* @return array
246225
*/
247-
protected static function transformRow($row)
226+
protected static function transformRow($row): array
248227
{
249228
foreach ($row as $key => $value) {
250229
if ($value instanceof DateTime) {
@@ -263,10 +242,8 @@ protected static function transformRow($row)
263242

264243
/**
265244
* Build parameters depending on # of arguments passed.
266-
*
267-
* @return array
268245
*/
269-
public static function buildParameters(array $args)
246+
public static function buildParameters(array $args): array
270247
{
271248
$parameters = [];
272249

@@ -286,12 +263,8 @@ public static function buildParameters(array $args)
286263

287264
/**
288265
* Replace all pattern occurrences with keyword.
289-
*
290-
* @param string $keyword
291-
* @param string $pattern
292-
* @return array
293266
*/
294-
public static function replacePatternWithKeyword(array $subject, $keyword, $pattern = '$1')
267+
public static function replacePatternWithKeyword(array $subject, string $keyword, string $pattern = '$1'): array
295268
{
296269
$parameters = [];
297270
foreach ($subject as $param) {
@@ -307,12 +280,8 @@ public static function replacePatternWithKeyword(array $subject, $keyword, $patt
307280

308281
/**
309282
* Get column name from string.
310-
*
311-
* @param string $str
312-
* @param bool $wantsAlias
313-
* @return string
314283
*/
315-
public static function extractColumnName($str, $wantsAlias)
284+
public static function extractColumnName(string $str, bool $wantsAlias): string
316285
{
317286
$matches = explode(' as ', Str::lower($str));
318287

@@ -333,25 +302,16 @@ public static function extractColumnName($str, $wantsAlias)
333302

334303
/**
335304
* Adds % wildcards to the given string.
336-
*
337-
* @param string $str
338-
* @param bool $lowercase
339-
* @return string
340305
*/
341-
public static function wildcardLikeString($str, $lowercase = true)
306+
public static function wildcardLikeString(string $str, bool $lowercase = true): string
342307
{
343308
return static::wildcardString($str, '%', $lowercase);
344309
}
345310

346311
/**
347312
* Adds wildcards to the given string.
348-
*
349-
* @param string $str
350-
* @param string $wildcard
351-
* @param bool $lowercase
352-
* @return string
353313
*/
354-
public static function wildcardString($str, $wildcard, $lowercase = true)
314+
public static function wildcardString(string $str, string $wildcard, bool $lowercase = true): string
355315
{
356316
$wild = $wildcard;
357317
$chars = (array) preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);

0 commit comments

Comments
 (0)