From ceb683bed11fdbb90bd93db6df725b2546c28610 Mon Sep 17 00:00:00 2001 From: Frank Prins <25006490+PrinsFrank@users.noreply.github.com> Date: Sun, 1 Jan 2023 20:34:03 +0100 Subject: [PATCH] Don't remove items from specification, instead mark them deprecated --- dev/DataTarget/EnumCase.php | 13 ++++++++++--- dev/DataTarget/EnumFile.php | 2 +- dev/SpecUpdater.php | 7 +++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/dev/DataTarget/EnumCase.php b/dev/DataTarget/EnumCase.php index 20a4b05a..6cdb2c84 100644 --- a/dev/DataTarget/EnumCase.php +++ b/dev/DataTarget/EnumCase.php @@ -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; } } diff --git a/dev/DataTarget/EnumFile.php b/dev/DataTarget/EnumFile.php index b5a5063f..ae16f437 100644 --- a/dev/DataTarget/EnumFile.php +++ b/dev/DataTarget/EnumFile.php @@ -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)); diff --git a/dev/SpecUpdater.php b/dev/SpecUpdater.php index bcc33aa5..8469bb54 100644 --- a/dev/SpecUpdater.php +++ b/dev/SpecUpdater.php @@ -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();