Skip to content

Commit

Permalink
coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2020
1 parent 282a0fa commit cfe069b
Show file tree
Hide file tree
Showing 22 changed files with 119 additions and 75 deletions.
26 changes: 13 additions & 13 deletions examples/custom-control.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}


Expand Down Expand Up @@ -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]',
Expand Down
8 changes: 4 additions & 4 deletions examples/manual-rendering.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@

<?php $form->render('begin') ?>

<?php if ($form->errors): ?>
<?php if ($form->errors) { ?>
<ul class="error">
<?php foreach ($form->errors as $error): ?>
<?php foreach ($form->errors as $error) { ?>
<li><?php echo htmlspecialchars($error) ?></li>
<?php endforeach ?>
<?php } ?>
</ul>
<?php endif ?>
<?php } ?>

<fieldset>
<legend>Personal data</legend>
Expand Down
8 changes: 6 additions & 2 deletions src/Bridges/FormsLatte/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -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;
Expand Down
16 changes: 12 additions & 4 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
Expand Down
14 changes: 11 additions & 3 deletions src/Forms/Controls/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
]);
}
}
8 changes: 3 additions & 5 deletions src/Forms/Controls/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 6 additions & 6 deletions src/Forms/Controls/ChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/CsrfProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 6 additions & 2 deletions src/Forms/Controls/TextBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/Forms/Controls/TextInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
12 changes: 5 additions & 7 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
}
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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;
}

Expand Down
24 changes: 18 additions & 6 deletions src/Forms/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 = '';
Expand Down
10 changes: 8 additions & 2 deletions src/Forms/Rendering/DefaultFormRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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') {
Expand Down
22 changes: 14 additions & 8 deletions src/Forms/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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])) {
Expand All @@ -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}'.");
}
}
Expand All @@ -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;
}
}
Loading

0 comments on commit cfe069b

Please sign in to comment.