Skip to content

Commit 0802c3c

Browse files
committed
PHPCS -i: display standards in same order cross-platform
I noticed this while debugging something completely different: the display order of the registered standards can vary wildly between different computers/OSes. For example, on Windows, they always seem to be ordered alphabetically: > The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz and Zend And in case there are external standards registered, they are sorted alphabetically per "set of standards", i.e. per registered "installed path": > The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and PHPCompatibility However, on Linux, I was seeing a completely different (more random) order, like for example: > The installed coding standards are PEAR, PSR2, PSR1, Squiz, PSR12, MySource and Zend **I'd like to propose to always display the standards in the same order and to use natural ordering per "set of standards" for this.** The order of external standards would still be determined by the order in which those standards are registered to the `installed_paths` configuration setting, but for a group of standards within the same `installed_path`, the standards would now be ordered by natural order too. For the PHPCS native standards, this will result in the standards displaying the same cross-OS, like this: > The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz and Zend Take note of the "saner" ordering of the PSR standards which also improves the experience on Windows. Implementation note: to avoid using the `array_merge()`, I've added the standards name as keys to the array as well, which allows for safely using array union instead.
1 parent 498a939 commit 0802c3c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Util/Standards.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ public static function getInstalledStandards(
180180
// Check if the installed dir is actually a standard itself.
181181
$csFile = $standardsDir.'/ruleset.xml';
182182
if (is_file($csFile) === true) {
183-
$installedStandards[] = basename($standardsDir);
183+
$basename = basename($standardsDir);
184+
$installedStandards[$basename] = $basename;
184185
continue;
185186
}
186187

@@ -190,6 +191,7 @@ public static function getInstalledStandards(
190191
}
191192

192193
$di = new \DirectoryIterator($standardsDir);
194+
$standardsInDir = [];
193195
foreach ($di as $file) {
194196
if ($file->isDir() === true && $file->isDot() === false) {
195197
$filename = $file->getFilename();
@@ -202,10 +204,13 @@ public static function getInstalledStandards(
202204
// Valid coding standard dirs include a ruleset.
203205
$csFile = $file->getPathname().'/ruleset.xml';
204206
if (is_file($csFile) === true) {
205-
$installedStandards[] = $filename;
207+
$standardsInDir[$filename] = $filename;
206208
}
207209
}
208210
}
211+
212+
natsort($standardsInDir);
213+
$installedStandards += $standardsInDir;
209214
}//end foreach
210215

211216
return $installedStandards;

0 commit comments

Comments
 (0)