From a0586047b71a5440dca0fb3cd28cff17324cad7f Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 8 Aug 2022 10:39:04 +0100 Subject: [PATCH] Issue #3135933 by Spokje, jungle, ravi.shankar, quietone, jonathan1055, longwave, phenaproxima: Sort sniffs/rules in phpcs.xml.dist and write test to keep them sorted (cherry picked from commit 6ef6ffcf468f07a9882265b11313ecfb8d8047cb) --- core/phpcs.xml.dist | 133 ++++++++++----------- core/tests/Drupal/Tests/PhpCs/SortTest.php | 99 +++++++++++++++ 2 files changed, 165 insertions(+), 67 deletions(-) create mode 100644 core/tests/Drupal/Tests/PhpCs/SortTest.php diff --git a/core/phpcs.xml.dist b/core/phpcs.xml.dist index b8d15f939f6..c8a0dbdf873 100644 --- a/core/phpcs.xml.dist +++ b/core/phpcs.xml.dist @@ -1,6 +1,21 @@ + Default PHP CodeSniffer configuration for Drupal core. + + + */bower_components/* + */node_modules/* + + ./assets/vendor/* + + + ./core/lib/Drupal/Component/Diff/ + ./core/tests/Drupal/Tests/Component/Annotation/Doctrine/ + + + ./modules/system/tests/fixtures/HtaccessTest + . ../composer scripts/drupal.sh @@ -8,24 +23,9 @@ scripts/rebuild_token_calculator.sh scripts/run-tests.sh scripts/update-countries.sh - - - - ./assets/vendor/* - ./core/tests/Drupal/Tests/Component/Annotation/Doctrine/ - - - */node_modules/* - */bower_components/* - - - ./modules/system/tests/fixtures/HtaccessTest - - - - ./core/lib/Drupal/Component/Diff/ + @@ -33,6 +33,8 @@ + + @@ -42,8 +44,6 @@ - - @@ -56,12 +56,12 @@ TagsNotGrouped, ParamGroup --> - - + + - + @@ -75,15 +75,8 @@ - - - - - - - - + @@ -91,9 +84,17 @@ + - + + + + + + + + @@ -113,6 +114,8 @@ Drupal.NamingConventions.ValidFunctionName.ScopeNotCamelCaps. --> + + @@ -143,9 +146,6 @@ - - - */tests/* @@ -156,14 +156,17 @@ + + 0 + 0 - - 0 - + + + @@ -178,8 +181,6 @@ - - @@ -200,30 +201,29 @@ - - 0 - 0 - + 0 0 - - - + + 0 + - - + 0 - + + 0 + + 0 @@ -233,15 +233,14 @@ 0 - + + 0 - - 0 - - + 0 + @@ -255,8 +254,8 @@ - + @@ -286,10 +285,10 @@ 0 - + 0 - + 0 @@ -346,10 +345,21 @@ 0 + + + + + + + 0 + 0 + + 0 + 0 @@ -360,17 +370,6 @@ 0 - - 0 - - - - - - - - 0 - diff --git a/core/tests/Drupal/Tests/PhpCs/SortTest.php b/core/tests/Drupal/Tests/PhpCs/SortTest.php new file mode 100644 index 00000000000..768c9340a5d --- /dev/null +++ b/core/tests/Drupal/Tests/PhpCs/SortTest.php @@ -0,0 +1,99 @@ +filePath = __DIR__ . '/../../../../../core/phpcs.xml.dist'; + } + + /** + * Tests that the phpcs.xml.dist file exists. + */ + public function testFileExists() { + $this->assertFileExists($this->filePath); + } + + /** + * Tests that the phpcs.xml.dist file is properly sorted. + */ + public function testSorted() { + $content = file_get_contents($this->filePath); + $xml_encoder = new XmlEncoder(); + $xml_encoded = $xml_encoder->decode($content, 'xml'); + $this->assertIsArray($xml_encoded); + + $top_level_keys = array_keys($xml_encoded); + $this->assertSorted($top_level_keys); + + $this->assertArrayHasKey('file', $xml_encoded); + $files = $xml_encoded['file']; + $this->assertSorted($files); + + $this->assertArrayHasKey('exclude-pattern', $xml_encoded); + $excluded_patterns = $xml_encoded['exclude-pattern']; + $this->assertSorted($excluded_patterns); + + $this->assertArrayHasKey('rule', $xml_encoded); + $rules = $xml_encoded['rule']; + $this->assertSorted($rules, '@ref'); + + foreach ($rules as $item) { + if (array_key_exists('exclude', $item)) { + $excluded = $item['exclude']; + $excluded = array_filter($excluded, static function ($item) { + return is_array($item) && array_key_exists('@name', $item); + }); + $this->assertSorted($excluded, '@name'); + } + } + } + + /** + * A helper method to assert that an input array is sorted. + * + * Compared by values, if the $column is not null, the column of the value is + * used for comparing. + * + * @param array $input + * The input array. + * @param null|string $column + * The column of the value or NULL. + */ + private function assertSorted(array $input, string $column = NULL) { + $input_sorted = $input; + + if ($column === NULL) { + usort($input_sorted, static function ($a, $b) { + return strcmp($a, $b); + }); + } + else { + usort($input_sorted, static function ($a, $b) use ($column) { + return strcmp($a[$column], $b[$column]); + }); + } + + $this->assertEquals($input, $input_sorted); + } + +}