Skip to content

Commit

Permalink
Improve templated closure types (#2182)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Mar 8, 2024
1 parent 5ae029f commit 50dc785
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 27 deletions.
6 changes: 3 additions & 3 deletions demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function atomic(\Closure $fx)
}

/**
* @param \Closure(Model): string $outputCallback
* @param \Closure(static): string $outputCallback
*/
protected function wrapUserActionCallbackPreventModification(Model\UserAction $action, \Closure $outputCallback): void
{
Expand All @@ -169,7 +169,7 @@ protected function wrapUserActionCallbackPreventModification(Model\UserAction $a
$action->callback = $callbackBackup;
}

return $outputCallback($model->isEntity() && !$model->isLoaded() ? $loadedEntity : $model, ...$args);
return $outputCallback($model->isEntity() && !$model->isLoaded() ? $loadedEntity : $model, ...$args); // @phpstan-ignore-line
};
}

Expand Down Expand Up @@ -198,7 +198,7 @@ protected function initPreventModification(): void
/**
* Improve testing by using prefixed real field and SQL names.
*
* @method (static|null) tryLoad(WrappedId $id = null) remove parentheses around return type once https://github.com/phpstan/phpstan/issues/10548 is fixed
* @method static|null tryLoad(WrappedId $id = null)
* @method static load(WrappedId $id)
* @method \Traversable<WrappedId, static> getIterator()
* @method WrappedId insert(array<string, mixed> $row)
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ parameters:
message: '~^Call to method Atk4\\Ui\\App::callExit\(\) with true will always evaluate to true\.$~'
count: 2

# assign to generic callable property is broken https://github.com/phpstan/phpstan/issues/8964
-
message: '~^Property Atk4\\Data\\Model\\UserAction::\$(callback|enabled) .*Closure<.+ does not accept .*Closure\(.+\.$~'
path: '*'
count: 2

# TODO these rules are generated, this ignores should be fixed in the code
# for level = 2
-
Expand Down
6 changes: 3 additions & 3 deletions src/Form/Control/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Dropdown extends Input
* ];
* }
*
* @var \Closure(Model): array{title: mixed, icon?: mixed}|\Closure(mixed, array-key): array{value: mixed, title: mixed, icon?: mixed}
* @var \Closure<T of Model>(T): array{title: mixed, icon?: mixed}|\Closure(mixed, array-key): array{value: mixed, title: mixed, icon?: mixed}
*/
public ?\Closure $renderRowFunction = null;

Expand Down Expand Up @@ -276,8 +276,8 @@ protected function _addCallBackRow($row, $key = null): void
$res = ($this->renderRowFunction)($row);
$this->_tItem->set('value', $this->getApp()->uiPersistence->typecastAttributeSaveField($this->model->getIdField(), $row->getId()));
} else {
$res = ($this->renderRowFunction)($row, $key); // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/10283#issuecomment-1850438891
$this->_tItem->set('value', (string) $res['value']); // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/10283
$res = ($this->renderRowFunction)($row, $key);
$this->_tItem->set('value', (string) $res['value']);
}

$this->_tItem->set('title', $res['title']);
Expand Down
6 changes: 3 additions & 3 deletions src/Form/Control/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Lookup extends Input
*
* If left null, then search will be performed on a model's title field
*
* @var list<string>|\Closure(Model, string): void|null
* @var list<string>|\Closure<T of Model>(T, string): void|null
*/
public $search;

Expand All @@ -54,7 +54,7 @@ class Lookup extends Input
* with dependency
* Then model of the 'state' field can be limited to states of the currently selected 'country'.
*
* @var \Closure(Model, array<string, mixed>): void|null
* @var \Closure<T of Model>(T, array<string, mixed>): void|null
*/
public $dependency;

Expand Down Expand Up @@ -113,7 +113,7 @@ class Lookup extends Input
* Define callback for generating the row data
* If left empty default callback Lookup::defaultRenderRow is used.
*
* @var \Closure($this, Model): array{title: mixed}
* @var \Closure<T of Model>($this, T): array{title: mixed}
*/
public ?\Closure $renderRowFunction = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Multiline extends Form\Control
* Set during fieldDefinition and apply during renderView() after getValue().
* Must contains callable function and function will receive $model field and value as parameter.
*
* @var array<string, \Closure(Field, string): void>
* @var array<string, \Closure<T of Field>(T, string): void>
*/
private array $valuePropsBinding = [];

Expand Down
16 changes: 8 additions & 8 deletions src/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ public function jsReload($args = [], $afterSuccess = null, array $apiConfig = []
* Adds a new button into the action column on the right. For Crud this
* column will already contain "delete" and "edit" buttons.
*
* @param string|array|View $button Label text, object or seed for the Button
* @param string|array|View $button Label text, object or seed for the Button
* @param JsExpressionable|JsCallbackSetClosure $action
* @param bool|\Closure(Model): bool $isDisabled
* @param bool|\Closure<T of Model>(T): bool $isDisabled
*
* @return View
*/
Expand Down Expand Up @@ -395,7 +395,7 @@ private function getActionButtons(): Table\Column\ActionButtons
*
* @param View|string $view
* @param JsExpressionable|JsCallbackSetClosure $action
* @param bool|\Closure(Model): bool $isDisabled
* @param bool|\Closure<T of Model>(T): bool $isDisabled
*
* @return View
*/
Expand Down Expand Up @@ -499,8 +499,8 @@ public function addPopup($columnName, $popup = null, $icon = 'caret square down'
* @param string|array|View $button
* @param string $title
* @param \Closure(View, mixed): void $callback
* @param array $args extra URL argument for callback
* @param bool|\Closure(Model): bool $isDisabled
* @param array $args extra URL argument for callback
* @param bool|\Closure<T of Model>(T): bool $isDisabled
*
* @return View
*/
Expand All @@ -526,9 +526,9 @@ private function explodeSelectionValue(string $value): array
* Similar to addActionButton but apply to a multiple records selection and display in menu.
* When menu item is clicked, $callback is executed.
*
* @param string|array|MenuItem $item
* @param \Closure(Js\Jquery, list<mixed>): JsExpressionable $callback
* @param array $args extra URL argument for callback
* @param string|array|MenuItem $item
* @param \Closure(Jquery, list<mixed>): JsExpressionable $callback
* @param array $args extra URL argument for callback
*
* @return View
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Panel/Right.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function addConfirmation(string $msg, string $title = 'Closing panel!', s
* Callback to execute when panel open if dynamic content is set.
* Differ the callback execution to the FlyoutContent.
*
* @param \Closure(object): void $fx
* @param \Closure<T of LoadableContent>(T): void $fx
*/
public function onOpen(\Closure $fx): void
{
Expand Down
8 changes: 4 additions & 4 deletions src/Table/Column/ActionButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ActionButtons extends Table\Column
/** @var array<string, View> Stores all the buttons that have been added. */
public $buttons = [];

/** @var array<string, \Closure(Model): bool> Callbacks as defined in UserAction->enabled for evaluating row-specific if an action is enabled. */
/** @var array<string, \Closure<T of Model>(T): bool> Callbacks as defined in UserAction->enabled for evaluating row-specific if an action is enabled. */
protected $isEnabledFxs = [];

#[\Override]
Expand All @@ -41,7 +41,7 @@ protected function init(): void
*
* @param string|array|View $button
* @param JsExpressionable|JsCallbackSetClosure|ExecutorInterface $action
* @param bool|\Closure(Model): bool $isDisabled
* @param bool|\Closure<T of Model>(T): bool $isDisabled
*
* @return View
*/
Expand Down Expand Up @@ -79,11 +79,11 @@ public function addButton($button, $action = null, string $confirmMsg = '', $isD
* load contents through $callback. Will pass a virtual page.
*
* @param string|array|View $button
* @param string|array $defaults modal title or modal defaults array
* @param string|array $defaults modal title or modal defaults array
* @param \Closure(View, mixed): void $callback
* @param View $owner
* @param array $args
* @param bool|\Closure(Model): bool $isDisabled
* @param bool|\Closure<T of Model>(T): bool $isDisabled
*
* @return View
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Table/Column/ActionMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ActionMenu extends Table\Column
/** @var array<int, View> Menu items collections. */
protected $items = [];

/** @var array<string, \Closure(Model): bool> Callbacks as defined in UserAction->enabled for evaluating row-specific if an action is enabled. */
/** @var array<string, \Closure<T of Model>(T): bool> Callbacks as defined in UserAction->enabled for evaluating row-specific if an action is enabled. */
protected $isEnabledFxs = [];

/** @var string Dropdown label. */
Expand Down Expand Up @@ -55,7 +55,7 @@ public function getTag(string $position, $attr, $value): string
*
* @param View|string $item
* @param JsExpressionable|JsCallbackSetClosure|ExecutorInterface $action
* @param bool|\Closure(Model): bool $isDisabled
* @param bool|\Closure<T of Model>(T): bool $isDisabled
*
* @return View
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Table/Column/Multiformat.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
*/
class Multiformat extends Table\Column
{
/** @var \Closure(Model, Field|null): list<array<0|string, mixed>|Table\Column> Method to execute which will return array of seeds for decorators */
/** @var \Closure<TModel of Model, TField of Field>(TModel, TField|null): list<array<0|string, mixed>|Table\Column> Method to execute which will return array of seeds for decorators */
protected \Closure $decoratorsFx;

/**
* @param \Closure(Model, Field|null): list<array<0|string, mixed>|Table\Column> $decoratorsFx
* @param \Closure<TModel of Model, TField of Field>(TModel, TField|null): list<array<0|string, mixed>|Table\Column> $decoratorsFx
*/
public function __construct(\Closure $decoratorsFx)
{
Expand Down

0 comments on commit 50dc785

Please sign in to comment.