Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add whitelist_patterns option to PhpMnd task #494

Merged
merged 2 commits into from
May 25, 2018
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
13 changes: 13 additions & 0 deletions doc/tasks/phpmnd.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ parameters:
tasks:
phpmnd:
directory: .
whitelist_patterns: []
exclude: []
exclude_name: []
exclude_path: []
Expand All @@ -35,6 +36,18 @@ parameters:

With this parameter you can define which directory you want to run `phpmnd` in (must be relative to cwd).

**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 exemple to validate only files in your `src/App/` and `src/AppBundle/` directories in a Symfony you can use
```yml
whitelist_patterns:
- /^src\/App\/(.*)/
- /^src\/AppBundle\/(.*)/
```

**exclude**

*Default: []*
Expand Down
1 change: 1 addition & 0 deletions spec/Task/PhpMndSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function it_should_have_configurable_options()
$options = $this->getConfigurableOptions();
$options->shouldBeAnInstanceOf(OptionsResolver::class);
$options->getDefinedOptions()->shouldContain('directory');
$options->getDefinedOptions()->shouldContain('whitelist_patterns');
$options->getDefinedOptions()->shouldContain('exclude');
$options->getDefinedOptions()->shouldContain('exclude_name');
$options->getDefinedOptions()->shouldContain('exclude_path');
Expand Down
17 changes: 14 additions & 3 deletions src/Task/PhpMnd.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function getConfigurableOptions()
$resolver = new OptionsResolver();
$resolver->setDefaults([
'directory' => '.',
'whitelist_patterns' => [],
'exclude' => [],
'exclude_name' => [],
'exclude_path' => [],
Expand All @@ -41,6 +42,7 @@ public function getConfigurableOptions()
]);

$resolver->addAllowedTypes('directory', ['string']);
$resolver->addAllowedTypes('whitelist_patterns', ['array']);
$resolver->addAllowedTypes('exclude', ['array']);
$resolver->addAllowedTypes('exclude_name', ['array']);
$resolver->addAllowedTypes('exclude_path', ['array']);
Expand All @@ -67,15 +69,24 @@ public function canRunInContext(ContextInterface $context)
*/
public function run(ContextInterface $context)
{
/** @var array $config */
$config = $this->getConfiguration();
$files = $context->getFiles()->extensions($config['triggered_by']);
/** @var array $whitelistPatterns */
$whitelistPatterns = $config['whitelist_patterns'];
/** @var array $extensions */
$extensions = $config['triggered_by'];

/** @var \GrumPHP\Collection\FilesCollection $files */
$files = $context->getFiles();
if (0 !== count($whitelistPatterns)) {
$files = $files->paths($whitelistPatterns);
}
$files = $files->extensions($extensions);

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

$config = $this->getConfiguration();

$arguments = $this->processBuilder->createArgumentsForCommand('phpmnd');
$arguments->addArgumentArray('--exclude=%s', $config['exclude']);
$arguments->addArgumentArray('--exclude-file=%s', $config['exclude_name']);
Expand Down