Skip to content

Commit

Permalink
Replace call_user_func() calls as much as possible (#1625)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Sep 6, 2023
1 parent afdae7e commit ae5cd3b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
4 changes: 1 addition & 3 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ public function __construct(array $defaults = [])

throw new \ErrorException($msg, 0, $severity, $file, $line);
});
if (\PHP_SAPI !== 'cli') { // for phpunit
http_response_code(500);
}
http_response_code(500);
}

// always run app on shutdown
Expand Down
27 changes: 14 additions & 13 deletions src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Multiline extends Form\Control
/** @var array SuiTable component props */
public $tableProps = [];

/** @var array<string, array{component: string, componentProps: mixed}> Set Vue component to use per field type. */
/** @var array<string, array<string, mixed>> Set Vue component to use per field type. */
protected $fieldMapToComponent = [
'default' => [
'component' => self::INPUT,
Expand Down 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
* @var array<string, \Closure(Field, string): void>
*/
private $valuePropsBinding = [];

Expand Down Expand Up @@ -546,7 +546,7 @@ protected function getLookupProps(Field $field): array

$props['config']['placeholder'] ??= 'Select ' . $field->getCaption();

$this->valuePropsBinding[$field->shortName] = [__CLASS__, 'setLookupOptionValue'];
$this->valuePropsBinding[$field->shortName] = fn ($field, $value) => $this->setLookupOptionValue($field, $value);

return $props;
}
Expand Down Expand Up @@ -590,13 +590,14 @@ protected function getComponentDefinition(Field $field): array
$component = $this->fieldMapToComponent['default'];
}

$definition = array_map(function ($value) use ($field) {
$this->issetOwner(); // prevent PHP CS Fixer to make this anonymous function static, TODO https://github.com/atk4/ui/pull/1625

return is_array($value) && is_callable($value) ? call_user_func($value, $field) : $value;
}, $component);
// map all callables defaults
foreach ($component as $k => $v) {
if (is_array($v) && is_callable($v)) {
$component[$k] = call_user_func($v, $field);
}
}

return $definition;
return $component;
}

protected function getFieldItems(Field $field, ?int $limit = 10): array
Expand Down Expand Up @@ -629,8 +630,8 @@ protected function valuePropsBinding(string $values): void

foreach ($fieldValues as $rows) {
foreach ($rows as $fieldName => $value) {
if (array_key_exists($fieldName, $this->valuePropsBinding)) {
call_user_func($this->valuePropsBinding[$fieldName], $this->model->getField($fieldName), $value);
if (isset($this->valuePropsBinding[$fieldName])) {
($this->valuePropsBinding[$fieldName])($this->model->getField($fieldName), $value);
}
}
}
Expand All @@ -657,7 +658,7 @@ protected function renderView(): void
'fields' => $this->fieldDefs,
'url' => $this->renderCallback->getJsUrl(),
'eventFields' => $this->eventFields,
'hasChangeCb' => $this->onChangeFunction ? true : false,
'hasChangeCb' => $this->onChangeFunction !== null,
'tableProps' => $this->tableProps,
'rowLimit' => $this->rowLimit,
'caption' => $this->caption,
Expand All @@ -681,7 +682,7 @@ private function outputJson(): void
$this->getApp()->terminateJson(['success' => true, 'expressions' => $expressionValues]);
// no break - expression above always terminate
case 'on-change':
$response = call_user_func($this->onChangeFunction, $this->typeCastLoadValues($this->getApp()->decodeJson($_POST['rows'])), $this->form);
$response = ($this->onChangeFunction)($this->typeCastLoadValues($this->getApp()->decodeJson($_POST['rows'])), $this->form);
$this->renderCallback->terminateAjax($this->renderCallback->getAjaxec($response));
// TODO JsCallback::terminateAjax() should return never
}
Expand Down
14 changes: 9 additions & 5 deletions src/Form/Control/ScopeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,16 @@ protected function getRule(string $type, array $defaults = [], Field $field = nu
$options = $defaults['options'] ?? [];
unset($defaults['options']);

// map all values for callables and merge with defaults
return array_merge(array_map(function ($value) use ($field, $options) {
$this->issetOwner(); // prevent PHP CS Fixer to make this anonymous function static, TODO https://github.com/atk4/ui/pull/1625
// map all callables
foreach ($rule as $k => $v) {
if (is_array($v) && is_callable($v)) {
$rule[$k] = call_user_func($v, $field, $options);
}
}

$rule = array_merge($rule, $defaults);

return is_array($value) && is_callable($value) ? call_user_func($value, $field, $options) : $value;
}, $rule), $defaults);
return $rule;
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/UserAction/ExecutorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ protected function createActionTrigger(UserAction $action, string $type = null):
}
}

$seed = is_array($seed) && is_callable($seed) ? call_user_func($seed, $action, $type) : $seed;
if (is_array($seed) && is_callable($seed)) {
$seed = call_user_func($seed, $action, $type);
}

return Factory::factory($seed);
}
Expand Down Expand Up @@ -242,7 +244,11 @@ protected function getActionCaption(UserAction $action, string $type = null): st
}
}

return is_array($caption) && is_callable($caption) ? call_user_func($caption, $action) : $caption;
if (is_array($caption) && is_callable($caption)) {
$caption = call_user_func($caption, $action);
}

return $caption;
}

/**
Expand Down

0 comments on commit ae5cd3b

Please sign in to comment.