Skip to content

Commit 779dfb1

Browse files
Vitexusclaude
andcommitted
feat(Shared): add isDebug() method for robust debug mode check
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 59e7ef6 commit 779dfb1

6 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/Ease/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Exception extends \Exception
4848
*/
4949
public function __construct(string $message, int $code = 0, ?self $previous = null)
5050
{
51-
if (\Ease\Shared::cfg('DEBUG', false)) {
51+
if (\Ease\Shared::isDebug()) {
5252
$trace = $this->getTrace();
5353
$caller = new Molecule();
5454
$where = $trace[0]['class'].'::'.$trace[0]['function'];

src/Ease/Functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ public static function guidv4($data = null): string
600600
*/
601601
public static function writeResult(array $result, string $destination = 'php://stdout', ?\Ease\Sand $engine = null)
602602
{
603-
$written = file_put_contents($destination, json_encode($result, Shared::cfg('DEBUG') ? \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES : 0));
603+
$written = file_put_contents($destination, json_encode($result, Shared::isDebug() ? \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES : 0));
604604

605605
if ($engine instanceof \Ease\Sand) {
606606
$engine->addStatusMessage(sprintf(_('Saving result to %s'), $destination), $written ? 'success' : 'error');

src/Ease/Logger/Logging.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function getLogger(?array $options = null)
9494
*/
9595
public function logBanner($prefix = '', $suffix = ''): void
9696
{
97-
if (\Ease\Shared::cfg('DEBUG') === true) {
97+
if (\Ease\Shared::isDebug()) {
9898
$suffix .= ' Loggers: '.\Ease\Shared::cfg('EASE_LOGGER');
9999
}
100100

src/Ease/Logger/Regent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function addStatusObject(Message $message)
163163
foreach ($this->loggers as $logger) {
164164
switch ($message->type) {
165165
case 'debug':
166-
if ((bool) \Ease\Shared::cfg('DEBUG', false) === false) {
166+
if (\Ease\Shared::isDebug() === false) {
167167
break;
168168
}
169169

src/Ease/Shared.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,20 @@ public static function cfg(/* string */ $constant, $cfg = null)
148148
return $cfg;
149149
}
150150

151+
/**
152+
* Whether debug mode is on.
153+
*
154+
* cfg() reads constants/env vars, both of which surface as strings —
155+
* PHP's (bool) cast treats any non-empty string (including "false" or
156+
* "0") as true, so callers comparing cfg('DEBUG') truthiness directly
157+
* flip on debug output for a literal `DEBUG=false` in the environment.
158+
* Compare the normalized string instead.
159+
*/
160+
public static function isDebug(): bool
161+
{
162+
return strtolower((string) self::cfg('DEBUG', 'false')) === 'true';
163+
}
164+
151165
/**
152166
* Application name or "Composer project Name" fallback.
153167
*

tests/src/Ease/SharedTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,27 @@ public function testUser(): void
169169
{
170170
$this->assertInstanceOf('\Ease\User', \Ease\Shared::user(null, '\Ease\User'));
171171
}
172+
173+
/**
174+
* Regression test: cfg('DEBUG') always surfaces env vars as strings, so a
175+
* literal DEBUG=false in the environment must NOT be treated as enabled —
176+
* PHP's (bool) cast on a non-empty string like "false" is true, which is
177+
* the bug isDebug() exists to avoid.
178+
*
179+
* @covers \Ease\Shared::isDebug
180+
*/
181+
public function testIsDebug(): void
182+
{
183+
putenv('DEBUG=false');
184+
$this->assertFalse(\Ease\Shared::isDebug(), 'DEBUG=false (string) must not enable debug mode');
185+
186+
putenv('DEBUG=true');
187+
$this->assertTrue(\Ease\Shared::isDebug(), 'DEBUG=true (string) must enable debug mode');
188+
189+
putenv('DEBUG=TRUE');
190+
$this->assertTrue(\Ease\Shared::isDebug(), 'DEBUG value must be compared case-insensitively');
191+
192+
putenv('DEBUG');
193+
$this->assertFalse(\Ease\Shared::isDebug(), 'Unset DEBUG must default to disabled');
194+
}
172195
}

0 commit comments

Comments
 (0)