Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some dependencies #16

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions app/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
10 changes: 8 additions & 2 deletions app/Foundation/Hooks/Support/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
17 changes: 11 additions & 6 deletions app/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
use Psr\Container\ContainerInterface;
use RecursiveDirectoryIterator;
use SplFileInfo;
use Syntatis\Utils\Val;

use function dirname;
use function is_dir;
use function is_file;
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.
Expand Down Expand Up @@ -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.');
}

Expand All @@ -241,12 +245,13 @@ 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');

if (! is_string($name) || Val::isBlank($name)) {
throw new InvalidArgumentException('The app "name" is required and cannot be empty.');
}

return new App($name, $config);
};
}
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions tests/app/Core/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Codex\Tests\Core;

use Codex\Core\Config;
use Codex\Tests\WPTestCase;

class ConfigTest extends WPTestCase
{
/**
* @dataProvider dataIsBlank
*
* @param mixed $value
*/
public function testIsBlank($value, bool $expect): void
{
$config = new Config(['foo' => $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];
}
}