Skip to content

Commit 387d852

Browse files
committed
Check rules are sorted
1 parent 7111187 commit 387d852

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

.github/workflows/rules.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Rules Checks
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
sorted:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
- name: Check rules are sorted
11+
run: php tests/sorted.php

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
PHP Code Sniffer rules
33

44
[![XML Lint](https://github.com/spaze/coding-standard/actions/workflows/xml.yml/badge.svg)](https://github.com/spaze/coding-standard/actions/workflows/xml.yml)
5+
[![Rules Checks](https://github.com/spaze/coding-standard/actions/workflows/rules.yml/badge.svg)](https://github.com/spaze/coding-standard/actions/workflows/rules.yml)
56

67
```
78
composer require --dev spaze/coding-standard

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"php": "^7.2 || ^8.0",
1717
"slevomat/coding-standard": "^8.9"
1818
},
19+
"require-dev": {
20+
"ext-simplexml": "*"
21+
},
1922
"autoload": {
2023
"psr-4": {"Spaze\\CodingStandard\\": "src"}
2124
},

tests/sorted.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
$rules = simplexml_load_file('src/ruleset.xml');
5+
$refs = $refsSorted = [];
6+
foreach ($rules->rule as $rule) {
7+
$refs[] = (string)$rule['ref'];
8+
}
9+
$refsSorted = $refs;
10+
sort($refsSorted, SORT_NATURAL | SORT_FLAG_CASE);
11+
$diff = array_diff_assoc($refs, $refsSorted);
12+
if (count($diff)) {
13+
printf("Rules not sorted: %s should be listed before %s\n", $diff[array_key_last($diff)], $diff[array_key_first($diff)]);
14+
exit(1);
15+
} else {
16+
echo "Rules sorted just about right\n";
17+
exit(0);
18+
}

0 commit comments

Comments
 (0)