From ae5cd3b4d03ce496fc2b0a3fea5de901e812475f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Wed, 6 Sep 2023 13:14:05 +0200 Subject: [PATCH] Replace call_user_func() calls as much as possible (#1625) --- src/App.php | 4 +--- src/Form/Control/Multiline.php | 27 ++++++++++++++------------- src/Form/Control/ScopeBuilder.php | 14 +++++++++----- src/UserAction/ExecutorFactory.php | 10 ++++++++-- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/App.php b/src/App.php index 880a03349d..6ad3f5072b 100644 --- a/src/App.php +++ b/src/App.php @@ -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 diff --git a/src/Form/Control/Multiline.php b/src/Form/Control/Multiline.php index 535828f77e..3b9a35550d 100644 --- a/src/Form/Control/Multiline.php +++ b/src/Form/Control/Multiline.php @@ -108,7 +108,7 @@ class Multiline extends Form\Control /** @var array SuiTable component props */ public $tableProps = []; - /** @var array Set Vue component to use per field type. */ + /** @var array> Set Vue component to use per field type. */ protected $fieldMapToComponent = [ 'default' => [ 'component' => self::INPUT, @@ -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 */ private $valuePropsBinding = []; @@ -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; } @@ -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 @@ -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); } } } @@ -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, @@ -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 } diff --git a/src/Form/Control/ScopeBuilder.php b/src/Form/Control/ScopeBuilder.php index 9031e6a256..e0228bfae2 100644 --- a/src/Form/Control/ScopeBuilder.php +++ b/src/Form/Control/ScopeBuilder.php @@ -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; } /** diff --git a/src/UserAction/ExecutorFactory.php b/src/UserAction/ExecutorFactory.php index 5d9bddd4f6..c8393d3a80 100644 --- a/src/UserAction/ExecutorFactory.php +++ b/src/UserAction/ExecutorFactory.php @@ -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); } @@ -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; } /**