From 66e6be38b15f7386d102af6fa03fd1cbf9c4b46a Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Mon, 27 Jan 2025 00:16:17 +0000 Subject: [PATCH 1/2] [CLEANUP] Extract method `Color::renderAsHex` An additional method to render in the "modern" syntax will soon be needed. Splitting up the `render` method into sub-methods will help as a precursor to that. --- src/Value/Color.php | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Value/Color.php b/src/Value/Color.php index 07f11c346..38b3cdd33 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -230,15 +230,9 @@ public function render(OutputFormat $outputFormat): string && \implode('', \array_keys($this->aComponents)) === 'rgb' && $this->allComponentsAreNumbers() ) { - $result = \sprintf( - '%02x%02x%02x', - $this->aComponents['r']->getSize(), - $this->aComponents['g']->getSize(), - $this->aComponents['b']->getSize() - ); - return '#' . (($result[0] == $result[1]) && ($result[2] == $result[3]) && ($result[4] == $result[5]) - ? "$result[0]$result[2]$result[4]" : $result); + return $this->renderAsHex(); } + return parent::render($outputFormat); } @@ -256,4 +250,23 @@ private function allComponentsAreNumbers(): bool return true; } + + /** + * Note that this method assumes the following: + * - The {@see aComponents} array has keys for `r`, `g` and `b`; + * - The values in the array are all instances of {@see Size}. + * + * Errors will be triggered or thrown if this is not the case. + */ + private function renderAsHex(): string + { + $result = \sprintf( + '%02x%02x%02x', + $this->aComponents['r']->getSize(), + $this->aComponents['g']->getSize(), + $this->aComponents['b']->getSize() + ); + return '#' . (($result[0] == $result[1]) && ($result[2] == $result[3]) && ($result[4] == $result[5]) + ? "$result[0]$result[2]$result[4]" : $result); + } } From 25bfff36c3d3bb5fbefd34405927ef5b42a90052 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Mon, 27 Jan 2025 16:30:30 +0000 Subject: [PATCH 2/2] Changes suggested in review --- src/Value/Color.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Value/Color.php b/src/Value/Color.php index 38b3cdd33..a58357a66 100644 --- a/src/Value/Color.php +++ b/src/Value/Color.php @@ -253,10 +253,12 @@ private function allComponentsAreNumbers(): bool /** * Note that this method assumes the following: - * - The {@see aComponents} array has keys for `r`, `g` and `b`; - * - The values in the array are all instances of {@see Size}. + * - The `aComponents` array has keys for `r`, `g` and `b`; + * - The values in the array are all instances of `Size`. * * Errors will be triggered or thrown if this is not the case. + * + * @return non-empty-string */ private function renderAsHex(): string { @@ -266,7 +268,8 @@ private function renderAsHex(): string $this->aComponents['g']->getSize(), $this->aComponents['b']->getSize() ); - return '#' . (($result[0] == $result[1]) && ($result[2] == $result[3]) && ($result[4] == $result[5]) - ? "$result[0]$result[2]$result[4]" : $result); + $canUseShortVariant = ($result[0] == $result[1]) && ($result[2] == $result[3]) && ($result[4] == $result[5]); + + return '#' . ($canUseShortVariant ? $result[0] . $result[2] . $result[4] : $result); } }