Skip to content

Commit fb7a3bb

Browse files
authored
Merge pull request #2 from Space48/S48-635-reporting-format
S48 635 reporting format
2 parents 35cc7d7 + c386477 commit fb7a3bb

File tree

8 files changed

+158
-2
lines changed

8 files changed

+158
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Do you want to create a grumphp.yml file? [Yes]: no
5050
Copy files and install npm packages:
5151
```shell
5252
warden env exec php-fpm chmod +x vendor/space48/magento2-code-quality/script/install.sh
53-
warden env exec ./vendor/space48/magento2-code-quality/script/install.sh
53+
warden env exec php-fpm ./vendor/space48/magento2-code-quality/script/install.sh
5454
vendor/bin/grumphp git:init
5555
```
5656

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"license": "proprietary",
66
"require": {
77
"phpro/grumphp": "^1.3",
8+
"symfony/dom-crawler": "*",
89
"magento/magento-coding-standard": "*",
910
"phpmd/phpmd": "^2",
1011
"squizlabs/php_codesniffer": ">=2.9.2 <4.0.0",

grumphp.yml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
1+
## rewrite classes to format output to have rule names in it
2+
services:
3+
formatter.phpmd:
4+
class: \Space48\CodeQuality\Formatter\PhpmdFormatter
5+
formatter.phpcs:
6+
class: \Space48\CodeQuality\Formatter\PhpcsFormatter
7+
GrumPHP\Task\PhpMd:
8+
## rewrite class to whitelist 'xml' format
9+
class: \Space48\CodeQuality\Task\PhpMd
10+
## set 'phpmd' formatter for PhpMd Task
11+
arguments:
12+
- '@process_builder'
13+
- '@formatter.phpmd'
14+
tags:
15+
- { name: grumphp.task, task: phpmd }
16+
## workaround to add '-s' option to PhpCs Task
17+
process_builder:
18+
class: \Space48\CodeQuality\Process\ProcessBuilder
19+
arguments:
20+
- '@GrumPHP\Locator\ExternalCommand'
21+
- '@grumphp.io'
22+
- '@GrumPHP\Configuration\Model\ProcessConfig'
23+
public: true
24+
25+
## linter settings
126
grumphp:
227
git_hook_variables:
328
EXEC_GRUMPHP_COMMAND: 'warden env run --rm php-fpm'
429
ignore_unstaged_changes: false
30+
fixer:
31+
enabled: false
32+
fix_by_default: true
533
tasks:
634
phpmd:
735
whitelist_patterns:
836
- /^app/
937
triggered_by: [ 'php', 'phtml' ]
1038
exclude: [ ]
11-
report_format: ansi
39+
report_format: xml
1240
ruleset: [ 'phpmd.xml' ]
1341
phpcs:
1442
standard: [ 'ruleset.xml' ]

rulesets/PhpMd/extra.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
77
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
88

9+
<rule ref="rulesets/naming.xml" >
10+
<exclude name="LongVariable" />
11+
<exclude name="ShortMethodName" />
12+
</rule>
13+
14+
<rule ref="rulesets/cleancode.xml" >
15+
<exclude name="BooleanArgumentFlag" />
16+
<exclude name="ElseExpression" />
17+
<exclude name="StaticAccess" />
18+
<exclude name="MissingImport" />
19+
</rule>
20+
921
<rule name="ExcessiveParameterList"
1022
since="0.1"
1123
message="The {0} {1} has {2} parameters. Consider reducing the number of parameters to less than {3}."
@@ -32,4 +44,5 @@ class Foo {
3244
</example>
3345

3446
</rule>
47+
3548
</ruleset>

src/Formatter/PhpcsFormatter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Space48\CodeQuality\Formatter;
4+
5+
use Symfony\Component\Process\Process;
6+
7+
class PhpcsFormatter extends \GrumPHP\Formatter\PhpcsFormatter
8+
{
9+
10+
public function format(Process $process): string
11+
{
12+
$output = sprintf("\n%'-90s\n", '-');
13+
$output .= 'PHPCS. (To exclude: "//phpcs:disable Rule.Name". Disables rule to the end of the file)' . "\n";
14+
$output .= sprintf("%'-90s\n\n", '-');
15+
16+
return $output . parent::format($process);
17+
}
18+
}

src/Formatter/PhpmdFormatter.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Space48\CodeQuality\Formatter;
4+
5+
use Symfony\Component\Process\Process;
6+
use Symfony\Component\DomCrawler\Crawler;
7+
8+
class PhpmdFormatter implements \GrumPHP\Formatter\ProcessFormatterInterface
9+
{
10+
11+
public function format(Process $process): string
12+
{
13+
$output = trim($process->getOutput());
14+
if (!$output) {
15+
return $process->getErrorOutput();
16+
}
17+
18+
$xml = new Crawler($output);
19+
try {
20+
// will throw an exception if $output is not in expected format
21+
$xml->filter('file')->children();
22+
} catch (\Exception $e) {
23+
return $output;
24+
}
25+
26+
$formattedOutput = 'PHPMD. (To exclude rule add "@SuppressWarnings(PHPMD.RuleName)" to function or class phpdoc)' . "\n";
27+
foreach ($xml->filter('file') as $node) {
28+
$name = $node->attributes->getNamedItem('name')->value;
29+
$formattedOutput .= sprintf("%'-". \strlen($name) . "s\n", '-');
30+
$formattedOutput .= $name . "\n";
31+
$formattedOutput .= sprintf("%'-". \strlen($name) . "s\n", '-');
32+
33+
foreach ($node->childNodes as $violation) {
34+
if ($violation->nodeName == '#text') {
35+
continue;
36+
}
37+
38+
$formattedOutput .= sprintf(
39+
"% 11s | Line: % 3s | %s | %s\n",
40+
strtoupper($violation->nodeName),
41+
$violation->attributes->getNamedItem('beginline')->value,
42+
$violation->attributes->getNamedItem('rule')->value,
43+
trim($violation->textContent)
44+
);
45+
}
46+
}
47+
48+
return $formattedOutput;
49+
}
50+
}

src/Process/ProcessBuilder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Space48\CodeQuality\Process;
4+
5+
use GrumPHP\Collection\ProcessArgumentsCollection;
6+
use GrumPHP\Exception\PlatformException;
7+
use Symfony\Component\Process\Process;
8+
9+
class ProcessBuilder extends \GrumPHP\Process\ProcessBuilder
10+
{
11+
12+
/**
13+
* @throws PlatformException
14+
*/
15+
public function buildProcess(ProcessArgumentsCollection $arguments): Process
16+
{
17+
// A workaround to add '-s' flag to PhpCs linter that prints rule name next to violation.
18+
// Alternative is to completely replace PhpCs Task as runtime arguments are hardcoded there.
19+
if (array_filter($arguments->toArray(), function ($argument) { return preg_match('/\/phpcs$/', $argument); })) {
20+
$arguments->add('-s');
21+
}
22+
23+
return parent::buildProcess($arguments);
24+
}
25+
26+
}

src/Task/PhpMd.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Space48\CodeQuality\Task;
4+
5+
use Symfony\Component\OptionsResolver\OptionsResolver;
6+
7+
/**
8+
* PhpMd task.
9+
*/
10+
class PhpMd extends \GrumPHP\Task\PhpMd
11+
{
12+
public static function getConfigurableOptions(): OptionsResolver
13+
{
14+
$resolver = parent::getConfigurableOptions();
15+
$resolver->addAllowedValues('report_format', ['xml']);
16+
17+
return $resolver;
18+
}
19+
20+
}

0 commit comments

Comments
 (0)