Skip to content

Commit

Permalink
added PHP 8 typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 8, 2024
1 parent 25019dc commit dffc07d
Show file tree
Hide file tree
Showing 28 changed files with 116 additions and 210 deletions.
8 changes: 5 additions & 3 deletions src/Forms/Blueprint.php
Expand Up @@ -9,6 +9,8 @@

namespace Nette\Forms;

use Nette\Utils\Html;


/**
* Generates blueprints for forms.
Expand Down Expand Up @@ -97,15 +99,15 @@ protected function receiveHttpData(): ?array
public $inner;


public function getLabel($caption = null)
public function getLabel($caption = null): Html|string|null
{
return $this->inner->getLabel()
? '{label ' . $this->inner->lookupPath(Form::class) . '/}'
: null;
}


public function getControl()
public function getControl(): Html|string
{
return '{input ' . $this->inner->lookupPath(Form::class) . '}';
}
Expand All @@ -117,7 +119,7 @@ public function isRequired(): bool
}


public function getOption($key)
public function getOption(mixed $key): mixed
{
return $key === 'rendered'
? parent::getOption($key)
Expand Down
35 changes: 11 additions & 24 deletions src/Forms/Container.php
Expand Up @@ -44,10 +44,8 @@ class Container extends Nette\ComponentModel\Container implements \ArrayAccess

/**
* Fill-in with default values.
* @param array|object $data
* @return static
*/
public function setDefaults($data, bool $erase = false)
public function setDefaults(array|object $data, bool $erase = false): static
{
$form = $this->getForm(false);
if (!$form || !$form->isAnchored() || !$form->isSubmitted()) {
Expand All @@ -60,20 +58,15 @@ public function setDefaults($data, bool $erase = false)

/**
* Fill-in with values.
* @param array|object $data
* @return static
* @internal
*/
public function setValues($data, bool $erase = false)
public function setValues(array|object $data, bool $erase = false): static
{
if ($data instanceof \Traversable) {
$values = iterator_to_array($data);

} elseif (is_object($data) || is_array($data) || $data === null) {
$values = (array) $data;

} else {
throw new Nette\InvalidArgumentException(sprintf('First parameter must be an array or object, %s given.', gettype($data)));
}

foreach ($this->getComponents() as $name => $control) {
Expand All @@ -85,7 +78,7 @@ public function setValues($data, bool $erase = false)
$control->setValue(null);
}
} elseif ($control instanceof self) {
if (array_key_exists($name, $values)) {
if (isset($values[$name])) {
$control->setValues($values[$name], $erase);

} elseif ($erase) {
Expand All @@ -100,11 +93,9 @@ public function setValues($data, bool $erase = false)

/**
* Returns the values submitted by the form.
* @param string|object|null $returnType 'array' for array
* @param Control[]|null $controls
* @return object|array
*/
public function getValues($returnType = null, ?array $controls = null)
public function getValues(string|object|bool|null $returnType = null, ?array $controls = null): object|array
{
$form = $this->getForm(false);
if ($form && ($submitter = $form->isSubmitted())) {
Expand Down Expand Up @@ -136,11 +127,9 @@ public function getValues($returnType = null, ?array $controls = null)

/**
* Returns the potentially unvalidated values submitted by the form.
* @param string|object|null $returnType 'array' for array
* @param Control[]|null $controls
* @return object|array
*/
public function getUntrustedValues($returnType = ArrayHash::class, ?array $controls = null)
public function getUntrustedValues(string|object|null $returnType, ?array $controls = null): object|array
{
if (is_object($returnType)) {
$obj = $returnType;
Expand Down Expand Up @@ -193,7 +182,7 @@ public function getUnsafeValues($returnType, ?array $controls = null)


/** @return static */
public function setMappedType(string $type)
public function setMappedType(string $type): static
{
$this->mappedType = $type;
return $this;
Expand Down Expand Up @@ -266,8 +255,7 @@ public function getErrors(): array
/********************* form building ****************d*g**/


/** @return static */
public function setCurrentGroup(?ControlGroup $group = null)
public function setCurrentGroup(?ControlGroup $group = null): static
{
$this->currentGroup = $group;
return $this;
Expand All @@ -285,14 +273,14 @@ public function getCurrentGroup(): ?ControlGroup

/**
* Adds the specified component to the IContainer.
* @return static
* @throws Nette\InvalidStateException
*/
public function addComponent(
Nette\ComponentModel\IComponent $component,
?string $name,
?string $insertBefore = null,
) {
): static
{
parent::addComponent($component, $name, $insertBefore);
if ($this->currentGroup !== null) {
$this->currentGroup->add($component);
Expand Down Expand Up @@ -567,9 +555,8 @@ public function addImage(): Controls\ImageButton

/**
* Adds naming container to the form.
* @param string|int $name
*/
public function addContainer($name): self
public function addContainer(string|int $name): self
{
$control = new self;
$control->currentGroup = $this->currentGroup;
Expand All @@ -584,7 +571,7 @@ public function addContainer($name): self
/********************* extension methods ****************d*g**/


public function __call(string $name, array $args)
public function __call(string $name, array $args): mixed
{
if (isset(self::$extMethods[$name])) {
return (self::$extMethods[$name])($this, ...$args);
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Control.php
Expand Up @@ -20,7 +20,7 @@ interface Control
* @param mixed $value
* @return static
*/
function setValue($value);
function setValue(mixed $value);

/**
* Returns control's value.
Expand Down
10 changes: 3 additions & 7 deletions src/Forms/ControlGroup.php
Expand Up @@ -27,8 +27,7 @@ public function __construct()
}


/** @return static */
public function add(...$items)
public function add(...$items): static
{
foreach ($items as $item) {
if ($item instanceof Control) {
Expand Down Expand Up @@ -82,10 +81,8 @@ public function getControls(): array
* - 'container' - container as Html object
* - 'description' - textual or Nette\HtmlStringable object description
* - 'embedNext' - describes how render next group
*
* @return static
*/
public function setOption(string $key, $value)
public function setOption(string $key, mixed $value): static
{
if ($value === null) {
unset($this->options[$key]);
Expand All @@ -100,9 +97,8 @@ public function setOption(string $key, $value)

/**
* Returns user-specific option.
* @return mixed
*/
public function getOption(string $key)
public function getOption(string $key): mixed
{
if (func_num_args() > 1) {
$default = func_get_arg(1);
Expand Down
52 changes: 18 additions & 34 deletions src/Forms/Controls/BaseControl.php
Expand Up @@ -21,7 +21,7 @@
*
* @property-read Form $form
* @property-read string $htmlName
* @property mixed $htmlId
* @property string|bool|null $htmlId
* @property mixed $value
* @property string|object $caption
* @property bool $disabled
Expand Down Expand Up @@ -80,9 +80,8 @@ public function __construct($caption = null)
/**
* Sets textual caption or label.
* @param object|string $caption
* @return static
*/
public function setCaption($caption)
public function setCaption($caption): static
{
$this->caption = $caption;
return $this;
Expand Down Expand Up @@ -117,9 +116,8 @@ public function loadHttpData(): void

/**
* Loads HTTP data.
* @return mixed
*/
protected function getHttpData($type, ?string $htmlTail = null)
protected function getHttpData($type, ?string $htmlTail = null): mixed
{
return $this->getForm()->getHttpData($type, $this->getHtmlName() . $htmlTail);
}
Expand All @@ -142,7 +140,7 @@ public function getHtmlName(): string
* @return static
* @internal
*/
public function setValue($value)
public function setValue(mixed $value)
{
$this->value = $value;
return $this;
Expand Down Expand Up @@ -186,10 +184,9 @@ public function setDefaultValue($value)

/**
* Disables or enables control.
* @param bool $value
* @return static
*/
public function setDisabled($value = true)
public function setDisabled(bool $value = true)
{
if ($this->disabled = (bool) $value) {
$this->setValue(null);
Expand All @@ -212,9 +209,8 @@ public function isDisabled(): bool

/**
* Sets whether control value is excluded from $form->getValues() result.
* @return static
*/
public function setOmitted(bool $value = true)
public function setOmitted(bool $value = true): static
{
$this->omitted = $value;
return $this;
Expand Down Expand Up @@ -299,10 +295,8 @@ public function getLabelPrototype(): Html

/**
* Changes control's HTML id.
* @param string|bool|null $id
* @return static
*/
public function setHtmlId($id)
public function setHtmlId(string|bool|null $id): static
{
$this->control->id = $id;
return $this;
Expand All @@ -311,9 +305,8 @@ public function setHtmlId($id)

/**
* Returns control's HTML id.
* @return mixed
*/
public function getHtmlId()
public function getHtmlId(): string|bool|null
{
if (!isset($this->control->id)) {
$form = $this->getForm();
Expand All @@ -329,9 +322,8 @@ public function getHtmlId()

/**
* Changes control's HTML attribute.
* @return static
*/
public function setHtmlAttribute(string $name, $value = true)
public function setHtmlAttribute(string $name, mixed $value = true): static
{
$this->control->$name = $value;
if (
Expand All @@ -350,9 +342,8 @@ public function setHtmlAttribute(string $name, $value = true)

/**
* @deprecated use setHtmlAttribute()
* @return static
*/
public function setAttribute(string $name, $value = true)
public function setAttribute(string $name, mixed $value = true): static
{
return $this->setHtmlAttribute($name, $value);
}
Expand All @@ -363,9 +354,8 @@ public function setAttribute(string $name, $value = true)

/**
* Sets translate adapter.
* @return static
*/
public function setTranslator(?Nette\Localization\Translator $translator)
public function setTranslator(?Nette\Localization\Translator $translator): static
{
$this->translator = $translator;
return $this;
Expand All @@ -389,9 +379,8 @@ public function getTranslator(): ?Nette\Localization\Translator

/**
* Returns translated string.
* @return mixed
*/
public function translate($value, ...$parameters)
public function translate($value, ...$parameters): mixed
{
if ($translator = $this->getTranslator()) {
$tmp = is_array($value) ? [&$value] : [[&$value]];
Expand All @@ -411,11 +400,10 @@ public function translate($value, ...$parameters)

/**
* Adds a validation rule.
* @param callable|string $validator
* @param string|object $errorMessage
* @return static
*/
public function addRule($validator, $errorMessage = null, $arg = null)
public function addRule(callable|string $validator, $errorMessage = null, mixed $arg = null)
{
$this->rules->addRule($validator, $errorMessage, $arg);
return $this;
Expand All @@ -442,9 +430,8 @@ public function addConditionOn(Control $control, $validator, $value = null): Rul

/**
* Adds an input filter callback.
* @return static
*/
public function addFilter(callable $filter)
public function addFilter(callable $filter): static
{
$this->getRules()->addFilter($filter);
return $this;
Expand All @@ -460,9 +447,8 @@ public function getRules(): Rules
/**
* Makes control mandatory.
* @param bool|string|object $value
* @return static
*/
public function setRequired($value = true)
public function setRequired($value = true): static
{
$this->rules->setRequired($value);
return $this;
Expand Down Expand Up @@ -537,9 +523,8 @@ public function cleanErrors(): void

/**
* Sets user-specific option.
* @return static
*/
public function setOption($key, $value)
public function setOption($key, mixed $value): static
{
if ($value === null) {
unset($this->options[$key]);
Expand All @@ -553,9 +538,8 @@ public function setOption($key, $value)

/**
* Returns user-specific option.
* @return mixed
*/
public function getOption($key)
public function getOption($key): mixed
{
if (func_num_args() > 1) {
$default = func_get_arg(1);
Expand All @@ -576,7 +560,7 @@ public function getOptions(): array
/********************* extension methods ****************d*g**/


public function __call(string $name, array $args)
public function __call(string $name, array $args): mixed
{
$class = static::class;
do {
Expand Down

0 comments on commit dffc07d

Please sign in to comment.