diff --git a/composer.json b/composer.json index f3473f7..b372022 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "dealerdirect/phpcodesniffer-composer-installer": "^0.7", "phpcompatibility/phpcompatibility-wp": "*", "phpunit/phpunit": "^9.6", - "rector/rector": "^2.5", + "rector/rector": "2.7.0", "captainhook/captainhook": "^5.29", "captainhook/hook-installer": "^1.0" }, diff --git a/src/Cache/CacheManager.php b/src/Cache/CacheManager.php index 143870e..f331279 100644 --- a/src/Cache/CacheManager.php +++ b/src/Cache/CacheManager.php @@ -34,9 +34,7 @@ public function store(?string $name = null): Repository { $name ??= $this->defaultStoreName(); - if (!isset($this->repositories[$name])) { - $this->repositories[$name] = new Repository($this->makeStore($name)); - } + $this->repositories[$name] ??= new Repository($this->makeStore($name)); return $this->repositories[$name]; } diff --git a/src/Hooks/Hooks.php b/src/Hooks/Hooks.php index 02c269e..c487759 100644 --- a/src/Hooks/Hooks.php +++ b/src/Hooks/Hooks.php @@ -20,9 +20,7 @@ final class Hooks public function __construct() { - if (!isset(self::$_hook)) { - self::$_hook = new HooksWrapper(); - } + self::$_hook ??= new HooksWrapper(); } public function __call(string $method, array $parameters) diff --git a/src/Http/Detection/ClientIpResolver.php b/src/Http/Detection/ClientIpResolver.php index c589049..0141c02 100644 --- a/src/Http/Detection/ClientIpResolver.php +++ b/src/Http/Detection/ClientIpResolver.php @@ -73,7 +73,7 @@ private static function isTrustedProxy(string $ip): bool return false; } - private static function isIpInRange(string $ip, $range) + private static function isIpInRange(string $ip, $range): bool { $range = trim((string) $range); if (!str_contains($range, '/')) { diff --git a/src/Http/Response.php b/src/Http/Response.php index 9d80924..384f580 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -254,9 +254,7 @@ private static function validateHeader($header, $value): array private static function current(): Response { - if (\is_null(self::$_current)) { - self::$_current = new self(); - } + self::$_current ??= new self(); return self::$_current; } diff --git a/src/Http/Router/AjaxRouter.php b/src/Http/Router/AjaxRouter.php index c1d5984..d363347 100644 --- a/src/Http/Router/AjaxRouter.php +++ b/src/Http/Router/AjaxRouter.php @@ -64,7 +64,7 @@ public function currentRoute() return $this->_router->getRegisteredRoute($this->currentRouteName()); } - private function isRouteMatched(RouteRegister $route, string|array $requestPath) + private function isRouteMatched(RouteRegister $route, string|array $requestPath): bool { if ($route->getRoutePrefix() . $route->getPath() === $requestPath) { return true; diff --git a/src/Http/Router/RouteBase.php b/src/Http/Router/RouteBase.php index 5cc0c88..9094068 100644 --- a/src/Http/Router/RouteBase.php +++ b/src/Http/Router/RouteBase.php @@ -185,9 +185,7 @@ public function group(Closure $callback): self */ public function getRouter() { - if (!isset($this->_router)) { - $this->_router = Router::instance(); - } + $this->_router ??= Router::instance(); return $this->_router; } diff --git a/src/Http/Router/Router.php b/src/Http/Router/Router.php index 5c3418e..2f5892f 100644 --- a/src/Http/Router/Router.php +++ b/src/Http/Router/Router.php @@ -77,9 +77,7 @@ public function getRegisteredRoutes(): array public static function instance($type = null, $namespace = null, $version = null) { if ($type === null) { - if (\is_null(self::$_instance)) { - self::$_instance = new self(RequestType::AJAX, $namespace, $version); - } + self::$_instance ??= new self(RequestType::AJAX, $namespace, $version); return self::$_instance; } diff --git a/src/Settings/SettingsRepository.php b/src/Settings/SettingsRepository.php index ecc7955..2f12c60 100644 --- a/src/Settings/SettingsRepository.php +++ b/src/Settings/SettingsRepository.php @@ -36,8 +36,12 @@ public function get(string $key, $default = null) } $field = $this->schema->field($key); + if ($field === null) { + return $default; + } - return $field !== null ? $field->default() : $default; + // A declared field's own default wins, even when it is null. + return $field->default(); } /** diff --git a/src/Shortcode/Shortcode.php b/src/Shortcode/Shortcode.php index 83b780b..1ff10f2 100644 --- a/src/Shortcode/Shortcode.php +++ b/src/Shortcode/Shortcode.php @@ -19,9 +19,7 @@ final class Shortcode public function __construct() { - if (!isset(self::$_wrapper)) { - self::$_wrapper = new ShortcodeWrapper(); - } + self::$_wrapper ??= new ShortcodeWrapper(); } public function __call(string $method, array $parameters) diff --git a/tests/Settings/SettingsRepositoryTest.php b/tests/Settings/SettingsRepositoryTest.php index 0b210d6..b6c6d6e 100644 --- a/tests/Settings/SettingsRepositoryTest.php +++ b/tests/Settings/SettingsRepositoryTest.php @@ -36,6 +36,15 @@ public function testSetSaveReload(): void $this->assertFalse($fresh->get('logging_enabled')); } + public function testDeclaredNullDefaultWinsOverCallerFallback(): void + { + $schema = (new SettingsSchema())->add(SettingField::string('label', null)); + $repo = new SettingsRepository('demo_settings', $schema); + + $this->assertNull($repo->get('label', 'fallback')); + $this->assertSame('fallback', $repo->get('undeclared', 'fallback')); + } + public function testUnknownKeyRejected(): void { $this->expectException(InvalidArgumentException::class);