Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve psalm #175

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions src/Column/ActionButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@

use Closure;
use Stringable;
use Yiisoft\Yii\DataView\Column\Base\DataContext;

/**
* @psalm-type ContentClosure = Closure(array|object,DataContext):string|Stringable
* @psalm-type UrlClosure = Closure(array|object,DataContext):string
* @psalm-type AttributesClosure = Closure(array|object,DataContext):array
* @psalm-type ClassClosure = Closure(array|object,DataContext):string|array<array-key,string|null>|null
*/
final class ActionButton
{
/**
* @psalm-param ContentClosure|string|Stringable $content
* @psalm-param UrlClosure|string|null $url
* @psalm-param AttributesClosure|array|null $attributes
* @psalm-param ClassClosure|string|array<array-key,string|null>|null|false $class
* @param Closure|string|Stringable $content Closure signature: Closure(array|object,DataContext): string|Stringable
* @param Closure|string|null $url Closure signature: Closure(array|object, DataContext): string
* @param array|Closure|null $attributes Closure signature: Closure(array|object,DataContext):array
* @param array<array-key,string|null>|Closure|false|string|null $class Closure signature: Closure(array|object,DataContext):string|array<array-key,string|null>|null
*/
public function __construct(
public readonly Closure|string|Stringable $content = '',
Expand Down
4 changes: 2 additions & 2 deletions src/Column/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ final class ActionColumn implements ColumnInterface
* @param ?callable $urlCreator A callback that creates a button URL using the specified data information.
*
* @psalm-param UrlCreator|null $urlCreator
* @psalm-param array<string,ButtonRenderer>|null $buttons
* @psalm-param array<string,bool|Closure>|null $visibleButtons
* @psalm-param array<array-key, ButtonRenderer>|null $buttons
* @psalm-param array<string, bool|Closure>|null $visibleButtons
*/
public function __construct(
public readonly ?string $template = null,
Expand Down
13 changes: 8 additions & 5 deletions src/Column/ActionColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ final class ActionColumnRenderer implements ColumnRendererInterface
private $urlCreator;

/**
* @psalm-var array<string,ButtonRenderer>
* @psalm-var array<array-key, ButtonRenderer>
*/
private readonly array $buttons;

/**
* @psalm-param UrlCreator|null $urlCreator
* @psalm-param array<string,ButtonRenderer>|null $buttons
* @psalm-param array<array-key, ButtonRenderer>|null $buttons
* @psalm-param array<string, string>|string|null $buttonClass
*/
public function __construct(
Expand Down Expand Up @@ -93,7 +93,7 @@ function (array $matches) use ($column, $buttons, $context): string {

return '';
},
$this->getTemplate($column, $buttons, $context),
$this->getTemplate($column, $buttons),
);
$content = trim($content);
}
Expand Down Expand Up @@ -137,13 +137,15 @@ private function renderButton(ActionButton|callable $button, string $name, DataC
$url = $this->createUrl($name, $context);
} elseif ($button->url instanceof Closure) {
$closure = $button->url;
/** @var string $url */
$url = $closure($context->data, $context);
} else {
$url = $button->url;
}

if ($button->attributes instanceof Closure) {
$closure = $button->attributes;
/** @var array $attributes */
$attributes = $closure($context->data, $context);
} else {
$attributes = $button->attributes ?? [];
Expand All @@ -156,6 +158,7 @@ private function renderButton(ActionButton|callable $button, string $name, DataC

if ($button->class instanceof Closure) {
$closure = $button->class;
/** @var array<array-key,string|null>|string|null $class */
$class = $closure($context->data, $context);
} else {
$class = $button->class;
Expand Down Expand Up @@ -206,9 +209,9 @@ private function isVisibleButton(
}

/**
* @psalm-param array<string,ButtonRenderer> $buttons
* @psalm-param array<array-key, ButtonRenderer> $buttons
*/
private function getTemplate(ActionColumn $column, array $buttons, DataContext $context): string
private function getTemplate(ActionColumn $column, array $buttons): string
{
if ($column->template !== null) {
return $column->template;
Expand Down
Loading