Skip to content

Commit

Permalink
Fix issue with constants with digit as first character
Browse files Browse the repository at this point in the history
This is not valid php code so we need to make sure that the leading
_ is preserved when transforming the case
  • Loading branch information
sunkan committed Jul 25, 2024
1 parent 37425b4 commit ba15dcf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/PhpConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ public function __construct(
public function getName(): string
{
$currentName = $this->identifier;
$name = (string)preg_replace('/^(\d)/', '_$0', $currentName);
$name = str_replace('-', '_', $name);
$sanitizedName = (string)preg_replace('/^(\d)/', '_$0', $currentName);
$sanitizedName = str_replace('-', '_', $sanitizedName);

if ($this->case === self::CASE_NONE) {
return $name;
return $sanitizedName;
}

$name = (string)preg_replace('/(?<!^)[A-Z]/', '_$0', $name);
$name = (string)preg_replace('/(?<!^)[A-Z]/', '_$0', $sanitizedName);
if ($this->case === self::CASE_LOWER) {
return strtolower($name);
}

if ($currentName === strtoupper($currentName)) {
return $currentName;
if ($sanitizedName === strtoupper($sanitizedName)) {
return $sanitizedName;
}
return strtoupper($name);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Renderer/PhpConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ public function testChangeValue()

$this->assertSame('public const TEST_CASE = \'test_value\';', trim($render->render($const)));
}

public function testUpperCaseWithLeadingDigit(): void
{
$const = PhpConstant::public(identifier: '3DS');
$render = new Php7Renderer();

$this->assertSame(['public const _3DS = \'3DS\';'], $render->renderConstant($const));
}
}

0 comments on commit ba15dcf

Please sign in to comment.