diff --git a/core/lib/Drupal/Core/Theme/ThemeColorsParser.php b/core/lib/Drupal/Core/Theme/ThemeColorsParser.php index 41efbd3986..75cf88f27e 100644 --- a/core/lib/Drupal/Core/Theme/ThemeColorsParser.php +++ b/core/lib/Drupal/Core/Theme/ThemeColorsParser.php @@ -41,7 +41,7 @@ class ThemeColorsParser { * @throws \Drupal\Core\Extension\InfoParserException * Exception thrown if there is a parsing error. */ - public function parse($filename) { + public function parse(string $filename): array { $defaults = [ 'colors' => [], 'schemes' => [], @@ -50,22 +50,20 @@ public function parse($filename) { if (!file_exists($filename)) { return $defaults; } - else { - try { - $parsed_info = Yaml::decode(file_get_contents($filename)) + $defaults; - } - catch (InvalidDataTypeException $e) { - throw new \RuntimeException("Unable to parse $filename " . $e->getMessage()); - } - // Check if the colors map contains at least one configurable color. - if (empty($parsed_info['colors'])) { - throw new \RuntimeException('At least one configurable color is required.'); - } - // Check if all scheme colors have a corresponding configurable color. - foreach ($parsed_info['schemes'] as $scheme => $content) { - if (!empty(array_diff_key($parsed_info['colors'], $content['colors']))) { - throw new \RuntimeException("The colors in scheme '$scheme' do not match with the configurable colors."); - } + try { + $parsed_info = Yaml::decode(file_get_contents($filename)) + $defaults; + } + catch (InvalidDataTypeException $e) { + throw new \RuntimeException("Unable to parse $filename " . $e->getMessage()); + } + // Check if the colors map contains at least one configurable color. + if (empty($parsed_info['colors'])) { + throw new \RuntimeException('At least one configurable color is required.'); + } + // Check if all scheme colors have a corresponding configurable color. + foreach ($parsed_info['schemes'] as $scheme => $content) { + if (!empty(array_diff_key($parsed_info['colors'], $content['colors']))) { + throw new \RuntimeException("The colors in scheme '$scheme' do not match with the configurable colors."); } } return $parsed_info; diff --git a/core/tests/Drupal/Tests/Core/Theme/ThemeColorsParserTest.php b/core/tests/Drupal/Tests/Core/Theme/ThemeColorsParserTest.php index 8a01c0c4f7..cda5f8cd01 100644 --- a/core/tests/Drupal/Tests/Core/Theme/ThemeColorsParserTest.php +++ b/core/tests/Drupal/Tests/Core/Theme/ThemeColorsParserTest.php @@ -106,7 +106,7 @@ public function testColorParserBroken() { ], ]); $filename = vfsStream::url('root/fixtures/broken.colors.yml'); - $this->expectException('\RuntimeException'); + $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('broken.colors.yml'); $this->themeColorsParser->parse($filename); } @@ -135,7 +135,7 @@ public function testColorParserMissingRequired() { ], ]); $filename = vfsStream::url('root/fixtures/required.colors.yml'); - $this->expectException('\RuntimeException'); + $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('At least one configurable color is required.'); $this->themeColorsParser->parse($filename); } @@ -165,7 +165,7 @@ public function testColorParserSchemeMismatch() { ], ]); $filename = vfsStream::url('root/fixtures/mismatch.colors.yml'); - $this->expectException('\RuntimeException'); + $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('default'); $this->themeColorsParser->parse($filename); }