From 0a4311fc6e332238b5770715800308d176eee4f2 Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Fri, 11 Oct 2024 01:04:22 +0700 Subject: [PATCH 1/3] Remove some dependencies --- app/Core/Config.php | 17 +++++++++++++++-- app/Foundation/Hooks/Support/Parser.php | 10 ++++++++-- app/Plugin.php | 12 ++++++------ composer.json | 3 +-- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/Core/Config.php b/app/Core/Config.php index 49d51dc..d7e1770 100644 --- a/app/Core/Config.php +++ b/app/Core/Config.php @@ -5,9 +5,12 @@ namespace Codex\Core; use Adbar\Dot; -use Syntatis\Utils\Val; +use function count; use function dot; +use function is_array; +use function is_string; +use function trim; /** @internal This class should not be used directly, Developers should use the `Config` facade instead. */ final class Config @@ -49,6 +52,16 @@ public function has(string $key): bool */ public function isBlank(string $key): bool { - return Val::isBlank($this->dot->get($key)); + $value = $this->dot->get($key); + + if ($value === false || $value === null) { + return true; + } + + if (is_string($value) && trim($value) === '') { + return true; + } + + return is_array($value) && count($value) === 0; } } diff --git a/app/Foundation/Hooks/Support/Parser.php b/app/Foundation/Hooks/Support/Parser.php index 53e3c27..d90749f 100644 --- a/app/Foundation/Hooks/Support/Parser.php +++ b/app/Foundation/Hooks/Support/Parser.php @@ -9,9 +9,10 @@ use Codex\Foundation\Hooks\Filter; use Codex\Foundation\Hooks\Hook; use ReflectionClass; -use Syntatis\Utils\Str; use function is_callable; +use function strlen; +use function strncmp; /** @internal */ final class Parser implements Hookable @@ -86,7 +87,12 @@ private function parseMethodAttrs(): void continue; } - if ($method->isConstructor() || $method->isDestructor() || Str::startsWith($method->getName(), '__')) { + if ( + $method->isConstructor() || + $method->isDestructor() || + // Starts with __ + strncmp($method->getName(), '__', strlen('__')) === 0 + ) { continue; } diff --git a/app/Plugin.php b/app/Plugin.php index 887e309..6a9e010 100644 --- a/app/Plugin.php +++ b/app/Plugin.php @@ -21,7 +21,6 @@ use Psr\Container\ContainerInterface; use RecursiveDirectoryIterator; use SplFileInfo; -use Syntatis\Utils\Val; use function dirname; use function is_dir; @@ -29,6 +28,7 @@ use function is_string; use function is_subclass_of; use function method_exists; +use function trim; /** * Orchastates the WordPress plugin lifecycle and define the required services. @@ -232,7 +232,11 @@ private function registerCoreServices(): void } } - if (! isset($config['app']['name']) || Val::isBlank($config['app']['name'])) { + if ( + ! isset($config['app']['name']) || + ! is_string($config['app']['name']) || + trim($config['app']['name']) === '' + ) { throw new InvalidArgumentException('The app "name" is required and cannot be empty.'); } @@ -243,10 +247,6 @@ private function registerCoreServices(): void $config = $container['config']; $name = $config->get('app.name'); - if (! is_string($name) || Val::isBlank($name)) { - throw new InvalidArgumentException('The app "name" is required and cannot be empty.'); - } - return new App($name, $config); }; } diff --git a/composer.json b/composer.json index 6bc57d4..1b3a870 100644 --- a/composer.json +++ b/composer.json @@ -36,8 +36,7 @@ "require": { "php": ">=7.4", "adbario/php-dot-notation": "^3.3", - "pimple/pimple": "^3.5", - "syntatis/utils": "^2.0" + "pimple/pimple": "^3.5" }, "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^1.0", From 2c72038820742df109cfda6c7be483ee3414a694 Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Fri, 11 Oct 2024 01:08:52 +0700 Subject: [PATCH 2/3] Add type --- app/Plugin.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/Plugin.php b/app/Plugin.php index 6a9e010..083cd1c 100644 --- a/app/Plugin.php +++ b/app/Plugin.php @@ -245,6 +245,11 @@ private function registerCoreServices(): void $this->pimple['app'] = static function (PimpleContainer $container): App { /** @var Config $config */ $config = $container['config']; + /** + * @internal App name has been validated in the Config class. + * + * @var string $name + */ $name = $config->get('app.name'); return new App($name, $config); From 6b5d838dd8591a768f15fd7803db44f3f652aa14 Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Fri, 11 Oct 2024 01:44:08 +0700 Subject: [PATCH 3/3] Add test for `isBlank` method --- tests/app/Core/ConfigTest.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/app/Core/ConfigTest.php diff --git a/tests/app/Core/ConfigTest.php b/tests/app/Core/ConfigTest.php new file mode 100644 index 0000000..b7d23aa --- /dev/null +++ b/tests/app/Core/ConfigTest.php @@ -0,0 +1,34 @@ + $value]); + + $this->assertSame($expect, $config->isBlank('foo')); + } + + public static function dataIsBlank(): iterable + { + yield 'null' => [null, true]; + yield 'false' => [false, true]; + yield 'empty string' => ['', true]; + yield 'empty array' => [[], true]; + yield 'whitespace string' => [' ', true]; + yield 'non-empty string' => ['foo', false]; + yield 'non-empty array' => [['foo'], false]; + } +}