Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ grumphp:
phpcsfixer: ~
phplint: ~
phpmd: ~
phpmd3: ~
phpmnd: ~
phpparser: ~
phpspec: ~
Expand Down Expand Up @@ -121,6 +122,7 @@ Every task has its own default configuration. It is possible to overwrite the pa
- [PHP-CS-Fixer](tasks/phpcsfixer.md)
- [PHPLint](tasks/phplint.md)
- [PhpMd](tasks/phpmd.md)
- [PhpMd3](tasks/phpmd3.md)
- [PhpMnd](tasks/phpmnd.md)
- [PhpParser](tasks/phpparser.md)
- [Phpspec](tasks/phpspec.md)
Expand Down
2 changes: 1 addition & 1 deletion doc/tasks/phpmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The PhpMd task will sniff your code for bad coding standards.
***Composer***

```
composer require --dev phpmd/phpmd
composer require --dev phpmd/phpmd:^2.0
```

***Config***
Expand Down
68 changes: 68 additions & 0 deletions doc/tasks/phpmd3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# PhpMd3

The PhpMd3 task will sniff your code for bad coding standards.

It is the same as the `phpmd` task, but uses the `analyze` command of PHPMD 3.
Use the [`phpmd`](phpmd.md) task for PHPMD 2.

***Composer***

```
composer require --dev phpmd/phpmd:^3.0
```

***Config***

The task lives under the `phpmd3` namespace and has following configurable parameters:

```yaml
# grumphp.yml
grumphp:
tasks:
phpmd3:
whitelist_patterns: []
exclude: []
report_format: text
ruleset: ['phpmd.xml.dist']
triggered_by: ['php']
```

**whitelist_patterns**

*Default: []*

This is a list of regex patterns that will filter files to validate. With this option you can skip files like tests. This option is used in relation with the parameter `triggered_by`.
For example: whitelist files in `src/FolderA/` and `src/FolderB/` you can use
```yaml
whitelist_patterns:
- /^src\/FolderA\/(.*)/
- /^src\/FolderB\/(.*)/
```

**exclude**

*Default: []*

This is a list of patterns that will be ignored by phpmd. With this option you can skip directories like tests. Leave this option blank to run phpmd for every php file.

**report_format**

*Default: text*

This sets the output [renderer](https://phpmd.org/documentation/#renderers) of phpmd.
Available formats: ansi, text.

**ruleset**

*Default: [phpmd.xml.dist]*

With this parameter you will be able to configure the rule/rulesets you want to use. You can use the standard
sets provided by PhpMd or you can configure your own xml configuration as described in the [PhpMd Documentation](https://phpmd.org/documentation/creating-a-ruleset.html)

The full list of rules/rulesets can be found at [PhpMd Rules](https://phpmd.org/rules/index.html)

**triggered_by**

*Default: [php]*

This is a list of extensions to be sniffed.
7 changes: 7 additions & 0 deletions resources/config/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ services:
tags:
- {name: grumphp.task, task: phpmd}

GrumPHP\Task\PhpMd3:
arguments:
- '@process_builder'
- '@formatter.raw_process'
tags:
- {name: grumphp.task, task: phpmd3}

GrumPHP\Task\PhpMnd:
arguments:
- '@process_builder'
Expand Down
84 changes: 84 additions & 0 deletions src/Task/PhpMd3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace GrumPHP\Task;

use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Config\ConfigOptionsResolver;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class PhpMd3 extends AbstractExternalTask
{
public static function getConfigurableOptions(): ConfigOptionsResolver
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'whitelist_patterns' => [],
'exclude' => [],
'report_format' => 'text',
'ruleset' => ['phpmd.xml.dist'],
'triggered_by' => ['php'],
]);

$resolver->addAllowedTypes('whitelist_patterns', ['array']);
$resolver->addAllowedTypes('exclude', ['array']);
$resolver->addAllowedTypes('report_format', ['string']);
$resolver->addAllowedValues('report_format', ['text', 'ansi']);
$resolver->addAllowedTypes('ruleset', ['array']);
$resolver->addAllowedTypes('triggered_by', ['array']);

return ConfigOptionsResolver::fromOptionsResolver($resolver);
}

/**
* {@inheritdoc}
*/
public function canRunInContext(ContextInterface $context): bool
{
return $context instanceof GitPreCommitContext || $context instanceof RunContext;
}

/**
* {@inheritdoc}
*/
public function run(ContextInterface $context): TaskResultInterface
{
$config = $this->getConfig()->getOptions();

$files = $context->getFiles()
->extensions($config['triggered_by'])
->paths($config['whitelist_patterns']);

if (0 === \count($files)) {
return TaskResult::createSkipped($this, $context);
}

$arguments = $this->processBuilder->createArgumentsForCommand('phpmd');
$arguments->add('analyze');
$arguments->addOptionalArgument('--format=%s', $config['report_format']);

$arguments->addArgumentArray('--ruleset=%s', $config['ruleset']);
$arguments->addArgumentArray('--exclude=%s', $config['exclude']);
$arguments->addArgumentArray('--suffixes=%s', $config['triggered_by']);

$arguments->addFiles($files);

$process = $this->processBuilder->buildProcess($arguments);
$process->run();

if (!$process->isSuccessful()) {
return TaskResult::createFailed($this, $context, $this->formatter->format($process));
}

return TaskResult::createPassed($this, $context);
}
}
Loading
Loading