Skip to content

Instantly share code, notes, and snippets.

@NeilMasters
Created July 26, 2024 10:03
Show Gist options
  • Save NeilMasters/a38356930554c89b63f7d713eb8aae48 to your computer and use it in GitHub Desktop.
Save NeilMasters/a38356930554c89b63f7d713eb8aae48 to your computer and use it in GitHub Desktop.
PHPMD Pretty
#!/usr/bin/env php
<?php
/**
* A very simple and basic utility that can be used to pipe phpmd errors and format
* in a cleaner way. This suits MY requirements and you will probably want line numbers
* etc so edit as you see fit.
*
* Example use:
* vendor/bin/phpmd ./src xml ./ruleset.xml | bin/phpmd-pretty
*/
$data = stream_get_contents(STDIN);
$xmlDom = simplexml_load_string(
$data,
"SimpleXMLElement", LIBXML_NOCDATA
);
foreach ($xmlDom->file as $file) {
echo sprintf(
"%s %s",
trim($file->attributes()->name->__toString()),
PHP_EOL
);
foreach ($file->violation as $violation) {
echo sprintf(
"%s %s",
trim($violation->__toString()),
PHP_EOL
);
}
echo PHP_EOL;
}
@NeilMasters
Copy link
Author

If you want a message when no violations are found instead of nothing being displayed add this above the foreach.

if ($xmlDom->file->count() === 0) {
    echo sprintf(
        "%s %s",
        'No violations found.',
        PHP_EOL
    );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment