From 5062ef1b6e725eb2a05c56759ac241c1ef776cf2 Mon Sep 17 00:00:00 2001 From: George Steel Date: Mon, 9 Oct 2023 12:54:27 +0100 Subject: [PATCH] Add inline assertions for non-empty `preg_match` patterns Signed-off-by: George Steel --- psalm-baseline.xml | 7 +------ src/Validator/IsFloat.php | 7 +++++++ src/Validator/PhoneNumber.php | 4 ++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 9007c0d5..6a15f780 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + ExtensionNotLoadedException @@ -533,9 +533,6 @@ - - $lastStringGroup - @@ -560,8 +557,6 @@ - $valueNoCountry - $valueNoCountry static::$phone[$code] diff --git a/src/Validator/IsFloat.php b/src/Validator/IsFloat.php index bc25c458..0e662693 100644 --- a/src/Validator/IsFloat.php +++ b/src/Validator/IsFloat.php @@ -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; @@ -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) diff --git a/src/Validator/PhoneNumber.php b/src/Validator/PhoneNumber.php index 51f92ddc..473eed40 100644 --- a/src/Validator/PhoneNumber.php +++ b/src/Validator/PhoneNumber.php @@ -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; @@ -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'])) { @@ -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)) { @@ -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)) {