Skip to content

Commit

Permalink
Fix @phpstan-ignore-next-line
Browse files Browse the repository at this point in the history
  • Loading branch information
yajra committed May 8, 2022
1 parent c3b9896 commit 429728e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 19 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"require-dev": {
"nunomaduro/larastan": "^2.1",
"orchestra/testbench": "^7.3"
"orchestra/testbench": "^7.3",
"yajra/laravel-datatables-html": "^9.0"
},
"suggest": {
"yajra/laravel-datatables-buttons": "Plugin for server-side exporting of dataTables.",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ parameters:
level: max

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

excludePaths:
- src/helper.php
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface Formatter
{
/**
* @param mixed $value
* @param mixed $row
* @param array|\Illuminate\Database\Eloquent\Model|object $row
* @return string
*/
public function format($value, $row);
Expand Down
3 changes: 1 addition & 2 deletions src/DataTableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,10 @@ public static function canCreate($source)
* Factory method, create and return an instance for the DataTable engine.
*
* @param mixed $source
* @return $this
* @return static
*/
public static function create($source)
{
/** @phpstan-ignore-next-line */
return new static($source);
}

Expand Down
23 changes: 12 additions & 11 deletions src/DataTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Traits\Macroable;
use Yajra\DataTables\Exceptions\Exception;
use Yajra\DataTables\Html\Builder;

class DataTables
{
Expand All @@ -21,11 +22,9 @@ class DataTables
/**
* HTML builder instance.
*
* @phpstan-ignore-next-line
*
* @var \Yajra\DataTables\Html\Builder|null
*/
protected $html = null;
protected ?Builder $html = null;

/**
* Make a DataTable instance from source.
Expand Down Expand Up @@ -60,8 +59,10 @@ public static function make($source)
$callback = [$engines[$engine], 'create'];

if (is_callable($callback)) {
// @phpstan-ignore-next-line
return call_user_func_array($callback, $args);
/** @var \Yajra\DataTables\DataTableAbstract $instance */
$instance = call_user_func_array($callback, $args);

return $instance;
}
}
}
Expand All @@ -72,8 +73,10 @@ public static function make($source)
$create = [$engine, 'create'];

if (is_callable($create)) {
// @phpstan-ignore-next-line
return call_user_func_array($create, $args);
/** @var \Yajra\DataTables\DataTableAbstract $instance */
$instance = call_user_func_array($create, $args);

return $instance;
}
}
}
Expand Down Expand Up @@ -137,15 +140,13 @@ public function collection($collection): CollectionDataTable
/**
* Get html builder instance.
*
* @phpstan-ignore-next-line
*
* @return \Yajra\DataTables\Html\Builder
*
* @throws Exception
* @throws \Yajra\DataTables\Exceptions\Exception
*/
public function getHtmlBuilder()
{
if (! class_exists('\Yajra\DataTables\Html\Builder')) {
if (! class_exists(Builder::class)) {
throw new Exception('Please install yajra/laravel-datatables-html to be able to use this function.');
}

Expand Down
63 changes: 59 additions & 4 deletions src/Processors/DataProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,73 @@

class DataProcessor
{
/**
* @var int
*/
protected int $start;
/**
* @var array
*/
protected array $output = [];

/**
* @var array<array-key, array{name: string, content: mixed}>
*/
protected array $appendColumns = [];

/**
* @var array<array-key, array{name: string, content: mixed}>
*/
protected array $editColumns = [];

/**
* @var array
*/
protected array $templates = [];

/**
* @var array
*/
protected array $rawColumns = [];

/**
* @var array|string[]
*/
protected array $exceptions = ['DT_RowId', 'DT_RowClass', 'DT_RowData', 'DT_RowAttr'];

/**
* @var array
*/
protected array $onlyColumns = [];

/**
* @var array
*/
protected array $makeHidden = [];

/**
* @var array
*/
protected array $makeVisible = [];

/**
* @var array
*/
protected array $excessColumns = [];

/**
* @var string|array
*/
protected mixed $escapeColumns = [];

/**
* @var iterable
*/
protected iterable $results;

/**
* @var bool
*/
protected bool $includeIndex = false;

/**
Expand All @@ -38,7 +92,7 @@ public function __construct($results, array $columnDef, array $templates, int $s
$this->excessColumns = $columnDef['excess'] ?? [];
$this->onlyColumns = $columnDef['only'] ?? [];
$this->escapeColumns = $columnDef['escape'] ?? [];
$this->includeIndex = $columnDef['index'] ?? [];
$this->includeIndex = $columnDef['index'] ?? false;
$this->rawColumns = $columnDef['raw'] ?? [];
$this->makeHidden = $columnDef['hidden'] ?? [];
$this->makeVisible = $columnDef['visible'] ?? [];
Expand Down Expand Up @@ -79,7 +133,7 @@ public function process($object = false): array
* Process add columns.
*
* @param array $data
* @param array|object $row
* @param array|object|\Illuminate\Database\Eloquent\Model $row
* @return array
*/
protected function addColumns(array $data, $row): array
Expand All @@ -89,8 +143,9 @@ protected function addColumns(array $data, $row): array
if ($content instanceof Formatter) {
$column = str_replace('_formatted', '', $value['name']);

/** @phpstan-ignore-next-line */
$value['content'] = $content->format($data[$column], $row);
if (isset($data[$column])) {
$value['content'] = $content->format($data[$column], $row);
}
} else {
$value['content'] = Helper::compileContent($content, $data, $row);
}
Expand Down

0 comments on commit 429728e

Please sign in to comment.