diff --git a/src/Column/ActionColumnRenderer.php b/src/Column/ActionColumnRenderer.php index 5e9df92a4..8e054de22 100644 --- a/src/Column/ActionColumnRenderer.php +++ b/src/Column/ActionColumnRenderer.php @@ -5,14 +5,18 @@ namespace Yiisoft\Yii\DataView\Column; use Closure; -use InvalidArgumentException; use Yiisoft\Html\Html; use Yiisoft\Yii\DataView\Column\Base\Cell; use Yiisoft\Yii\DataView\Column\Base\GlobalContext; use Yiisoft\Yii\DataView\Column\Base\DataContext; use Yiisoft\Yii\DataView\Column\Base\HeaderContext; +use function is_bool; +use function is_callable; + /** + * @implements ColumnRendererInterface + * * @psalm-import-type UrlCreator from ActionColumn * @psalm-import-type ButtonRenderer from ActionColumn */ @@ -53,13 +57,11 @@ public function __construct( public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell { - $this->checkColumn($column); return $cell->addAttributes($column->columnAttributes); } public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): Cell { - $this->checkColumn($column); return $cell ->content($column->header ?? $context->translate('Actions')) ->addAttributes($column->headerAttributes); @@ -67,8 +69,6 @@ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell { - $this->checkColumn($column); - $contentSource = $column->content; if ($contentSource !== null) { @@ -114,8 +114,6 @@ function (array $matches) use ($column, $buttons, $context): string { public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell { - $this->checkColumn($column); - if ($column->footer !== null) { $cell = $cell->content($column->footer); } @@ -238,20 +236,4 @@ private function getTemplate(ActionColumn $column, array $buttons): string return implode("\n", $tokens); } - - /** - * @psalm-assert ActionColumn $column - */ - private function checkColumn(ColumnInterface $column): void - { - if (!$column instanceof ActionColumn) { - throw new InvalidArgumentException( - sprintf( - 'Expected "%s", but "%s" given.', - self::class, - $column::class - ) - ); - } - } } diff --git a/src/Column/CheckboxColumnRenderer.php b/src/Column/CheckboxColumnRenderer.php index 5a110d669..3771129f8 100644 --- a/src/Column/CheckboxColumnRenderer.php +++ b/src/Column/CheckboxColumnRenderer.php @@ -4,7 +4,6 @@ namespace Yiisoft\Yii\DataView\Column; -use InvalidArgumentException; use Stringable; use Yiisoft\Html\Html; use Yiisoft\Yii\DataView\Column\Base\Cell; @@ -12,18 +11,18 @@ use Yiisoft\Yii\DataView\Column\Base\GlobalContext; use Yiisoft\Yii\DataView\Column\Base\HeaderContext; +/** + * @implements ColumnRendererInterface + */ final class CheckboxColumnRenderer implements ColumnRendererInterface { public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell { - $this->checkColumn($column); return $cell->addAttributes($column->columnAttributes); } public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): ?Cell { - $this->checkColumn($column); - $header = $column->header; if ($header === null) { if (!$column->multiple) { @@ -39,8 +38,6 @@ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell { - $this->checkColumn($column); - $inputAttributes = $column->inputAttributes; $name = null; $value = null; @@ -67,28 +64,10 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell { - $this->checkColumn($column); - if ($column->footer !== null) { $cell = $cell->content($column->footer); } return $cell; } - - /** - * @psalm-assert CheckboxColumn $column - */ - private function checkColumn(ColumnInterface $column): void - { - if (!$column instanceof CheckboxColumn) { - throw new InvalidArgumentException( - sprintf( - 'Expected "%s", but "%s" given.', - CheckboxColumn::class, - $column::class - ) - ); - } - } } diff --git a/src/Column/ColumnRendererInterface.php b/src/Column/ColumnRendererInterface.php index 5c5e972e0..333d3d2a1 100644 --- a/src/Column/ColumnRendererInterface.php +++ b/src/Column/ColumnRendererInterface.php @@ -11,14 +11,28 @@ /** * An interface for column renderers that implement {@see ColumnInterface}. + * + * @template TColumn as ColumnInterface */ interface ColumnRendererInterface { + /** + * @psalm-param TColumn $column + */ public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell; + /** + * @psalm-param TColumn $column + */ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): ?Cell; + /** + * @psalm-param TColumn $column + */ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell; + /** + * @psalm-param TColumn $column + */ public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell; } diff --git a/src/Column/DataColumnRenderer.php b/src/Column/DataColumnRenderer.php index 5869cf4a4..81cb72148 100644 --- a/src/Column/DataColumnRenderer.php +++ b/src/Column/DataColumnRenderer.php @@ -5,7 +5,6 @@ namespace Yiisoft\Yii\DataView\Column; use DateTimeInterface; -use InvalidArgumentException; use Psr\Container\ContainerInterface; use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Data\Reader\FilterInterface; @@ -31,6 +30,10 @@ use function is_string; /** + * @template TColumn as DataColumn + * @implements FilterableColumnRendererInterface + * @implements SortableColumnInterface + * * @psalm-import-type FilterEmptyCallable from DataColumn */ final class DataColumnRenderer implements FilterableColumnRendererInterface, SortableColumnInterface @@ -57,7 +60,6 @@ public function __construct( public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell { - $this->checkColumn($column); /** @var DataColumn $column This annotation is for IDE only */ return $cell @@ -67,7 +69,6 @@ public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): Cell { - $this->checkColumn($column); /** @var DataColumn $column This annotation is for IDE only */ $cell = $cell @@ -96,7 +97,6 @@ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext $context): ?Cell { - $this->checkColumn($column); /** @var DataColumn $column This annotation is for IDE only */ if ($column->property === null || $column->filter === false) { @@ -133,7 +133,6 @@ public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext public function makeFilter(ColumnInterface $column, MakeFilterContext $context): ?FilterInterface { - $this->checkColumn($column); /** @var DataColumn $column This annotation is for IDE only */ if ($column->property === null) { @@ -181,7 +180,6 @@ public function makeFilter(ColumnInterface $column, MakeFilterContext $context): public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell { - $this->checkColumn($column); /** @var DataColumn $column This annotation is for IDE only */ $contentSource = $column->content; @@ -215,7 +213,6 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell { - $this->checkColumn($column); /** @var DataColumn $column This annotation is for IDE only */ if ($column->footer !== null) { @@ -255,25 +252,8 @@ private function castToString(mixed $value, DataColumn $column): string return (string) $value; } - /** - * @psalm-assert DataColumn $column - */ - private function checkColumn(ColumnInterface $column): void - { - if (!$column instanceof DataColumn) { - throw new InvalidArgumentException( - sprintf( - 'Expected "%s", but "%s" given.', - DataColumn::class, - $column::class - ) - ); - } - } - public function getOrderProperties(ColumnInterface $column): array { - $this->checkColumn($column); /** @var DataColumn $column This annotation is for IDE only */ if ($column->property === null) { diff --git a/src/Column/FilterableColumnRendererInterface.php b/src/Column/FilterableColumnRendererInterface.php index 87c1f5cf8..0c8a7f5aa 100644 --- a/src/Column/FilterableColumnRendererInterface.php +++ b/src/Column/FilterableColumnRendererInterface.php @@ -10,12 +10,21 @@ use Yiisoft\Yii\DataView\Column\Base\MakeFilterContext; use Yiisoft\Yii\DataView\Filter\Factory\IncorrectValueException; +/** + * @template TColumn as ColumnInterface + * @extends ColumnRendererInterface + */ interface FilterableColumnRendererInterface extends ColumnRendererInterface { + /** + * @psalm-param TColumn $column + */ public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext $context): ?Cell; /** * @throws IncorrectValueException + * + * @psalm-param TColumn $column */ public function makeFilter(ColumnInterface $column, MakeFilterContext $context): ?FilterInterface; } diff --git a/src/Column/RadioColumnRenderer.php b/src/Column/RadioColumnRenderer.php index 967f9e8a1..b61580d7f 100644 --- a/src/Column/RadioColumnRenderer.php +++ b/src/Column/RadioColumnRenderer.php @@ -4,7 +4,6 @@ namespace Yiisoft\Yii\DataView\Column; -use InvalidArgumentException; use Stringable; use Yiisoft\Html\Html; use Yiisoft\Yii\DataView\Column\Base\Cell; @@ -12,18 +11,20 @@ use Yiisoft\Yii\DataView\Column\Base\GlobalContext; use Yiisoft\Yii\DataView\Column\Base\HeaderContext; +use function array_key_exists; + +/** + * @implements ColumnRendererInterface + */ final class RadioColumnRenderer implements ColumnRendererInterface { public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell { - $this->checkColumn($column); return $cell->addAttributes($column->columnAttributes); } public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): ?Cell { - $this->checkColumn($column); - $header = $column->header; if ($header === null) { return null; @@ -36,8 +37,6 @@ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell { - $this->checkColumn($column); - $inputAttributes = $column->inputAttributes; $name = null; $value = null; @@ -64,28 +63,10 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell { - $this->checkColumn($column); - if ($column->footer !== null) { $cell = $cell->content($column->footer); } return $cell; } - - /** - * @psalm-assert RadioColumn $column - */ - private function checkColumn(ColumnInterface $column): void - { - if (!$column instanceof RadioColumn) { - throw new InvalidArgumentException( - sprintf( - 'Expected "%s", but "%s" given.', - RadioColumn::class, - $column::class - ) - ); - } - } } diff --git a/src/Column/SerialColumnRenderer.php b/src/Column/SerialColumnRenderer.php index 4a943033c..0fe26d05a 100644 --- a/src/Column/SerialColumnRenderer.php +++ b/src/Column/SerialColumnRenderer.php @@ -4,30 +4,28 @@ namespace Yiisoft\Yii\DataView\Column; -use InvalidArgumentException; use Yiisoft\Yii\DataView\Column\Base\Cell; use Yiisoft\Yii\DataView\Column\Base\DataContext; use Yiisoft\Yii\DataView\Column\Base\GlobalContext; use Yiisoft\Yii\DataView\Column\Base\HeaderContext; +/** + * @implements ColumnRendererInterface + */ final class SerialColumnRenderer implements ColumnRendererInterface { public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell { - $this->checkColumn($column); return $cell->addAttributes($column->columnAttributes); } public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): Cell { - $this->checkColumn($column); return $cell->content($column->header ?? '#'); } public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell { - $this->checkColumn($column); - return $cell ->addAttributes($column->bodyAttributes) ->content((string)($context->index + 1)); @@ -35,23 +33,6 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell { - $this->checkColumn($column); return $cell->content($column->footer ?? ''); } - - /** - * @psalm-assert SerialColumn $column - */ - private function checkColumn(ColumnInterface $column): void - { - if (!$column instanceof SerialColumn) { - throw new InvalidArgumentException( - sprintf( - 'Expected "%s", but "%s" given.', - SerialColumn::class, - $column::class - ) - ); - } - } } diff --git a/src/Column/SortableColumnInterface.php b/src/Column/SortableColumnInterface.php index 7f2b0ff1e..65403f891 100644 --- a/src/Column/SortableColumnInterface.php +++ b/src/Column/SortableColumnInterface.php @@ -4,13 +4,17 @@ namespace Yiisoft\Yii\DataView\Column; +/** + * @template TColumn as ColumnInterface + */ interface SortableColumnInterface { /** * @return array The properties that can be sorted by this column. The array keys are the logical property names, * and the array values are the corresponding field names. * - * @psalm-return array + * @psalm-param TColumn $column + * @psalm-return array */ public function getOrderProperties(ColumnInterface $column): array; }