From cfe069b5880e07930e92058b774f22e491aa7b0e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 28 Oct 2020 16:19:15 +0100 Subject: [PATCH] coding style --- examples/custom-control.php | 26 +++++++++---------- examples/manual-rendering.php | 8 +++--- src/Bridges/FormsLatte/Runtime.php | 8 ++++-- src/Forms/Container.php | 16 +++++++++--- src/Forms/Controls/BaseControl.php | 14 +++++++--- src/Forms/Controls/Button.php | 2 +- src/Forms/Controls/CheckboxList.php | 8 +++--- src/Forms/Controls/ChoiceControl.php | 12 ++++----- src/Forms/Controls/CsrfProtection.php | 2 +- src/Forms/Controls/TextBase.php | 8 ++++-- src/Forms/Controls/TextInput.php | 8 ++++-- src/Forms/Form.php | 12 ++++----- src/Forms/Helpers.php | 24 ++++++++++++----- src/Forms/Rendering/DefaultFormRenderer.php | 10 +++++-- src/Forms/Rules.php | 22 ++++++++++------ src/Forms/Validator.php | 4 ++- .../Forms.Latte/Runtime.renderBlueprint.phpt | 2 +- ...phpt => Container.values.mapping.74.phptx} | 1 - tests/Forms/Container.values.mapping.phpt | 1 - tests/Forms/Controls.BaseControl.phpt | 1 - tests/Forms/Controls.TestBase.validators.phpt | 4 +-- tests/Forms/Helpers.extractHttpData.phpt | 1 - 22 files changed, 119 insertions(+), 75 deletions(-) rename tests/Forms/{Container.values.mapping.74.phpt => Container.values.mapping.74.phptx} (99%) diff --git a/examples/custom-control.php b/examples/custom-control.php index 87ccb6353..ca1f3d236 100644 --- a/examples/custom-control.php +++ b/examples/custom-control.php @@ -29,7 +29,7 @@ class DateInput extends Nette\Forms\Controls\BaseControl public function __construct($label = null) { parent::__construct($label); - $this->addRule([__CLASS__, 'validateDate'], 'Date is invalid.'); + $this->addRule([self::class, 'validateDate'], 'Date is invalid.'); } @@ -76,20 +76,20 @@ public function getControl() { $name = $this->getHtmlName(); return Html::el('input', [ - 'name' => $name . '[day]', - 'id' => $this->getHtmlId(), - 'value' => $this->day, - 'type' => 'number', - 'min' => 1, - 'max' => 31, - 'data-nette-rules' => Helpers::exportRules($this->getRules()) ?: null, - ]) + 'name' => $name . '[day]', + 'id' => $this->getHtmlId(), + 'value' => $this->day, + 'type' => 'number', + 'min' => 1, + 'max' => 31, + 'data-nette-rules' => Helpers::exportRules($this->getRules()) ?: null, + ]) . Helpers::createSelectBox( - [1 => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], - [], - $this->month - )->name($name . '[month]') + [1 => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], + [], + $this->month + )->name($name . '[month]') . Html::el('input', [ 'name' => $name . '[year]', diff --git a/examples/manual-rendering.php b/examples/manual-rendering.php index e25c8abb7..521af8d7e 100644 --- a/examples/manual-rendering.php +++ b/examples/manual-rendering.php @@ -56,13 +56,13 @@ render('begin') ?> - errors): ?> + errors) { ?> - +
Personal data diff --git a/src/Bridges/FormsLatte/Runtime.php b/src/Bridges/FormsLatte/Runtime.php index fb8d5be4b..03ee8c4f9 100644 --- a/src/Bridges/FormsLatte/Runtime.php +++ b/src/Bridges/FormsLatte/Runtime.php @@ -88,7 +88,9 @@ public static function renderBlueprint($form): void public function getLabel($name = null) { - return $this->inner->getLabel() ? '{label ' . $this->inner->lookupPath(Form::class) . '/}' : null; + return $this->inner->getLabel() + ? '{label ' . $this->inner->lookupPath(Form::class) . '/}' + : null; } @@ -106,7 +108,9 @@ public function isRequired(): bool public function getOption($key, $default = null) { - return $key === 'rendered' ? parent::getOption($key) : $this->inner->getOption($key, $default); + return $key === 'rendered' + ? parent::getOption($key) + : $this->inner->getOption($key, $default); } }; $dummyInput->inner = $input; diff --git a/src/Forms/Container.php b/src/Forms/Container.php index 5906cdd4b..db83acdd9 100644 --- a/src/Forms/Container.php +++ b/src/Forms/Container.php @@ -267,8 +267,12 @@ public function addText(string $name, $label = null, int $cols = null, int $maxL * Adds single-line text input control used for sensitive input such as passwords. * @param string|object $label */ - public function addPassword(string $name, $label = null, int $cols = null, int $maxLength = null): Controls\TextInput - { + public function addPassword( + string $name, + $label = null, + int $cols = null, + int $maxLength = null + ): Controls\TextInput { return $this[$name] = (new Controls\TextInput($label, $maxLength)) ->setHtmlAttribute('size', $cols) ->setHtmlType('password'); @@ -388,8 +392,12 @@ public function addSelect(string $name, $label = null, array $items = null, int * Adds select box control that allows multiple item selection. * @param string|object $label */ - public function addMultiSelect(string $name, $label = null, array $items = null, int $size = null): Controls\MultiSelectBox - { + public function addMultiSelect( + string $name, + $label = null, + array $items = null, + int $size = null + ): Controls\MultiSelectBox { return $this[$name] = (new Controls\MultiSelectBox($label, $items)) ->setHtmlAttribute('size', $size > 1 ? $size : null); } diff --git a/src/Forms/Controls/BaseControl.php b/src/Forms/Controls/BaseControl.php index 99541ed4f..3101929bd 100644 --- a/src/Forms/Controls/BaseControl.php +++ b/src/Forms/Controls/BaseControl.php @@ -273,7 +273,7 @@ public function getLabel($caption = null) { $label = clone $this->label; $label->for = $this->getHtmlId(); - $caption = $caption === null ? $this->caption : $caption; + $caption = $caption ?? $this->caption; $translator = $this->getForm()->getTranslator(); $label->setText($translator && !$caption instanceof Nette\Utils\IHtmlString ? $translator->translate($caption) : $caption); return $label; @@ -346,7 +346,13 @@ public function getHtmlId() public function setHtmlAttribute(string $name, $value = true) { $this->control->$name = $value; - if ($name === 'name' && ($form = $this->getForm(false)) && !$this->isDisabled() && $form->isAnchored() && $form->isSubmitted()) { + if ( + $name === 'name' + && ($form = $this->getForm(false)) + && !$this->isDisabled() + && $form->isAnchored() + && $form->isSubmitted() + ) { $this->loadHttpData(); } return $this; @@ -383,7 +389,9 @@ public function setTranslator(?Nette\Localization\ITranslator $translator) public function getTranslator(): ?Nette\Localization\ITranslator { if ($this->translator === true) { - return $this->getForm(false) ? $this->getForm()->getTranslator() : null; + return $this->getForm(false) + ? $this->getForm()->getTranslator() + : null; } return $this->translator; } diff --git a/src/Forms/Controls/Button.php b/src/Forms/Controls/Button.php index 088733f5b..8a7bbe2e8 100644 --- a/src/Forms/Controls/Button.php +++ b/src/Forms/Controls/Button.php @@ -58,7 +58,7 @@ public function getControl($caption = null): Nette\Utils\Html return $el->addAttributes([ 'name' => $this->getHtmlName(), 'disabled' => $this->isDisabled(), - 'value' => $this->translate($caption === null ? $this->getCaption() : $caption), + 'value' => $this->translate($caption ?? $this->getCaption()), ]); } } diff --git a/src/Forms/Controls/CheckboxList.php b/src/Forms/Controls/CheckboxList.php index c961fcec6..c933ca26c 100644 --- a/src/Forms/Controls/CheckboxList.php +++ b/src/Forms/Controls/CheckboxList.php @@ -49,11 +49,9 @@ public function __construct($label = null, array $items = null) public function loadHttpData(): void { $data = $this->getForm()->getHttpData(Nette\Forms\Form::DATA_TEXT, substr($this->getHtmlName(), 0, -2)); - if ($data === null) { - $data = $this->getHttpData(Nette\Forms\Form::DATA_TEXT); - } else { - $data = explode(',', $data); - } + $data = $data === null + ? $this->getHttpData(Nette\Forms\Form::DATA_TEXT) + : explode(',', $data); $this->value = array_keys(array_flip($data)); if (is_array($this->disabled)) { $this->value = array_diff($this->value, array_keys($this->disabled)); diff --git a/src/Forms/Controls/ChoiceControl.php b/src/Forms/Controls/ChoiceControl.php index 8e59765b2..21c0445c7 100644 --- a/src/Forms/Controls/ChoiceControl.php +++ b/src/Forms/Controls/ChoiceControl.php @@ -40,11 +40,9 @@ public function loadHttpData(): void { $this->value = $this->getHttpData(Nette\Forms\Form::DATA_TEXT); if ($this->value !== null) { - if (is_array($this->disabled) && isset($this->disabled[$this->value])) { - $this->value = null; - } else { - $this->value = key([$this->value => null]); - } + $this->value = is_array($this->disabled) && isset($this->disabled[$this->value]) + ? null + : key([$this->value => null]); } } @@ -72,7 +70,9 @@ public function setValue($value) */ public function getValue() { - return array_key_exists($this->value, $this->items) ? $this->value : null; + return array_key_exists($this->value, $this->items) + ? $this->value + : null; } diff --git a/src/Forms/Controls/CsrfProtection.php b/src/Forms/Controls/CsrfProtection.php index c1eef2913..5ec0274d6 100644 --- a/src/Forms/Controls/CsrfProtection.php +++ b/src/Forms/Controls/CsrfProtection.php @@ -71,7 +71,7 @@ public function getToken(): string if (!$this->session) { throw new Nette\InvalidStateException('Session initialization error'); } - $session = $this->session->getSection(__CLASS__); + $session = $this->session->getSection(self::class); if (!isset($session->token)) { $session->token = Nette\Utils\Random::generate(); } diff --git a/src/Forms/Controls/TextBase.php b/src/Forms/Controls/TextBase.php index afa30783f..3c3c52e08 100644 --- a/src/Forms/Controls/TextBase.php +++ b/src/Forms/Controls/TextBase.php @@ -53,7 +53,9 @@ public function setValue($value) */ public function getValue() { - $value = $this->value === Strings::trim($this->translate($this->emptyValue)) ? '' : $this->value; + $value = $this->value === Strings::trim($this->translate($this->emptyValue)) + ? '' + : $this->value; return $this->nullable && $value === '' ? null : $value; } @@ -138,7 +140,9 @@ public function addRule($validator, $errorMessage = null, $arg = null) if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) { $tmp = is_array($arg) ? $arg[1] : $arg; if (is_scalar($tmp)) { - $this->control->maxlength = isset($this->control->maxlength) ? min($this->control->maxlength, $tmp) : $tmp; + $this->control->maxlength = isset($this->control->maxlength) + ? min($this->control->maxlength, $tmp) + : $tmp; } } return parent::addRule($validator, $errorMessage, $arg); diff --git a/src/Forms/Controls/TextInput.php b/src/Forms/Controls/TextInput.php index 209ca1a7d..15b354aff 100644 --- a/src/Forms/Controls/TextInput.php +++ b/src/Forms/Controls/TextInput.php @@ -84,10 +84,14 @@ public function addRule($validator, $errorMessage = null, $arg = null) $range = $arg; } if (isset($range[0]) && is_scalar($range[0])) { - $this->control->min = isset($this->control->min) ? max($this->control->min, $range[0]) : $range[0]; + $this->control->min = isset($this->control->min) + ? max($this->control->min, $range[0]) + : $range[0]; } if (isset($range[1]) && is_scalar($range[1])) { - $this->control->max = isset($this->control->max) ? min($this->control->max, $range[1]) : $range[1]; + $this->control->max = isset($this->control->max) + ? min($this->control->max, $range[1]) + : $range[1]; } } elseif ( diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 1b80f17ef..8b7d0aad9 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -133,7 +133,7 @@ public function __construct(string $name = null) $this[self::TRACKER_ID] = $tracker; $this->setParent(null, $name); } - $this->monitor(__CLASS__, function (): void { + $this->monitor(self::class, function (): void { throw new Nette\InvalidStateException('Nested forms are forbidden.'); }); } @@ -238,11 +238,9 @@ public function addGroup(string $caption = null, bool $setAsCurrent = true): Con $this->setCurrentGroup($group); } - if (!is_scalar($caption) || isset($this->groups[$caption])) { - return $this->groups[] = $group; - } else { - return $this->groups[$caption] = $group; - } + return !is_scalar($caption) || isset($this->groups[$caption]) + ? $this->groups[] = $group + : $this->groups[$caption] = $group; } @@ -355,7 +353,7 @@ public function isSuccess(): bool */ public function setSubmittedBy(?ISubmitterControl $by) { - $this->submittedBy = $by === null ? false : $by; + $this->submittedBy = $by ?? false; return $this; } diff --git a/src/Forms/Helpers.php b/src/Forms/Helpers.php index 3129c0be1..6d4aa86bb 100644 --- a/src/Forms/Helpers.php +++ b/src/Forms/Helpers.php @@ -62,10 +62,14 @@ public static function extractHttpData(array $data, string $htmlName, int $type) private static function sanitize(int $type, $value) { if ($type === Form::DATA_TEXT) { - return is_scalar($value) ? Strings::normalizeNewLines($value) : null; + return is_scalar($value) + ? Strings::normalizeNewLines($value) + : null; } elseif ($type === Form::DATA_LINE) { - return is_scalar($value) ? Strings::trim(strtr((string) $value, "\r\n", ' ')) : null; + return is_scalar($value) + ? Strings::trim(strtr((string) $value, "\r\n", ' ')) + : null; } elseif ($type === Form::DATA_FILE) { return $value instanceof Nette\Http\FileUpload ? $value : null; @@ -124,10 +128,14 @@ public static function exportRules(Rules $rules): array if (is_array($rule->arg)) { $item['arg'] = []; foreach ($rule->arg as $key => $value) { - $item['arg'][$key] = $value instanceof IControl ? ['control' => $value->getHtmlName()] : $value; + $item['arg'][$key] = $value instanceof IControl + ? ['control' => $value->getHtmlName()] + : $value; } } elseif ($rule->arg !== null) { - $item['arg'] = $rule->arg instanceof IControl ? ['control' => $rule->arg->getHtmlName()] : $rule->arg; + $item['arg'] = $rule->arg instanceof IControl + ? ['control' => $rule->arg->getHtmlName()] + : $rule->arg; } $payload[] = $item; @@ -136,8 +144,12 @@ public static function exportRules(Rules $rules): array } - public static function createInputList(array $items, array $inputAttrs = null, array $labelAttrs = null, $wrapper = null): string - { + public static function createInputList( + array $items, + array $inputAttrs = null, + array $labelAttrs = null, + $wrapper = null + ): string { [$inputAttrs, $inputTag] = self::prepareAttrs($inputAttrs, 'input'); [$labelAttrs, $labelTag] = self::prepareAttrs($labelAttrs, 'label'); $res = ''; diff --git a/src/Forms/Rendering/DefaultFormRenderer.php b/src/Forms/Rendering/DefaultFormRenderer.php index 98c244835..8ea2eff91 100644 --- a/src/Forms/Rendering/DefaultFormRenderer.php +++ b/src/Forms/Rendering/DefaultFormRenderer.php @@ -262,7 +262,9 @@ public function renderBody(): string } $container = $group->getOption('container', $defaultContainer); - $container = $container instanceof Html ? clone $container : Html::el($container); + $container = $container instanceof Html + ? clone $container + : Html::el($container); $id = $group->getOption('id'); if ($id) { @@ -324,7 +326,11 @@ public function renderControls($parent): string $buttons = null; foreach ($parent->getControls() as $control) { - if ($control->getOption('rendered') || $control->getOption('type') === 'hidden' || $control->getForm(false) !== $this->form) { + if ( + $control->getOption('rendered') + || $control->getOption('type') === 'hidden' + || $control->getForm(false) !== $this->form + ) { // skip } elseif ($control->getOption('type') === 'button') { diff --git a/src/Forms/Rules.php b/src/Forms/Rules.php index a3bbf5a7b..f7da9ad6e 100644 --- a/src/Forms/Rules.php +++ b/src/Forms/Rules.php @@ -249,7 +249,11 @@ public function validate(bool $emptyOptional = null): bool } $success = $this->validateRule($rule); - if ($success && $rule->branch && !$rule->branch->validate($rule->validator === Form::BLANK ? false : $emptyOptional)) { + if ( + $success + && $rule->branch + && !$rule->branch->validate($rule->validator === Form::BLANK ? false : $emptyOptional) + ) { return false; } elseif (!$success && !$rule->branch) { @@ -310,7 +314,9 @@ private function adjustOperation(Rule $rule): void $rule->isNegative = true; $rule->validator = ~$rule->validator; if (!$rule->branch) { - $name = strncmp($rule->validator, ':', 1) ? $rule->validator : 'Form:' . strtoupper($rule->validator); + $name = strncmp($rule->validator, ':', 1) + ? $rule->validator + : 'Form:' . strtoupper($rule->validator); trigger_error("Negative validation rules such as ~$name are deprecated.", E_USER_DEPRECATED); } if (isset(self::NEG_RULES[$rule->validator])) { @@ -321,7 +327,9 @@ private function adjustOperation(Rule $rule): void } if (!is_callable($this->getCallback($rule))) { - $validator = is_scalar($rule->validator) ? " '$rule->validator'" : ''; + $validator = is_scalar($rule->validator) + ? " '$rule->validator'" + : ''; throw new Nette\InvalidArgumentException("Unknown validator$validator for control '{$rule->control->name}'."); } } @@ -330,10 +338,8 @@ private function adjustOperation(Rule $rule): void private static function getCallback(Rule $rule) { $op = $rule->validator; - if (is_string($op) && strncmp($op, ':', 1) === 0) { - return [Validator::class, 'validate' . ltrim($op, ':')]; - } else { - return $op; - } + return is_string($op) && strncmp($op, ':', 1) === 0 + ? [Validator::class, 'validate' . ltrim($op, ':')] + : $op; } } diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index e970ffd6f..3a4df0ce2 100644 --- a/src/Forms/Validator.php +++ b/src/Forms/Validator.php @@ -81,7 +81,9 @@ public static function formatMessage(Rule $rule, bool $withValue = true) return rtrim((string) $caption, ':'); } return ''; - case 'value': return $withValue ? $rule->control->getValue() : $m[0]; + case 'value': return $withValue + ? $rule->control->getValue() + : $m[0]; default: $args = is_array($rule->arg) ? $rule->arg : [$rule->arg]; $i = (int) $m[1] ? (int) $m[1] - 1 : $i + 1; diff --git a/tests/Forms.Latte/Runtime.renderBlueprint.phpt b/tests/Forms.Latte/Runtime.renderBlueprint.phpt index fbe7347b6..802a0e666 100644 --- a/tests/Forms.Latte/Runtime.renderBlueprint.phpt +++ b/tests/Forms.Latte/Runtime.renderBlueprint.phpt @@ -37,7 +37,7 @@ Nette\Bridges\FormsLatte\Runtime::renderBlueprint($form); $res = ob_get_clean(); Assert::match( -'%A%
+ '%A%
  • {$error}
  • diff --git a/tests/Forms/Container.values.mapping.74.phpt b/tests/Forms/Container.values.mapping.74.phptx similarity index 99% rename from tests/Forms/Container.values.mapping.74.phpt rename to tests/Forms/Container.values.mapping.74.phptx index d1c5e7a17..bffd9c3a7 100644 --- a/tests/Forms/Container.values.mapping.74.phpt +++ b/tests/Forms/Container.values.mapping.74.phptx @@ -223,7 +223,6 @@ test('getValues(...arguments...)', function () { ]), ]), $form->getValues(FormData::class)); - $form->setMappedType(FormData::class); $form['first']->setMappedType(FormFirstLevel::class); $form['first-second']->setMappedType(FormSecondLevel::class); diff --git a/tests/Forms/Container.values.mapping.phpt b/tests/Forms/Container.values.mapping.phpt index d8178ae70..fe8ad7111 100644 --- a/tests/Forms/Container.values.mapping.phpt +++ b/tests/Forms/Container.values.mapping.phpt @@ -220,7 +220,6 @@ test('getValues(...arguments...)', function () { ]), ]), $form->getValues(FormData::class)); - $form->setMappedType(FormData::class); $form['first']->setMappedType(FormFirstLevel::class); $form['first-second']->setMappedType(FormSecondLevel::class); diff --git a/tests/Forms/Controls.BaseControl.phpt b/tests/Forms/Controls.BaseControl.phpt index a2bc4ca13..c2ddba740 100644 --- a/tests/Forms/Controls.BaseControl.phpt +++ b/tests/Forms/Controls.BaseControl.phpt @@ -135,7 +135,6 @@ test('disabled & submitted', function () { Assert::true($form->isSubmitted()); Assert::same('default', $form['disabled']->getValue()); - unset($form['disabled']); $input = new Nette\Forms\Controls\TextInput; $input->setDisabled() diff --git a/tests/Forms/Controls.TestBase.validators.phpt b/tests/Forms/Controls.TestBase.validators.phpt index 93e65cc5f..2bd6c35e6 100644 --- a/tests/Forms/Controls.TestBase.validators.phpt +++ b/tests/Forms/Controls.TestBase.validators.phpt @@ -98,8 +98,7 @@ test('', function () { test('', function () { $control = new TextInput; - - $control->value = new class () { + $control->value = new class() { public $lorem = 'ipsum'; @@ -109,7 +108,6 @@ test('', function () { } }; - Assert::false(Validator::validatePattern($control, '[0-9]')); Assert::true(Validator::validatePattern($control, '[0-9]+x')); Assert::false(Validator::validatePattern($control, '[0-9]+X')); diff --git a/tests/Forms/Helpers.extractHttpData.phpt b/tests/Forms/Helpers.extractHttpData.phpt index 45645633f..56a12cfec 100644 --- a/tests/Forms/Helpers.extractHttpData.phpt +++ b/tests/Forms/Helpers.extractHttpData.phpt @@ -75,7 +75,6 @@ test('files', function () { 'multiple' => ['avatar' => [$file, $file]], ], 'multiple[avatar]', Form::DATA_FILE)); - Assert::equal([$file, $file], Helpers::extractHttpData([ 'multiple' => ['avatar' => ['x' => $file, null, $file]], ], 'multiple[avatar][]', Form::DATA_FILE));