From 6a4df8373cc5b7b0eec4995a09097890726aa363 Mon Sep 17 00:00:00 2001 From: babaev-an Date: Sat, 24 May 2025 14:30:34 +0300 Subject: [PATCH] 20250524 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Добавлено перечисление VarNotBoolAction, для определения действий в классе BoolExtensions (методы AnyTrue и TrueCount), если передана часть не булевого типа. - В расширении BoolExtensions в методах AnyTrue и TrueCount убрано выброс исключение, если какой-то аргумент из массива expressions не является булевым типом. Вместо него в обоих методах введён необязательный параметр $ifNotBool (тип VarNotBoolAction), который определяет действие: игнорирование (этот аргумент просто исключается из проверки), считать правдивым (вместо него ставится true) и считать ложным (вместо него ставится false). --- composer.json | 2 +- composer.lock | 52 +++++++++++++++---------- sources/enums/VarNotBoolAction.php | 34 ++++++++++++++++ sources/extensions/BoolExtensions.php | 45 ++++++++++++++------- tests/extensions/BoolExtensionsTest.php | 17 ++++---- 5 files changed, 105 insertions(+), 45 deletions(-) create mode 100644 sources/enums/VarNotBoolAction.php diff --git a/composer.json b/composer.json index 906ff55..132de5f 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^12.1.5" + "phpunit/phpunit": "^12.1.6" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index dec9765..43b8373 100644 --- a/composer.lock +++ b/composer.lock @@ -245,16 +245,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "12.2.1", + "version": "12.3.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "448f2c504d86dbff3949dcd02c95aa85db2c7617" + "reference": "9075a8efc66e11bc55c319062e147bdb06777267" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/448f2c504d86dbff3949dcd02c95aa85db2c7617", - "reference": "448f2c504d86dbff3949dcd02c95aa85db2c7617", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/9075a8efc66e11bc55c319062e147bdb06777267", + "reference": "9075a8efc66e11bc55c319062e147bdb06777267", "shasum": "" }, "require": { @@ -281,7 +281,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "12.2.x-dev" + "dev-main": "12.3.x-dev" } }, "autoload": { @@ -310,7 +310,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/12.2.1" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/12.3.0" }, "funding": [ { @@ -330,7 +330,7 @@ "type": "tidelift" } ], - "time": "2025-05-04T05:25:05+00:00" + "time": "2025-05-23T15:49:03+00:00" }, { "name": "phpunit/php-file-iterator", @@ -579,16 +579,16 @@ }, { "name": "phpunit/phpunit", - "version": "12.1.5", + "version": "12.1.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "f93ef2198df8d54b3195bcee381a33be51d8705e" + "reference": "2fdf0056c673c8f0f1eed00030be5f8243c1e6e0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f93ef2198df8d54b3195bcee381a33be51d8705e", - "reference": "f93ef2198df8d54b3195bcee381a33be51d8705e", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/2fdf0056c673c8f0f1eed00030be5f8243c1e6e0", + "reference": "2fdf0056c673c8f0f1eed00030be5f8243c1e6e0", "shasum": "" }, "require": { @@ -610,7 +610,7 @@ "sebastian/cli-parser": "^4.0.0", "sebastian/comparator": "^7.0.1", "sebastian/diff": "^7.0.0", - "sebastian/environment": "^8.0.0", + "sebastian/environment": "^8.0.1", "sebastian/exporter": "^7.0.0", "sebastian/global-state": "^8.0.0", "sebastian/object-enumerator": "^7.0.0", @@ -656,7 +656,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/12.1.5" + "source": "https://github.com/sebastianbergmann/phpunit/tree/12.1.6" }, "funding": [ { @@ -680,7 +680,7 @@ "type": "tidelift" } ], - "time": "2025-05-11T06:44:52+00:00" + "time": "2025-05-21T12:36:31+00:00" }, { "name": "sebastian/cli-parser", @@ -946,16 +946,16 @@ }, { "name": "sebastian/environment", - "version": "8.0.0", + "version": "8.0.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "8afe311eca49171bf95405cc0078be9a3821f9f2" + "reference": "d364b9e5d0d3b18a2573351a1786fbf96b7e0792" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/8afe311eca49171bf95405cc0078be9a3821f9f2", - "reference": "8afe311eca49171bf95405cc0078be9a3821f9f2", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/d364b9e5d0d3b18a2573351a1786fbf96b7e0792", + "reference": "d364b9e5d0d3b18a2573351a1786fbf96b7e0792", "shasum": "" }, "require": { @@ -998,15 +998,27 @@ "support": { "issues": "https://github.com/sebastianbergmann/environment/issues", "security": "https://github.com/sebastianbergmann/environment/security/policy", - "source": "https://github.com/sebastianbergmann/environment/tree/8.0.0" + "source": "https://github.com/sebastianbergmann/environment/tree/8.0.2" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/environment", + "type": "tidelift" } ], - "time": "2025-02-07T04:56:08+00:00" + "time": "2025-05-21T15:05:44+00:00" }, { "name": "sebastian/exporter", diff --git a/sources/enums/VarNotBoolAction.php b/sources/enums/VarNotBoolAction.php new file mode 100644 index 0000000..97f4e86 --- /dev/null +++ b/sources/enums/VarNotBoolAction.php @@ -0,0 +1,34 @@ +true. * * @param array $expressions Переменные формата правда/ложь. + * @param VarNotBoolAction $ifNotBool Действие, если какой-то аргумент expressions не является булевым + * типом. По умолчанию игнорировать. * * @return bool Есть ли хотя бы одно в значении true. * - * @throws Exception Выбрасывается, если хотя бы один аргумент expressions не являются типом - * правда/ложь. */ - public static function AnyTrue (array $expressions): bool + public static function AnyTrue (array $expressions, VarNotBoolAction $ifNotBool = VarNotBoolAction::Ignore): bool { - return self::TrueCount($expressions) > 0; + return self::TrueCount($expressions, $ifNotBool) > 0; } /** * Вычисляет количество переменных формата правда/ложь expressions в значении true. * * @param array $expressions Переменные формата правда/ложь. + * @param VarNotBoolAction $ifNotBool Действие, если какой-то аргумент expressions не является булевым + * типом. По умолчанию игнорировать. * * @return int Количество переменных в значении true. - * - * @throws Exception Выбрасывается, если хотя бы один аргумент expressions не являются типом - * правда/ложь. */ - public static function TrueCount (array $expressions): int + public static function TrueCount (array $expressions, VarNotBoolAction $ifNotBool = VarNotBoolAction::Ignore): int { - // Проверяем все аргументы + // Создаем пустой массив для хранения проверяемых аргументов + $checkArray = []; + + // Проверяем все входящие аргументы foreach ($expressions as $expression) // - если аргумент не является типом правда/ложь if (!is_bool($expression)) - // -- то выбрасываем исключение - throw new Exception('All arguments must be bool. / Все аргументы должны быть типа «правда/ложь».'); + // -- то делаем следующее в зависимости от настроек: + switch ($ifNotBool) { + case VarNotBoolAction::Ignore: + // --- игнорируем аргумент + break; + case VarNotBoolAction::ConsiderItTrue: + // --- считаем аргумент как истинное значение + $checkArray[] = true; + break; + case VarNotBoolAction::ConsiderItFalse: + // --- считаем аргумент как ложное значение + $checkArray[] = false; + break; + } + else + // - иначе добавляем аргумент в массив проверяемых аргументов + $checkArray[] = $expression; // Используем array_filter для фильтрации всех истинных значений - $filtered = array_filter($expressions); + $filtered = array_filter($checkArray, fn ($value) => $value === true); // Возвращаем количество элементов в отфильтрованном массиве return count($filtered); diff --git a/tests/extensions/BoolExtensionsTest.php b/tests/extensions/BoolExtensionsTest.php index ee403cf..3903774 100644 --- a/tests/extensions/BoolExtensionsTest.php +++ b/tests/extensions/BoolExtensionsTest.php @@ -2,7 +2,7 @@ namespace goodboyalex\php_components_pack\tests\extensions; -use Exception; +use goodboyalex\php_components_pack\enums\VarNotBoolAction; use goodboyalex\php_components_pack\extensions\BoolExtensions; use PHPUnit\Framework\TestCase; @@ -30,6 +30,7 @@ class BoolExtensionsTest extends TestCase private function PrepareForTest (): void { + require_once __DIR__ . '/../../sources/enums/VarNotBoolAction.php'; require_once __DIR__ . '/../../sources/extensions/BoolExtensions.php'; } @@ -46,23 +47,19 @@ class BoolExtensionsTest extends TestCase $this->assertEquals(14, BoolExtensions::TrueCount($array)); - $this->expectException(Exception::class); - $array[] = "ПРЕДАТЕЛЬ!"; - BoolExtensions::TrueCount($array); + $this->assertEquals(14, BoolExtensions::TrueCount($array)); + $this->assertEquals(14, BoolExtensions::TrueCount($array, VarNotBoolAction::ConsiderItFalse)); + $this->assertEquals(15, BoolExtensions::TrueCount($array, VarNotBoolAction::ConsiderItTrue)); } public function testExportToString () { $this->PrepareForTest(); - $b = true; + $this->assertEquals('О, да!', BoolExtensions::ExportToString(true, 'О, да!', 'О, нет!')); - $this->assertEquals('О, да!', BoolExtensions::ExportToString($b, 'О, да!', 'О, нет!')); - - $b = false; - - $this->assertEquals('О, нет!', BoolExtensions::ExportToString($b, 'О, да!', 'О, нет!')); + $this->assertEquals('О, нет!', BoolExtensions::ExportToString(false, 'О, да!', 'О, нет!')); } } \ No newline at end of file