From 2757bb1012c5d0c2aba516fe7a3bbdd68335c46f Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Thu, 14 Mar 2024 03:44:38 +0400 Subject: [PATCH] Refactor code to remove potential nullability issues Updated method signatures across codebase to remove nullable string types for validation input. These changes ensure consistency in handling input values from CSV files. The prior handling could potentially lead to null value related errors, thus this update improves the overall stability of the CSV validation process. --- src/Rules/AbstarctRule.php | 4 ++-- src/Rules/AllMustContain.php | 4 ++-- src/Rules/AllowValues.php | 2 +- src/Rules/AtLeastContains.php | 4 ++-- src/Rules/CardinalDirection.php | 4 ++-- src/Rules/DateFormat.php | 4 ++-- src/Rules/ExactValue.php | 4 ++-- src/Rules/IsBool.php | 4 ++-- src/Rules/IsDomain.php | 4 ++-- src/Rules/IsEmail.php | 2 +- src/Rules/IsFloat.php | 4 ++-- src/Rules/IsInt.php | 4 ++-- src/Rules/IsIp.php | 2 +- src/Rules/IsLatitude.php | 2 +- src/Rules/IsLongitude.php | 2 +- src/Rules/IsUrl.php | 2 +- src/Rules/IsUuid4.php | 4 ++-- src/Rules/Max.php | 2 +- src/Rules/MaxDate.php | 4 ++-- src/Rules/MaxLength.php | 4 ++-- src/Rules/MaxPrecision.php | 2 +- src/Rules/MaxWordCount.php | 4 ++-- src/Rules/Min.php | 2 +- src/Rules/MinDate.php | 4 ++-- src/Rules/MinLength.php | 4 ++-- src/Rules/MinPrecision.php | 2 +- src/Rules/MinWordCount.php | 4 ++-- src/Rules/NotEmpty.php | 4 ++-- src/Rules/OnlyCapitalize.php | 4 ++-- src/Rules/OnlyLowercase.php | 4 ++-- src/Rules/OnlyTrimed.php | 4 ++-- src/Rules/OnlyUppercase.php | 4 ++-- src/Rules/Precision.php | 10 ++++------ src/Rules/Regex.php | 4 ++-- src/Rules/StrEndsWith.php | 4 ++-- src/Rules/StrStartsWith.php | 4 ++-- src/Rules/UsaMarketName.php | 4 ++-- src/Rules/WordCount.php | 4 ++-- src/Validators/ColumnValidator.php | 2 +- src/Validators/Ruleset.php | 2 +- tests/Blueprint/RulesTest.php | 6 +----- 41 files changed, 71 insertions(+), 77 deletions(-) diff --git a/src/Rules/AbstarctRule.php b/src/Rules/AbstarctRule.php index dd827158..8da530e0 100644 --- a/src/Rules/AbstarctRule.php +++ b/src/Rules/AbstarctRule.php @@ -33,7 +33,7 @@ abstract class AbstarctRule private string $columnNameId; private string $ruleCode; - abstract public function validateRule(?string $cellValue): ?string; + abstract public function validateRule(string $cellValue): ?string; public function __construct(string $columnNameId, null|array|bool|float|int|string $options = null) { @@ -42,7 +42,7 @@ public function __construct(string $columnNameId, null|array|bool|float|int|stri $this->ruleCode = $this->getRuleCode(); } - public function validate(?string $cellValue, int $line = 0): ?Error + public function validate(string $cellValue, int $line = 0): ?Error { $error = $this->validateRule($cellValue); if ($error !== null) { diff --git a/src/Rules/AllMustContain.php b/src/Rules/AllMustContain.php index 507d97a3..e400241f 100644 --- a/src/Rules/AllMustContain.php +++ b/src/Rules/AllMustContain.php @@ -18,7 +18,7 @@ final class AllMustContain extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $inclusions = $this->getOptionAsArray(); if (\count($inclusions) === 0) { @@ -26,7 +26,7 @@ public function validateRule(?string $cellValue): ?string } foreach ($inclusions as $inclusion) { - if (\strpos((string)$cellValue, (string)$inclusion) === false) { + if (\strpos($cellValue, (string)$inclusion) === false) { return "Value \"{$cellValue}\" must contain all of the following:" . ' "["' . \implode('", "', $inclusions) . '"]"'; } diff --git a/src/Rules/AllowValues.php b/src/Rules/AllowValues.php index 6541d2e5..c2f602c5 100644 --- a/src/Rules/AllowValues.php +++ b/src/Rules/AllowValues.php @@ -18,7 +18,7 @@ class AllowValues extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $allowedValues = $this->getOptionAsArray(); diff --git a/src/Rules/AtLeastContains.php b/src/Rules/AtLeastContains.php index 5fec1baa..694d325d 100644 --- a/src/Rules/AtLeastContains.php +++ b/src/Rules/AtLeastContains.php @@ -18,7 +18,7 @@ final class AtLeastContains extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $inclusions = $this->getOptionAsArray(); if (\count($inclusions) === 0) { @@ -26,7 +26,7 @@ public function validateRule(?string $cellValue): ?string } foreach ($inclusions as $inclusion) { - if (\strpos((string)$cellValue, (string)$inclusion) !== false) { + if (\strpos($cellValue, (string)$inclusion) !== false) { return null; } } diff --git a/src/Rules/CardinalDirection.php b/src/Rules/CardinalDirection.php index 751dffe6..60ca6d92 100644 --- a/src/Rules/CardinalDirection.php +++ b/src/Rules/CardinalDirection.php @@ -18,13 +18,13 @@ final class CardinalDirection extends AllowValues { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; } - return parent::validateRule((string)$cellValue); + return parent::validateRule($cellValue); } public function getOptionAsArray(): array diff --git a/src/Rules/DateFormat.php b/src/Rules/DateFormat.php index 49bdfefe..232e1046 100644 --- a/src/Rules/DateFormat.php +++ b/src/Rules/DateFormat.php @@ -18,14 +18,14 @@ final class DateFormat extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $expectedDateFormat = $this->getOptionAsString(); if ($expectedDateFormat === '') { return 'Date format is not defined'; } - if ($cellValue === null || $cellValue === '') { + if ($cellValue === '') { return 'Date format of value "" is not valid. Expected format: "' . $expectedDateFormat . '"'; } diff --git a/src/Rules/ExactValue.php b/src/Rules/ExactValue.php index 1914320a..2c3aa3d9 100644 --- a/src/Rules/ExactValue.php +++ b/src/Rules/ExactValue.php @@ -18,9 +18,9 @@ final class ExactValue extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { - if ($this->getOptionAsString() !== (string)$cellValue) { + if ($this->getOptionAsString() !== $cellValue) { return "Value \"{$cellValue}\" is not strict equal to " . "\"{$this->getOptionAsString()}\""; } diff --git a/src/Rules/IsBool.php b/src/Rules/IsBool.php index 51a59dc8..f1c08a79 100644 --- a/src/Rules/IsBool.php +++ b/src/Rules/IsBool.php @@ -18,13 +18,13 @@ final class IsBool extends AllowValues { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; } - return parent::validateRule(\strtolower((string)$cellValue)); + return parent::validateRule(\strtolower($cellValue)); } public function getOptionAsArray(): array diff --git a/src/Rules/IsDomain.php b/src/Rules/IsDomain.php index ac3628e4..1234350e 100644 --- a/src/Rules/IsDomain.php +++ b/src/Rules/IsDomain.php @@ -18,7 +18,7 @@ final class IsDomain extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; @@ -26,7 +26,7 @@ public function validateRule(?string $cellValue): ?string $domainPattern = '/^(?!-)[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,})$/'; - if (\preg_match($domainPattern, (string)$cellValue) === 0) { + if (\preg_match($domainPattern, $cellValue) === 0) { return "Value \"{$cellValue}\" is not a valid domain"; } diff --git a/src/Rules/IsEmail.php b/src/Rules/IsEmail.php index 3d812588..d59fbb95 100644 --- a/src/Rules/IsEmail.php +++ b/src/Rules/IsEmail.php @@ -18,7 +18,7 @@ final class IsEmail extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; diff --git a/src/Rules/IsFloat.php b/src/Rules/IsFloat.php index a4bb73e7..ab2f8402 100644 --- a/src/Rules/IsFloat.php +++ b/src/Rules/IsFloat.php @@ -18,13 +18,13 @@ class IsFloat extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; } - if (\preg_match('/^-?\d+(\.\d+)?$/', (string)$cellValue) === 0) { + if (\preg_match('/^-?\d+(\.\d+)?$/', $cellValue) === 0) { return "Value \"{$cellValue}\" is not a float number"; } diff --git a/src/Rules/IsInt.php b/src/Rules/IsInt.php index 792369d9..478376b8 100644 --- a/src/Rules/IsInt.php +++ b/src/Rules/IsInt.php @@ -18,13 +18,13 @@ final class IsInt extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; } - if (\preg_match('/^-?\d+$/', (string)$cellValue) === 0) { + if (\preg_match('/^-?\d+$/', $cellValue) === 0) { return "Value \"{$cellValue}\" is not an integer"; } diff --git a/src/Rules/IsIp.php b/src/Rules/IsIp.php index 437a000c..6ed5cf1e 100644 --- a/src/Rules/IsIp.php +++ b/src/Rules/IsIp.php @@ -18,7 +18,7 @@ final class IsIp extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; diff --git a/src/Rules/IsLatitude.php b/src/Rules/IsLatitude.php index 67ba570e..27b19b53 100644 --- a/src/Rules/IsLatitude.php +++ b/src/Rules/IsLatitude.php @@ -21,7 +21,7 @@ final class IsLatitude extends IsFloat private float $min = -90.0; private float $max = 90.0; - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; diff --git a/src/Rules/IsLongitude.php b/src/Rules/IsLongitude.php index 542d8503..0188bf5f 100644 --- a/src/Rules/IsLongitude.php +++ b/src/Rules/IsLongitude.php @@ -21,7 +21,7 @@ final class IsLongitude extends IsFloat private float $min = -180.0; private float $max = 180.0; - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; diff --git a/src/Rules/IsUrl.php b/src/Rules/IsUrl.php index fc072f9d..d57580c9 100644 --- a/src/Rules/IsUrl.php +++ b/src/Rules/IsUrl.php @@ -18,7 +18,7 @@ final class IsUrl extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; diff --git a/src/Rules/IsUuid4.php b/src/Rules/IsUuid4.php index f47fd5bd..37c35fab 100644 --- a/src/Rules/IsUuid4.php +++ b/src/Rules/IsUuid4.php @@ -18,7 +18,7 @@ final class IsUuid4 extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; @@ -26,7 +26,7 @@ public function validateRule(?string $cellValue): ?string $uuid4 = '/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89ABab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/'; - if (\preg_match($uuid4, (string)$cellValue) === 0) { + if (\preg_match($uuid4, $cellValue) === 0) { return 'Value is not a valid UUID v4'; } diff --git a/src/Rules/Max.php b/src/Rules/Max.php index eb96587a..b99d1f72 100644 --- a/src/Rules/Max.php +++ b/src/Rules/Max.php @@ -18,7 +18,7 @@ final class Max extends IsFloat { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $result = parent::validateRule($cellValue); if ($result !== null) { diff --git a/src/Rules/MaxDate.php b/src/Rules/MaxDate.php index 504df30f..834c7471 100644 --- a/src/Rules/MaxDate.php +++ b/src/Rules/MaxDate.php @@ -18,10 +18,10 @@ final class MaxDate extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $minDate = $this->getOptionAsDate(); - $cellDate = new \DateTimeImmutable((string)$cellValue); + $cellDate = new \DateTimeImmutable($cellValue); if ($cellDate->getTimestamp() > $minDate->getTimestamp()) { return "Value \"{$cellValue}\" is more than the maximum " . diff --git a/src/Rules/MaxLength.php b/src/Rules/MaxLength.php index 5d18a6d8..e34ff02b 100644 --- a/src/Rules/MaxLength.php +++ b/src/Rules/MaxLength.php @@ -18,10 +18,10 @@ final class MaxLength extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $minLength = $this->getOptionAsInt(); - $length = \mb_strlen((string)$cellValue); + $length = \mb_strlen($cellValue); if ($length > $minLength) { return "Value \"{$cellValue}\" (length: {$length}) is too long. " . diff --git a/src/Rules/MaxPrecision.php b/src/Rules/MaxPrecision.php index 35e8a65c..1dea055f 100644 --- a/src/Rules/MaxPrecision.php +++ b/src/Rules/MaxPrecision.php @@ -18,7 +18,7 @@ final class MaxPrecision extends Precision { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $valuePrecision = self::getFloatPrecision($cellValue); diff --git a/src/Rules/MaxWordCount.php b/src/Rules/MaxWordCount.php index d7ea0734..99925603 100644 --- a/src/Rules/MaxWordCount.php +++ b/src/Rules/MaxWordCount.php @@ -18,10 +18,10 @@ final class MaxWordCount extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $wordCount = $this->getOptionAsInt(); - $count = \str_word_count((string)$cellValue); + $count = \str_word_count($cellValue); if ($count > $wordCount) { return "Value \"{$cellValue}\" has {$count} words, " . diff --git a/src/Rules/Min.php b/src/Rules/Min.php index c6c2cfda..33e7271c 100644 --- a/src/Rules/Min.php +++ b/src/Rules/Min.php @@ -18,7 +18,7 @@ final class Min extends IsFloat { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $result = parent::validateRule($cellValue); if ($result !== null) { diff --git a/src/Rules/MinDate.php b/src/Rules/MinDate.php index 3c4ae330..174f73d9 100644 --- a/src/Rules/MinDate.php +++ b/src/Rules/MinDate.php @@ -18,10 +18,10 @@ final class MinDate extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $minDate = $this->getOptionAsDate(); - $cellDate = new \DateTimeImmutable((string)$cellValue); + $cellDate = new \DateTimeImmutable($cellValue); if ($cellDate->getTimestamp() < $minDate->getTimestamp()) { return "Value \"{$cellValue}\" is less than the minimum " . diff --git a/src/Rules/MinLength.php b/src/Rules/MinLength.php index bb7b9957..0d6b6fe0 100644 --- a/src/Rules/MinLength.php +++ b/src/Rules/MinLength.php @@ -18,10 +18,10 @@ final class MinLength extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $minLength = $this->getOptionAsInt(); - $length = \mb_strlen((string)$cellValue); + $length = \mb_strlen($cellValue); if ($length < $minLength) { return "Value \"{$cellValue}\" (length: {$length}) is too short. " . diff --git a/src/Rules/MinPrecision.php b/src/Rules/MinPrecision.php index 8af2d12d..90ceda3e 100644 --- a/src/Rules/MinPrecision.php +++ b/src/Rules/MinPrecision.php @@ -18,7 +18,7 @@ final class MinPrecision extends Precision { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $valuePrecision = self::getFloatPrecision($cellValue); diff --git a/src/Rules/MinWordCount.php b/src/Rules/MinWordCount.php index 1c80f4d2..a7fe08a8 100644 --- a/src/Rules/MinWordCount.php +++ b/src/Rules/MinWordCount.php @@ -18,10 +18,10 @@ final class MinWordCount extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $wordCount = $this->getOptionAsInt(); - $count = \str_word_count((string)$cellValue); + $count = \str_word_count($cellValue); if ($count < $wordCount) { return "Value \"{$cellValue}\" has {$count} words, " . diff --git a/src/Rules/NotEmpty.php b/src/Rules/NotEmpty.php index 6a936c8d..cfa623ca 100644 --- a/src/Rules/NotEmpty.php +++ b/src/Rules/NotEmpty.php @@ -18,13 +18,13 @@ final class NotEmpty extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; } - if ($cellValue === null || $cellValue === '') { + if ($cellValue === '') { return 'Value is empty'; } diff --git a/src/Rules/OnlyCapitalize.php b/src/Rules/OnlyCapitalize.php index f426f3fb..e25f747d 100644 --- a/src/Rules/OnlyCapitalize.php +++ b/src/Rules/OnlyCapitalize.php @@ -18,13 +18,13 @@ final class OnlyCapitalize extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; } - if ($cellValue !== null && $cellValue !== \ucfirst($cellValue)) { + if ($cellValue !== \ucfirst($cellValue)) { return "Value \"{$cellValue}\" should be in capitalize"; } diff --git a/src/Rules/OnlyLowercase.php b/src/Rules/OnlyLowercase.php index a5fe11e6..cf36c7f7 100644 --- a/src/Rules/OnlyLowercase.php +++ b/src/Rules/OnlyLowercase.php @@ -18,13 +18,13 @@ final class OnlyLowercase extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; } - if ($cellValue !== null && $cellValue !== \mb_strtolower($cellValue)) { + if ($cellValue !== \mb_strtolower($cellValue)) { return "Value \"{$cellValue}\" should be in lowercase"; } diff --git a/src/Rules/OnlyTrimed.php b/src/Rules/OnlyTrimed.php index f55053b9..d8621755 100644 --- a/src/Rules/OnlyTrimed.php +++ b/src/Rules/OnlyTrimed.php @@ -18,13 +18,13 @@ final class OnlyTrimed extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; } - if (\trim((string)$cellValue) !== (string)$cellValue) { + if (\trim($cellValue) !== $cellValue) { return "Value \"{$cellValue}\" is not trimmed"; } diff --git a/src/Rules/OnlyUppercase.php b/src/Rules/OnlyUppercase.php index 839fe5d4..a1034e5e 100644 --- a/src/Rules/OnlyUppercase.php +++ b/src/Rules/OnlyUppercase.php @@ -18,13 +18,13 @@ final class OnlyUppercase extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; } - if ($cellValue !== null && \mb_strtoupper($cellValue, 'UTF-8') !== $cellValue) { + if (\mb_strtoupper($cellValue, 'UTF-8') !== $cellValue) { return "Value \"{$cellValue}\" is not uppercase"; } diff --git a/src/Rules/Precision.php b/src/Rules/Precision.php index 25b6b816..697a5b80 100644 --- a/src/Rules/Precision.php +++ b/src/Rules/Precision.php @@ -18,7 +18,7 @@ class Precision extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $valuePrecision = self::getFloatPrecision($cellValue); @@ -30,15 +30,13 @@ public function validateRule(?string $cellValue): ?string return null; } - protected static function getFloatPrecision(?string $cellValue): int + protected static function getFloatPrecision(string $cellValue): int { - $floatAsString = (string)$cellValue; - $dotPosition = \strpos($floatAsString, '.'); - + $dotPosition = \strpos($cellValue, '.'); if ($dotPosition === false) { return 0; } - return \strlen($floatAsString) - $dotPosition - 1; + return \strlen($cellValue) - $dotPosition - 1; } } diff --git a/src/Rules/Regex.php b/src/Rules/Regex.php index 95e2be6e..f07ca4d8 100644 --- a/src/Rules/Regex.php +++ b/src/Rules/Regex.php @@ -20,7 +20,7 @@ final class Regex extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $regex = Utils::prepareRegex($this->getOptionAsString()); @@ -28,7 +28,7 @@ public function validateRule(?string $cellValue): ?string return 'Regex pattern is not defined'; } - if (\preg_match($regex, (string)$cellValue) === 0) { + if (\preg_match($regex, $cellValue) === 0) { return "Value \"{$cellValue}\" does not match the pattern \"{$regex}\""; } diff --git a/src/Rules/StrEndsWith.php b/src/Rules/StrEndsWith.php index 639e742a..a8cbb129 100644 --- a/src/Rules/StrEndsWith.php +++ b/src/Rules/StrEndsWith.php @@ -18,14 +18,14 @@ final class StrEndsWith extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $prefix = $this->getOptionAsString(); if ($prefix === '') { return null; } - if (!\str_ends_with((string)$cellValue, $prefix)) { + if (!\str_ends_with($cellValue, $prefix)) { return "Value \"{$cellValue}\" must end with \"{$prefix}\""; } diff --git a/src/Rules/StrStartsWith.php b/src/Rules/StrStartsWith.php index 50a22f09..5295bf1d 100644 --- a/src/Rules/StrStartsWith.php +++ b/src/Rules/StrStartsWith.php @@ -18,14 +18,14 @@ final class StrStartsWith extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $prefix = $this->getOptionAsString(); if ($prefix === '') { return null; } - if (!\str_starts_with((string)$cellValue, $prefix)) { + if (!\str_starts_with($cellValue, $prefix)) { return "Value \"{$cellValue}\" must start with \"{$prefix}\""; } diff --git a/src/Rules/UsaMarketName.php b/src/Rules/UsaMarketName.php index 786f61c8..0626d44e 100644 --- a/src/Rules/UsaMarketName.php +++ b/src/Rules/UsaMarketName.php @@ -18,13 +18,13 @@ final class UsaMarketName extends AllowValues { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { if (!$this->getOptionAsBool()) { return null; } - if (\preg_match('/^[A-Za-z0-9\s-]+, [A-Z]{2}$/u', (string)$cellValue) === 0) { + if (\preg_match('/^[A-Za-z0-9\s-]+, [A-Z]{2}$/u', $cellValue) === 0) { return "Invalid market name format for value \"{$cellValue}\". " . 'Market name must have format "New York, NY"'; } diff --git a/src/Rules/WordCount.php b/src/Rules/WordCount.php index 2dbc66c8..dc020c6f 100644 --- a/src/Rules/WordCount.php +++ b/src/Rules/WordCount.php @@ -18,10 +18,10 @@ final class WordCount extends AbstarctRule { - public function validateRule(?string $cellValue): ?string + public function validateRule(string $cellValue): ?string { $wordCount = $this->getOptionAsInt(); - $count = \str_word_count((string)$cellValue); + $count = \str_word_count($cellValue); if ($count !== $wordCount) { return "Value \"{$cellValue}\" has {$count} words, " . diff --git a/src/Validators/ColumnValidator.php b/src/Validators/ColumnValidator.php index 929f7b47..be91b832 100644 --- a/src/Validators/ColumnValidator.php +++ b/src/Validators/ColumnValidator.php @@ -27,7 +27,7 @@ public function __construct(Column $column) $this->ruleset = new Ruleset($column->getRules(), $column->getHumanName()); } - public function validate(?string $cellValue, int $line): ErrorSuite + public function validate(string $cellValue, int $line): ErrorSuite { return $this->ruleset->validate($cellValue, $line); } diff --git a/src/Validators/Ruleset.php b/src/Validators/Ruleset.php index 4953f84f..9bccd8c0 100644 --- a/src/Validators/Ruleset.php +++ b/src/Validators/Ruleset.php @@ -50,7 +50,7 @@ public function createRule(string $ruleName, null|array|bool|float|int|string $o throw new Exception("Rule \"{$ruleName}\" not found. Expected class: \"{$classname}\""); } - public function validate(?string $cellValue, int $line): ErrorSuite + public function validate(string $cellValue, int $line): ErrorSuite { $errors = new ErrorSuite(); diff --git a/tests/Blueprint/RulesTest.php b/tests/Blueprint/RulesTest.php index 1074fb18..cdee6fc3 100644 --- a/tests/Blueprint/RulesTest.php +++ b/tests/Blueprint/RulesTest.php @@ -465,13 +465,9 @@ public function testNotEmpty(): void '"not_empty" at line 0, column "prop". Value is empty.', \strip_tags((string)$rule->validate('')), ); - isSame( - '"not_empty" at line 0, column "prop". Value is empty.', - \strip_tags((string)$rule->validate(null)), - ); $rule = new NotEmpty('prop', false); - isSame(null, $rule->validate(null)); + isSame(null, $rule->validate('')); } public function testOnlyCapitalize(): void