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

Disallow numeric constant name #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Roots\WPConfig;

use Roots\WPConfig\Exceptions\ConstantAlreadyDefinedException;
use Roots\WPConfig\Exceptions\NumericConstantNameException;
use Roots\WPConfig\Exceptions\UndefinedConfigKeyException;

/**
Expand All @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/NumericConstantNameException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Roots\WPConfig\Exceptions;

class NumericConstantNameException extends \RuntimeException
{

}
7 changes: 7 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use PHPUnit\Framework\TestCase;
use Roots\WPConfig\Exceptions\ConstantAlreadyDefinedException;
use Roots\WPConfig\Exceptions\NumericConstantNameException;
use Roots\WPConfig\Exceptions\UndefinedConfigKeyException;

/**
Expand All @@ -17,6 +18,12 @@ public function testDefineHappy() {
Config::define('WP_SCRIPT_DEBUG', true);
$this->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);
Expand Down