Skip to content

Commit

Permalink
Merge pull request #32 from mitrpaka/feature/phpstan-drupal
Browse files Browse the repository at this point in the history
Add support for Drupal deprecation testing
  • Loading branch information
guncha25 authored Oct 9, 2019
2 parents b62063a + 8c5fecf commit a0410c3
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This repository currently has following checks:
* Cognitive complexity and other ecs sniffs - [ecs](src/Task/Ecs/README.md)
* Yaml syntax - [yaml_lint](src/Task/YamlLint/README.md)
* Json syntax - [json_lint](src/Task/JsonLint/README.md)
* Deprecation testing - [phpstan_check_deprecation](src/Task/PhpstanCheckDeprecation/README.md)

## Pre-requisites

Expand All @@ -35,6 +36,7 @@ This needs to be done only once either while creating a project or enabling code
```
composer require wunderio/code-quality --dev
cp vendor/wunderio/code-quality/config/grumphp.yml ./grumphp.yml
cp vendor/wunderio/code-quality/config/phpstan.neon ./phpstan.neon
```

The commit hook for GrumPHP is automatically installed on composer require.
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
"wunderio/ecs-drupal": "^0.2.0",
"symplify/coding-standard": "^6.0",
"squizlabs/php_codesniffer": "^3.4",
"dealerdirect/phpcodesniffer-composer-installer": "*"
"dealerdirect/phpcodesniffer-composer-installer": "*",
"mglaman/phpstan-drupal": "^0.11.1",
"phpstan/phpstan-deprecation-rules": "^0.11.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions config/grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ parameters:
check_file_permissions: ~
php_check_syntax: ~
phpcs: ~
phpstan_check_deprecation: ~
ecs: ~
yaml_lint: ~
json_lint: ~
Expand All @@ -17,5 +18,6 @@ parameters:
- Wunderio\GrumPHP\Task\CheckFilePermissions\CheckFilePermissionsExtensionLoader
- Wunderio\GrumPHP\Task\Ecs\EcsExtensionLoader
- Wunderio\GrumPHP\Task\Phpcs\PhpcsExtensionLoader
- Wunderio\GrumPHP\Task\PhpstanCheckDeprecation\PhpstanCheckDeprecationExtensionLoader
- Wunderio\GrumPHP\Task\YamlLint\YamlLintExtensionLoader
- Wunderio\GrumPHP\Task\JsonLint\JsonLintExtensionLoader
12 changes: 12 additions & 0 deletions config/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
customRulesetUsed: true
reportUnmatchedIgnoredErrors: false
# Ignore phpstan-drupal extension's rules.
ignoreErrors:
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
- '#Plugin definitions cannot be altered.#'
- '#Missing cache backend declaration for performance.#'
- '#Plugin manager has cache backend specified but does not declare cache tags.#'
includes:
- vendor/mglaman/phpstan-drupal/extension.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
4 changes: 4 additions & 0 deletions grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ parameters:
run_on: ['src', 'tests']
phpcs:
run_on: ['src', 'tests']
phpstan_check_deprecation:
run_on: ['src']
configuration: phpstan.neon
ecs:
config: ecs.yml
yaml_lint: ~
Expand All @@ -20,5 +23,6 @@ parameters:
- Wunderio\GrumPHP\Task\CheckFilePermissions\CheckFilePermissionsExtensionLoader
- Wunderio\GrumPHP\Task\Ecs\EcsExtensionLoader
- Wunderio\GrumPHP\Task\Phpcs\PhpcsExtensionLoader
- Wunderio\GrumPHP\Task\PhpstanCheckDeprecation\PhpstanCheckDeprecationExtensionLoader
- Wunderio\GrumPHP\Task\YamlLint\YamlLintExtensionLoader
- Wunderio\GrumPHP\Task\JsonLint\JsonLintExtensionLoader
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
parameters:
customRulesetUsed: true
reportUnmatchedIgnoredErrors: false
# Ignore phpstan-drupal extension's rules.
ignoreErrors:
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
- '#Plugin definitions cannot be altered.#'
- '#Missing cache backend declaration for performance.#'
- '#Plugin manager has cache backend specified but does not declare cache tags.#'
includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Wunderio\GrumPHP\Task\PhpstanCheckDeprecation;

use Wunderio\GrumPHP\Task\AbstractExternalExtensionLoader;

/**
* Class PhpstanCheckDeprecationExtensionLoader.
*
* @package Wunderio\GrumPHP\Task
*/
class PhpstanCheckDeprecationExtensionLoader extends AbstractExternalExtensionLoader {}
38 changes: 38 additions & 0 deletions src/Task/PhpstanCheckDeprecation/PhpstanCheckDeprecationTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types = 1);

namespace Wunderio\GrumPHP\Task\PhpstanCheckDeprecation;

use GrumPHP\Collection\ProcessArgumentsCollection;
use Wunderio\GrumPHP\Task\AbstractMultiPathProcessingTask;

/**
* Class PhpstanCheckDeprecationTask.
*
* @package Wunderio\GrumPHP\Task
*/
class PhpstanCheckDeprecationTask extends AbstractMultiPathProcessingTask {

/**
* {@inheritdoc}
*/
public function buildArguments(iterable $files): ProcessArgumentsCollection {
$arguments = $this->processBuilder->createArgumentsForCommand('phpstan');
$config = $this->getConfiguration();
$arguments->add('analyse');
$arguments->addOptionalArgument('--autoload-file=%s', $config['autoload_file']);
$arguments->addOptionalArgument('--configuration=%s', $config['configuration']);
$arguments->addOptionalArgument('--memory-limit=%s', $config['memory_limit']);
$arguments->addOptionalMixedArgument('--level=%s', $config['level']);
$arguments->add('--no-ansi');
$arguments->add('--no-interaction');
$arguments->add('--no-progress');

foreach ($files as $file) {
$arguments->add($file);
}
return $arguments;
}

}
31 changes: 31 additions & 0 deletions src/Task/PhpstanCheckDeprecation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# phpstan_check_deprecation

Check Drupal code against deprecation rules.

### grumphp.yml (with current defaults):
````yml
parameters:
tasks:
phpstan_check_deprecation:
ignore_patterns:
- '/vendor/'
- '/node_modules/'
- '/core/'
- '/libraries/'
extensions: ['php', 'inc', 'module', 'install', 'theme']
run_on: ['.']
autoload_file:
defaults: ~
allowed_types: ['string', 'null']
configuration:
defaults: 'phpstan.neon'
allowed_types: ['string', 'null']
memory_limit:
defaults: ~
allowed_types: ['string', 'null']
level:
defaults: ~
allowed_types: ['string', 'null']
extensions:
- Wunderio\GrumPHP\Task\PhpstanCheckDeprecation\PhpstanCheckDeprecationExtensionLoader
````
27 changes: 27 additions & 0 deletions src/Task/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,33 @@ Wunderio\GrumPHP\Task\Phpcs\PhpcsTask:
parallel:
defaults: 20
allowed_types: ['int']
Wunderio\GrumPHP\Task\PhpstanCheckDeprecation\PhpstanCheckDeprecationTask:
options:
ignore_patterns:
defaults:
- '/vendor/'
- '/node_modules/'
- '/core/'
- '/libraries/'
allowed_types: ['array']
extensions:
defaults: ['php', 'inc', 'module', 'install', 'theme']
allowed_types: ['array']
run_on:
defaults: ['.']
allowed_types: ['array']
autoload_file:
defaults: ~
allowed_types: ['string', 'null']
configuration:
defaults: 'phpstan.neon'
allowed_types: ['string', 'null']
memory_limit:
defaults: ~
allowed_types: ['string', 'null']
level:
defaults: ~
allowed_types: ['string', 'null']
Wunderio\GrumPHP\Task\YamlLint\YamlLintTask:
is_file_specific: true
arguments:
Expand Down
65 changes: 65 additions & 0 deletions tests/PhpstanCheckDeprecation/PhpstanCheckDeprecationTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* @file
* Tests covering PhpstanCheckDeprecationTask.
*/

declare(strict_types = 1);

use GrumPHP\Collection\ProcessArgumentsCollection;
use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Process\ProcessBuilder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;
use Wunderio\GrumPHP\Task\PhpstanCheckDeprecation\PhpstanCheckDeprecationTask;

/**
* Class PhpstanTaskTest.
*/
final class PhpstanCheckDeprecationTaskTest extends TestCase {

/**
* Test building arguments.
*
* @covers \Wunderio\GrumPHP\Task\Phpstan\PhpstanDrupalCheckTask::buildArguments
*/
public function testBuildsProcessArguments(): void {
$processBuilder = $this->createMock(ProcessBuilder::class);
$stub = $this->getMockBuilder(PhpstanCheckDeprecationTask::class)->setConstructorArgs([
$this->createMock(GrumPHP::class),
$processBuilder,
$this->createMock(ProcessFormatterInterface::class),
])
->setMethodsExcept(['buildArguments'])->getMock();
$arguments = $this->createMock(ProcessArgumentsCollection::class);

$files = ['file1.php', 'file2.php', 'dir1/'];
$processBuilder->expects($this->once())
->method('createArgumentsForCommand')
->willReturn($arguments);

$arguments->expects($this->exactly(7))->method('add');
$config = [];
foreach ($this->getConfigurations() as $name => $option) {
$config[$name] = $option['defaults'];
}
$stub->expects($this->once())->method('getConfiguration')->willReturn($config);

$actual = $stub->buildArguments($files);
$this->assertInstanceOf(ProcessArgumentsCollection::class, $actual);
}

/**
* Gets task configurations.
*
* @return array
* Array of options.
*/
protected function getConfigurations(): array {
$tasks = Yaml::parseFile(__DIR__ . '/../../src/Task/tasks.yml');
return $tasks[PhpstanCheckDeprecationTask::class]['options'];
}

}

0 comments on commit a0410c3

Please sign in to comment.