Skip to content

Commit

Permalink
Add inline assertions for non-empty preg_match patterns
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Oct 9, 2023
1 parent ec0cf50 commit 5062ef1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
7 changes: 1 addition & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.13.1@086b94371304750d1c673315321a55d15fc59015">
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="src/Exception/ExtensionNotLoadedException.php">
<UnusedClass>
<code>ExtensionNotLoadedException</code>
Expand Down Expand Up @@ -533,9 +533,6 @@
<MixedArgument>
<code><![CDATA[$options['locale']]]></code>
</MixedArgument>
<PossiblyFalseArgument>
<code>$lastStringGroup</code>
</PossiblyFalseArgument>
</file>
<file src="src/Validator/IsInt.php">
<DocblockTypeContradiction>
Expand All @@ -560,8 +557,6 @@
<code><![CDATA[$options['allow_possible']]]></code>
<code><![CDATA[$options['allowed_types']]]></code>
<code><![CDATA[$options['country']]]></code>
<code>$valueNoCountry</code>
<code>$valueNoCountry</code>
</MixedArgument>
<MixedAssignment>
<code>static::$phone[$code]</code>
Expand Down
7 changes: 7 additions & 0 deletions src/Validator/IsFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use function is_float;
use function is_int;
use function is_scalar;
use function is_string;
use function preg_match;
use function preg_quote;
use function str_replace;
Expand Down Expand Up @@ -237,6 +238,12 @@ public function isValid($value)
$this->wrapper->substr($value, 0 - $groupSize) :
$value;

assert(is_string($lastStringGroup));
assert($lastStringGroup !== '');
assert($lnumSearch !== '');
assert($dnumSearch !== '');
assert($expDnumSearch !== '');

if (
(preg_match($lnumSearch, $unGroupedValue)
|| preg_match($dnumSearch, $unGroupedValue)
Expand Down
4 changes: 4 additions & 0 deletions src/Validator/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Traversable;

use function array_key_exists;
use function assert;
use function file_exists;
use function in_array;
use function is_scalar;
Expand Down Expand Up @@ -226,6 +227,7 @@ public function isValid($value = null, $context = null)
* 2) International double-O prefix
* 3) Bare country prefix
*/
$valueNoCountry = null;
if (0 === strpos((string) $value, '+' . $countryPattern['code'])) {
$valueNoCountry = substr((string) $value, $codeLength + 1);
} elseif (0 === strpos((string) $value, '00' . $countryPattern['code'])) {
Expand All @@ -236,6 +238,7 @@ public function isValid($value = null, $context = null)

// check against allowed types strict match:
foreach ($countryPattern['patterns']['national'] as $type => $pattern) {
assert($pattern !== '');
if (in_array($type, $this->allowedTypes, true)) {
// check pattern:
if (preg_match($pattern, (string) $value)) {
Expand All @@ -252,6 +255,7 @@ public function isValid($value = null, $context = null)
// check for possible match:
if ($this->allowPossible()) {
foreach ($countryPattern['patterns']['possible'] as $type => $pattern) {
assert($pattern !== '');
if (in_array($type, $this->allowedTypes, true)) {
// check pattern:
if (preg_match($pattern, (string) $value)) {
Expand Down

0 comments on commit 5062ef1

Please sign in to comment.