Last active
August 30, 2023 14:30
-
-
Save dfelton/24f4122c0f5107b77f136188dcc89fbd to your computer and use it in GitHub Desktop.
Generates XML for a PHPCS exlusion list of existing violations, segregated by rule name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
./vendor/bin/phpcs . > phpcs_results | |
php -r ' | |
$contents = file_get_contents("phpcs_results"); | |
$contents = explode(PHP_EOL, $contents); | |
$currentFile = ""; | |
$rules = []; | |
foreach ($contents as $line) { | |
if (strpos($line, "FILE: " . __DIR__) !== false) { | |
$currentFile = trim(explode(".php", explode(basename(__DIR__) . "/", $line)[1])[0]) . ".php"; | |
$currentFile = str_replace( | |
[".less.php", ".html.php", ".phtml.php", ".js.php", ".css.php"], | |
[".less", ".html", ".phtml", ".js", ".css"], | |
$currentFile | |
); | |
} | |
$line = str_replace( | |
[ | |
"()", | |
"(...)", | |
"(7)", | |
"(8)", | |
"(6)", | |
"(SHA-256, SHA-512 etc.)", | |
"(dash)", | |
"(underscore)", | |
"(int)" | |
"(string)", | |
], | |
"", | |
$line | |
); | |
if (strpos($line, "(") !== false && strpos($line, ")") !== false) { | |
$rule = trim(str_replace( | |
["(",")"], | |
["",""], | |
substr( | |
$line, | |
strpos($line, " (") | |
) | |
)); | |
if (!in_array($currentFile, $rules[$rule] ?? [])) { | |
$rules[$rule][] = $currentFile; | |
} | |
} | |
} | |
ksort($rules); | |
foreach ($rules as $rule => $files) { | |
ksort($files); | |
if ( | |
strpos($rule, "|") === 0 || | |
strpos($rule, "data") === 0 || | |
strpos($rule, "WARNING") !== false || | |
strpos($rule, "ERROR") !== false | |
) { | |
continue; | |
} | |
echo " <rule ref=\"$rule\">" . PHP_EOL; | |
foreach ($files as $file) { | |
echo " <exclude-pattern>$file</exclude-pattern>" . PHP_EOL; | |
} | |
echo " </rule>" . PHP_EOL; | |
} | |
' > rules.xml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment