From ee056801450c52cb359ace9e1bdb25caf6c30954 Mon Sep 17 00:00:00 2001 From: TangRufus Date: Fri, 13 Mar 2020 14:55:47 +0000 Subject: [PATCH] Disallow numeric constant name close #4 --- src/Config.php | 5 +++++ src/Exceptions/NumericConstantNameException.php | 9 +++++++++ tests/ConfigTest.php | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 src/Exceptions/NumericConstantNameException.php diff --git a/src/Config.php b/src/Config.php index e6de5a5..89f79cd 100644 --- a/src/Config.php +++ b/src/Config.php @@ -4,6 +4,7 @@ namespace Roots\WPConfig; use Roots\WPConfig\Exceptions\ConstantAlreadyDefinedException; +use Roots\WPConfig\Exceptions\NumericConstantNameException; use Roots\WPConfig\Exceptions\UndefinedConfigKeyException; /** @@ -24,6 +25,10 @@ class Config */ public static function define(string $key, $value): void { + if (is_numeric($key)) { + throw new NumericConstantNameException("Numeric constant name '$key' is not allowed."); + } + self::defined($key) or self::$configMap[$key] = $value; } diff --git a/src/Exceptions/NumericConstantNameException.php b/src/Exceptions/NumericConstantNameException.php new file mode 100644 index 0000000..4e5c431 --- /dev/null +++ b/src/Exceptions/NumericConstantNameException.php @@ -0,0 +1,9 @@ +assertEquals(true, Config::get('WP_SCRIPT_DEBUG')); } + + public function testDefineSadIntegerKey() { + $this->expectException(NumericConstantNameException::class); + + Config::define('123', true); + } public function testDefineSad() { $this->expectException(ConstantAlreadyDefinedException::class);