Skip to content

Commit

Permalink
Refactor Hostname validator
Browse files Browse the repository at this point in the history
- Only allow a well known array shape as options for the constructor
- Add types everywhere
- Replace internal options array with RO props
- Remove option setters and getters

Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Jun 30, 2024
1 parent 31fc753 commit 63c2e2b
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 290 deletions.
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]);

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 @@ -194,9 +194,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 @@ -216,7 +213,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 @@ -1008,59 +1005,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">
<DocblockTypeContradiction>
Expand Down Expand Up @@ -1409,8 +1359,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 @@ -1724,25 +1672,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

0 comments on commit 63c2e2b

Please sign in to comment.