From ec96c1d8ddeafda2b6a18954915e9e80ea09ff07 Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Thu, 14 Mar 2024 12:17:06 +0400 Subject: [PATCH] Refactor string validation rules and terminal width determination String validation rules 'str_starts_with' and 'str_ends_with' were renamed to 'starts_with' and 'ends_with' for brevity and consistency. Also, a new utilitarian method named 'autoDetectTerminalWidth' was introduced to accurately estimate terminal width based on the execution environment. The code now takes into account wider terminal widths in Docker and GitHub actions environments. --- Dockerfile | 3 --- README.md | 4 ++-- schema-examples/full.json | 4 ++-- schema-examples/full.php | 4 ++-- schema-examples/full.yml | 4 ++-- src/Rules/{StrEndsWith.php => EndsWith.php} | 2 +- .../{StrStartsWith.php => StartsWith.php} | 2 +- src/Utils.php | 19 +++++++++++++++ src/Validators/ErrorSuite.php | 16 +------------ tests/Blueprint/RulesTest.php | 24 +++++++++---------- 10 files changed, 42 insertions(+), 40 deletions(-) rename src/Rules/{StrEndsWith.php => EndsWith.php} (94%) rename src/Rules/{StrStartsWith.php => StartsWith.php} (94%) diff --git a/Dockerfile b/Dockerfile index 078b0ff8..929daa31 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,7 +25,4 @@ RUN cd /app \ && composer clear-cache RUN chmod +x /app/csv-blueprint -# Color output by default -ENV TERM_PROGRAM=Hyper - ENTRYPOINT ["/app/csv-blueprint"] diff --git a/README.md b/README.md index 1f8501fe..85e6367f 100644 --- a/README.md +++ b/README.md @@ -330,8 +330,8 @@ columns: max_word_count: 5 # Integer only. Max count of words in the string Example: "Hello World! 123" - 2 words only (123 is not a word) at_least_contains: [ a, b ] # At least one of the string must be in the CSV value. Case-sensitive. all_must_contain: [ a, b, c ] # All the strings must be part of a CSV value. Case-sensitive. - str_starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World" - str_ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix" + starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World" + ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix" # Decimal and integer numbers min: 10 # Can be integer or float, negative and positive diff --git a/schema-examples/full.json b/schema-examples/full.json index 31ab7f6b..6106829b 100644 --- a/schema-examples/full.json +++ b/schema-examples/full.json @@ -28,8 +28,8 @@ "max_word_count" : 5, "at_least_contains" : ["a", "b"], "all_must_contain" : ["a", "b", "c"], - "str_starts_with" : "prefix ", - "str_ends_with" : " suffix", + "starts_with" : "prefix ", + "ends_with" : " suffix", "min" : 10, "max" : 100.5, "precision" : 3, diff --git a/schema-examples/full.php b/schema-examples/full.php index fde385b1..c2afbac0 100644 --- a/schema-examples/full.php +++ b/schema-examples/full.php @@ -46,8 +46,8 @@ 'max_word_count' => 5, 'at_least_contains' => ['a', 'b'], 'all_must_contain' => ['a', 'b', 'c'], - 'str_starts_with' => 'prefix ', - 'str_ends_with' => ' suffix', + 'starts_with' => 'prefix ', + 'ends_with' => ' suffix', 'min' => 10, 'max' => 100.5, 'precision' => 3, diff --git a/schema-examples/full.yml b/schema-examples/full.yml index 3f351a10..7ba41d13 100644 --- a/schema-examples/full.yml +++ b/schema-examples/full.yml @@ -52,8 +52,8 @@ columns: max_word_count: 5 # Integer only. Max count of words in the string Example: "Hello World! 123" - 2 words only (123 is not a word) at_least_contains: [ a, b ] # At least one of the string must be in the CSV value. Case-sensitive. all_must_contain: [ a, b, c ] # All the strings must be part of a CSV value. Case-sensitive. - str_starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World" - str_ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix" + starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World" + ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix" # Decimal and integer numbers min: 10 # Can be integer or float, negative and positive diff --git a/src/Rules/StrEndsWith.php b/src/Rules/EndsWith.php similarity index 94% rename from src/Rules/StrEndsWith.php rename to src/Rules/EndsWith.php index aa5dd3ad..3d205112 100644 --- a/src/Rules/StrEndsWith.php +++ b/src/Rules/EndsWith.php @@ -16,7 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules; -final class StrEndsWith extends AbstarctRule +final class EndsWith extends AbstarctRule { public function validateRule(string $cellValue): ?string { diff --git a/src/Rules/StrStartsWith.php b/src/Rules/StartsWith.php similarity index 94% rename from src/Rules/StrStartsWith.php rename to src/Rules/StartsWith.php index a16e8f32..f2f37214 100644 --- a/src/Rules/StrStartsWith.php +++ b/src/Rules/StartsWith.php @@ -16,7 +16,7 @@ namespace JBZoo\CsvBlueprint\Rules; -final class StrStartsWith extends AbstarctRule +final class StartsWith extends AbstarctRule { public function validateRule(string $cellValue): ?string { diff --git a/src/Utils.php b/src/Utils.php index 5691bc3f..1081156a 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -16,6 +16,7 @@ namespace JBZoo\CsvBlueprint; +use JBZoo\Utils\Cli; use JBZoo\Utils\Env; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; @@ -114,4 +115,22 @@ public static function isGithubActions(): bool { return self::isDocker() && Env::bool('GITHUB_ACTIONS'); } + + public static function autoDetectTerminalWidth(): int + { + static $maxAutoDetected; // Execution optimization + + if ($maxAutoDetected === null) { + if (self::isGithubActions()) { + $maxAutoDetected = 200; // GitHub Actions has a wide terminal + } elseif (self::isDocker()) { + $maxAutoDetected = 120; + } else { + // Fallback value is 80 + $maxAutoDetected = Env::int('COLUMNS_TEST', Cli::getNumberOfColumns()); + } + } + + return $maxAutoDetected; + } } diff --git a/src/Validators/ErrorSuite.php b/src/Validators/ErrorSuite.php index 2612ae6e..d698cc56 100644 --- a/src/Validators/ErrorSuite.php +++ b/src/Validators/ErrorSuite.php @@ -207,7 +207,7 @@ private static function getTableSize(): array // Fallback to 80 if the terminal width cannot be determined. // env.COLUMNS_TEST usually not defined, so we use it only for testing purposes. - $maxAutoDetected = self::autoDetectTerminalWidth(); + $maxAutoDetected = Utils::autoDetectTerminalWidth(); $maxWindowWidth = Vars::limit( $maxAutoDetected, @@ -222,18 +222,4 @@ private static function getTableSize(): array return $floatingSizes; } - - private static function autoDetectTerminalWidth(): int - { - $maxAutoDetected = Env::int('COLUMNS_TEST', Cli::getNumberOfColumns()); - if (Utils::isDocker()) { - $maxAutoDetected = 120; - } - - if (Utils::isGithubActions()) { - $maxAutoDetected = 200; // GitHub Actions has a wide terminal - } - - return $maxAutoDetected; - } } diff --git a/tests/Blueprint/RulesTest.php b/tests/Blueprint/RulesTest.php index d7f741d3..9fd72b83 100644 --- a/tests/Blueprint/RulesTest.php +++ b/tests/Blueprint/RulesTest.php @@ -49,8 +49,8 @@ use JBZoo\CsvBlueprint\Rules\OnlyUppercase; use JBZoo\CsvBlueprint\Rules\Precision; use JBZoo\CsvBlueprint\Rules\Regex; -use JBZoo\CsvBlueprint\Rules\StrEndsWith; -use JBZoo\CsvBlueprint\Rules\StrStartsWith; +use JBZoo\CsvBlueprint\Rules\EndsWith; +use JBZoo\CsvBlueprint\Rules\StartsWith; use JBZoo\CsvBlueprint\Rules\UsaMarketName; use JBZoo\CsvBlueprint\Rules\WordCount; use JBZoo\PHPUnit\PHPUnit; @@ -767,46 +767,46 @@ public function testAllMustContain(): void public function testStrStartsWith(): void { - $rule = new StrStartsWith('prop', 'a'); + $rule = new StartsWith('prop', 'a'); isSame(null, $rule->validate('a')); isSame(null, $rule->validate('abc')); isSame( - '"str_starts_with" at line 0, column "prop". Value "" must start with "a".', + '"starts_with" at line 0, column "prop". Value "" must start with "a".', \strip_tags((string)$rule->validate('')), ); isSame( - '"str_starts_with" at line 0, column "prop". Value " a" must start with "a".', + '"starts_with" at line 0, column "prop". Value " a" must start with "a".', \strip_tags((string)$rule->validate(' a')), ); - $rule = new StrStartsWith('prop', ''); + $rule = new StartsWith('prop', ''); isSame( - '"str_starts_with" at line 0, column "prop". Rule must contain a prefix value in schema file.', + '"starts_with" at line 0, column "prop". Rule must contain a prefix value in schema file.', \strip_tags((string)$rule->validate('a ')), ); } public function testStrEndsWith(): void { - $rule = new StrEndsWith('prop', 'a'); + $rule = new EndsWith('prop', 'a'); isSame(null, $rule->validate('a')); isSame(null, $rule->validate('cba')); isSame( - '"str_ends_with" at line 0, column "prop". Value "" must end with "a".', + '"ends_with" at line 0, column "prop". Value "" must end with "a".', \strip_tags((string)$rule->validate('')), ); isSame( - '"str_ends_with" at line 0, column "prop". Value "a " must end with "a".', + '"ends_with" at line 0, column "prop". Value "a " must end with "a".', \strip_tags((string)$rule->validate('a ')), ); - $rule = new StrEndsWith('prop', ''); + $rule = new EndsWith('prop', ''); isSame( - '"str_ends_with" at line 0, column "prop". Rule must contain a suffix value in schema file.', + '"ends_with" at line 0, column "prop". Rule must contain a suffix value in schema file.', \strip_tags((string)$rule->validate('a ')), ); }