Skip to content

Commit

Permalink
Don't remove items from specification, instead mark them deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
PrinsFrank committed Jan 1, 2023
1 parent 1ee6abf commit ceb683b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
13 changes: 10 additions & 3 deletions dev/DataTarget/EnumCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@

class EnumCase
{
public function __construct(private string $key, private string|int $value)
public function __construct(private string $key, private string|int $value, private bool $deprecated = false)
{
}

public function __toString(): string
{
$case = '';
if ($this->deprecated === true) {
$case .= '/** @deprecated Has been removed from the specification but is maintained here for Backwards Compatibility reasons */' . PHP_EOL;
}

if (is_int($this->value)) {
return 'case ' . $this->key . ' = ' . $this->value . ';';
$case .= 'case ' . $this->key . ' = ' . $this->value . ';' . PHP_EOL;
} else {
$case .= 'case ' . $this->key . ' = \'' . str_replace('\'', '\\\'', $this->value) . '\';' . PHP_EOL;
}

return 'case ' . $this->key . ' = \'' . str_replace('\'', '\\\'', $this->value) . '\';';
return $case;
}
}
2 changes: 1 addition & 1 deletion dev/DataTarget/EnumFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function writeCases(): self
$endEnumPos = mb_strrpos($enumContent, '}');
$newEnumContent = mb_substr($enumContent, 0, $startEnum + 1) . PHP_EOL;
foreach ($this->cases as $case) {
$newEnumContent .= ' ' . ((string) $case) . PHP_EOL;
$newEnumContent .= $case;
}
$newEnumContent .= mb_substr($enumContent, $firstMethodPos !== false ? ($firstMethodPos - 5) : ($endEnumPos - 1));

Expand Down
7 changes: 7 additions & 0 deletions dev/SpecUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ public static function update(Event $event): void
$enumCases[] = new EnumCase($name, $value);
}

$existingValues = array_map(static function (\BackedEnum $backedEnum) {
return $backedEnum->value;
}, $specFQN::cases());
foreach (array_diff($existingValues, $nameValuePairs) as $deprecatedValue) {
$enumCases[] = new EnumCase($specFQN::from($deprecatedValue)->name, $deprecatedValue, true);
}

(new EnumFile($sourceFQN::getSpecFQN()))
->setCases(...$enumCases)
->writeCases();
Expand Down

0 comments on commit ceb683b

Please sign in to comment.