Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Hostname validator #298

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions docs/book/v3/validators/hostname.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ The following options are supported for `Laminas\Validator\Hostname`:
[See below](#validating-different-types-of-hostnames) for details.
- `idn`: Defines if IDN domains are allowed or not. This option defaults to
`true`.
- `ip`: Allows defining a custom IP validator. This option defaults to a new
instance of `Laminas\Validator\Ip`.
- `ipValidator`: Allows defining an [IP validator](ip.md) with custom configuration
- `tld`: Defines if TLDs are validated. This option defaults to `true`.

## Basic usage
Expand Down Expand Up @@ -63,7 +62,7 @@ To check for IP addresses only, you can use the example below:
```php
use Laminas\Validator\Hostname;

$validator = new Hostname(Hostname::ALLOW_IP);
$validator = new Hostname(['allow' => Hostname::ALLOW_IP]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the single parameter approach still working?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, only an options array is accepted


if ($validator->isValid($hostname)) {
// hostname appears to be valid
Expand All @@ -82,7 +81,9 @@ Local hostnames:
```php
use Laminas\Validator\Hostname;

$validator = new Hostname(Hostname::ALLOW_DNS | Hostname::ALLOW_IP);
$validator = new Hostname([
'allow' => Hostname::ALLOW_DNS | Hostname::ALLOW_IP,
]);
```

## Validating International Domains Names
Expand All @@ -104,7 +105,7 @@ You can disable IDN validation by passing a second parameter to the
```php
$validator = new Laminas\Validator\Hostname([
'allow' => Laminas\Validator\Hostname::ALLOW_DNS,
'useIdnCheck' => false,
'useIdnCheck' => false,
]);
```

Expand Down
73 changes: 1 addition & 72 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@
<code><![CDATA['']]></code>
<code><![CDATA[is_string($value)]]></code>
</DocblockTypeContradiction>
<InvalidArgument>
<code><![CDATA[$this->getAllow()]]></code>
</InvalidArgument>
<LessSpecificImplementedReturnType>
<code><![CDATA[AbstractValidator]]></code>
</LessSpecificImplementedReturnType>
Expand All @@ -198,7 +195,7 @@
<code><![CDATA[bool]]></code>
<code><![CDATA[bool]]></code>
<code><![CDATA[bool]]></code>
<code><![CDATA[int]]></code>
<code><![CDATA[int-mask-of<Hostname::ALLOW_*>]]></code>
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[setAllow]]></code>
Expand Down Expand Up @@ -987,59 +984,12 @@
</MixedArgument>
</file>
<file src="src/Hostname.php">
<DocblockTypeContradiction>
<code><![CDATA[is_array($options)]]></code>
<code><![CDATA[is_string($value)]]></code>
</DocblockTypeContradiction>
<MixedArgument>
<code><![CDATA[$regexChar]]></code>
<code><![CDATA[$regexKey]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$partRegexChars]]></code>
<code><![CDATA[$regexChar]]></code>
<code><![CDATA[$regexChars]]></code>
<code><![CDATA[$regexKey]]></code>
<code><![CDATA[$temp['allow']]]></code>
<code><![CDATA[$temp['ipValidator']]]></code>
<code><![CDATA[$temp['useIdnCheck']]]></code>
<code><![CDATA[$temp['useTldCheck']]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[Ip]]></code>
<code><![CDATA[bool]]></code>
<code><![CDATA[bool]]></code>
<code><![CDATA[int]]></code>
</MixedInferredReturnType>
<MixedOperand>
<code><![CDATA[$regexChars]]></code>
</MixedOperand>
<MixedReturnStatement>
<code><![CDATA[$this->options['allow']]]></code>
<code><![CDATA[$this->options['ipValidator']]]></code>
<code><![CDATA[$this->options['useIdnCheck']]]></code>
<code><![CDATA[$this->options['useTldCheck']]]></code>
</MixedReturnStatement>
<MoreSpecificImplementedParamType>
<code><![CDATA[$value]]></code>
</MoreSpecificImplementedParamType>
<PossiblyInvalidArgument>
<code><![CDATA[$options]]></code>
</PossiblyInvalidArgument>
<PossiblyUndefinedVariable>
<code><![CDATA[$temp]]></code>
</PossiblyUndefinedVariable>
<PossiblyUnusedMethod>
<code><![CDATA[setAllow]]></code>
<code><![CDATA[setIpValidator]]></code>
</PossiblyUnusedMethod>
<RedundantCastGivenDocblockType>
<code><![CDATA[(bool) $useIdnCheck]]></code>
<code><![CDATA[(bool) $useTldCheck]]></code>
</RedundantCastGivenDocblockType>
<UnresolvableInclude>
<code><![CDATA[include __DIR__ . '/' . $this->validIdns[$this->tld]]]></code>
</UnresolvableInclude>
</file>
<file src="src/Iban.php">
<MixedArgument>
Expand Down Expand Up @@ -1305,8 +1255,6 @@
</file>
<file src="test/EmailAddressTest.php">
<InvalidArgument>
<code><![CDATA[Hostname::ALLOW_ALL]]></code>
<code><![CDATA[Hostname::ALLOW_ALL]]></code>
<code><![CDATA[Hostname::ALLOW_ALL]]></code>
<code><![CDATA[Hostname::ALLOW_ALL]]></code>
<code><![CDATA[Hostname::ALLOW_ALL]]></code>
Expand Down Expand Up @@ -1617,25 +1565,6 @@
</PossiblyUnusedMethod>
</file>
<file src="test/HostnameTest.php">
<InvalidArgument>
<code><![CDATA[$option]]></code>
<code><![CDATA[$option]]></code>
<code><![CDATA[$option]]></code>
<code><![CDATA[$option]]></code>
<code><![CDATA[Hostname::ALLOW_ALL]]></code>
<code><![CDATA[Hostname::ALLOW_ALL]]></code>
<code><![CDATA[Hostname::ALLOW_ALL]]></code>
<code><![CDATA[Hostname::ALLOW_ALL]]></code>
<code><![CDATA[Hostname::ALLOW_ALL]]></code>
<code><![CDATA[Hostname::ALLOW_DNS]]></code>
<code><![CDATA[Hostname::ALLOW_DNS]]></code>
<code><![CDATA[Hostname::ALLOW_DNS]]></code>
<code><![CDATA[Hostname::ALLOW_URI]]></code>
<code><![CDATA[[1 => 1]]]></code>
</InvalidArgument>
<InvalidCast>
<code><![CDATA[[1 => 1]]]></code>
</InvalidCast>
<MixedArrayOffset>
<code><![CDATA[$translations[$code]]]></code>
</MixedArrayOffset>
Expand Down
18 changes: 14 additions & 4 deletions src/EmailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@

use const INTL_IDNA_VARIANT_UTS46;

/**
* @psalm-type Options = array{
* useMxCheck?: bool,
* useDeepMxCheck?: bool,
* useDomainCheck?: bool,
* allow?: int-mask-of<Hostname::ALLOW_*>,
* strict?: bool,
* hostnameValidator?: Hostname|null,
* }
*/
final class EmailAddress extends AbstractValidator
{
public const INVALID = 'emailAddressInvalid';
Expand Down Expand Up @@ -81,7 +91,7 @@ final class EmailAddress extends AbstractValidator
/**
* Internal options array
*
* @var array<string, mixed>
* @var Options
*/
protected $options = [
'useMxCheck' => false,
Expand Down Expand Up @@ -158,7 +168,7 @@ public function setMessage($messageString, $messageKey = null)
public function getHostnameValidator()
{
if (! isset($this->options['hostnameValidator'])) {
$this->options['hostnameValidator'] = new Hostname($this->getAllow());
$this->options['hostnameValidator'] = new Hostname(['allow' => $this->getAllow()]);
}

return $this->options['hostnameValidator'];
Expand All @@ -178,7 +188,7 @@ public function setHostnameValidator(?Hostname $hostnameValidator = null)
/**
* Returns the allow option of the attached hostname validator
*
* @return int
* @return int-mask-of<Hostname::ALLOW_*>
*/
public function getAllow()
{
Expand All @@ -188,7 +198,7 @@ public function getAllow()
/**
* Sets the allow option of the hostname validator to use
*
* @param int $allow
* @param int-mask-of<Hostname::ALLOW_*> $allow
* @return $this Provides a fluent interface
*/
public function setAllow($allow)
Expand Down
Loading