Skip to content

Commit

Permalink
Merge pull request #37 from gsteel/validator-deprecations
Browse files Browse the repository at this point in the history
Deprecate runtime mutation of validator options
  • Loading branch information
gsteel authored Jul 12, 2024
2 parents 3f1271d + ed8743f commit 421107b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 86 deletions.
84 changes: 42 additions & 42 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions docs/book/validators/phone-number.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,18 @@ $options = [
$validator = new PhoneNumber($options);
```

## Runtime Modification of Options

CAUTION: **Deprecated**
Runtime mutation of options is deprecated and will be removed in version 2.0

Options can also be changed at runtime with:

```php
$validator = new Laminas\I18n\PhoneNumber\Validator\PhoneNumber();
$validator->setOptions($options);
```

## Runtime Modification of Options

Each of the 3 options have companion "setters" to change the runtime behaviour of the validator after it has been constructed:

### Set Country Code
Expand Down
54 changes: 33 additions & 21 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,75 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.8.0@9cf4f60a333f779ad3bc704a555920e81d4fdcda">
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
<file src="src/Filter/Factory/ToE164Factory.php">
<UnusedParam>
<code>$name</code>
<code><![CDATA[$name]]></code>
</UnusedParam>
</file>
<file src="src/Filter/Factory/ToInternationalPhoneNumberFactory.php">
<UnusedParam>
<code>$name</code>
<code><![CDATA[$name]]></code>
</UnusedParam>
</file>
<file src="src/Filter/Factory/ToNationalPhoneNumberFactory.php">
<UnusedParam>
<code>$name</code>
<code><![CDATA[$name]]></code>
</UnusedParam>
</file>
<file src="src/Form/Element/Factory/PhoneNumberFactory.php">
<UnusedParam>
<code>$requestedName</code>
<code><![CDATA[$requestedName]]></code>
</UnusedParam>
</file>
<file src="src/Module.php">
<UnusedClass>
<code>Module</code>
<code><![CDATA[Module]]></code>
</UnusedClass>
</file>
<file src="src/Validator/Factory/PhoneNumberFactory.php">
<UnusedParam>
<code>$requestedName</code>
<code><![CDATA[$requestedName]]></code>
</UnusedParam>
</file>
<file src="src/Validator/PhoneNumber.php">
<DeprecatedMethod>
<code><![CDATA[parent::setOptions($options)]]></code>
<code><![CDATA[setAllowedTypes]]></code>
<code><![CDATA[setCountry]]></code>
<code><![CDATA[setCountryContext]]></code>
</DeprecatedMethod>
</file>
<file src="test/Factory/ConfigurationTest.php">
<PossiblyUnusedMethod>
<code>nullConfigProvider</code>
<code><![CDATA[nullConfigProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/Filter/AbstractFilterTest.php">
<PossiblyUnusedMethod>
<code>filterClassProvider</code>
<code><![CDATA[filterClassProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/Filter/FilterPluginManagerIntegrationTest.php">
<PossiblyUnusedMethod>
<code>filterClassProvider</code>
<code><![CDATA[filterClassProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/Form/FormIntegrationTest.php">
<PossiblyUnusedMethod>
<code>validE164Provider</code>
<code><![CDATA[validE164Provider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/NumberGeneratorTrait.php">
<PossiblyUnusedMethod>
<code>invalidPhoneNumberProvider</code>
<code>invalidPhoneNumberProvider</code>
<code>invalidPhoneNumberProvider</code>
<code>validPhoneNumberProvider</code>
<code>validPhoneNumberProvider</code>
<code>validPhoneNumberProvider</code>
<code><![CDATA[invalidPhoneNumberProvider]]></code>
<code><![CDATA[invalidPhoneNumberProvider]]></code>
<code><![CDATA[invalidPhoneNumberProvider]]></code>
<code><![CDATA[validPhoneNumberProvider]]></code>
<code><![CDATA[validPhoneNumberProvider]]></code>
<code><![CDATA[validPhoneNumberProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/PhoneNumberValueTest.php">
<PossiblyUnusedMethod>
<code>internationalFormatsProvider</code>
<code><![CDATA[internationalFormatsProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/Validator/PhoneNumberTest.php">
<DeprecatedMethod>
<code><![CDATA[setOptions]]></code>
<code><![CDATA[setOptions]]></code>
</DeprecatedMethod>
<PossiblyUnusedMethod>
<code>invalidAllowedTypeProvider</code>
<code>invalidCountryProvider</code>
<code>invalidTypeProvider</code>
<code><![CDATA[invalidAllowedTypeProvider]]></code>
<code><![CDATA[invalidCountryProvider]]></code>
<code><![CDATA[invalidTypeProvider]]></code>
</PossiblyUnusedMethod>
</file>
</files>
20 changes: 17 additions & 3 deletions src/Validator/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function __construct($options = null)
}

/**
* @deprecated Since 1.2.0 Option setters will be removed in 2.0. Provide options to the constructor instead
*
* @param Options|iterable<string, mixed> $options
* @return $this
*/
Expand Down Expand Up @@ -156,7 +158,11 @@ public function isValid($value, ?array $context = null): bool
return true;
}

/** @param non-empty-string $countryCodeOrLocale */
/**
* @deprecated Since 1.2.0 Option setters will be removed in 2.0. Provide options to the constructor instead
*
* @param non-empty-string $countryCodeOrLocale
*/
public function setCountry(string $countryCodeOrLocale): void
{
$code = CountryCode::tryFromString($countryCodeOrLocale);
Expand All @@ -171,13 +177,21 @@ public function setCountry(string $countryCodeOrLocale): void
$this->country = $code;
}

/** @param non-empty-string $inputName */
/**
* @deprecated Since 1.2.0 Option setters will be removed in 2.0. Provide options to the constructor instead
*
* @param non-empty-string $inputName
*/
public function setCountryContext(string $inputName): void
{
$this->countryContext = $inputName;
}

/** @param int-mask-of<PhoneNumberValue::TYPE_*> $types */
/**
* @deprecated Since 1.2.0 Option setters will be removed in 2.0. Provide options to the constructor instead
*
* @param int-mask-of<PhoneNumberValue::TYPE_*> $types
*/
public function setAllowedTypes(int $types): void
{
if ($types <= 0 || ($types & PhoneNumberValue::TYPE_KNOWN) !== $types) {
Expand Down
Loading

0 comments on commit 421107b

Please sign in to comment.