-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1054 from alessandroaussems/rector
Add Rector Task
- Loading branch information
Showing
6 changed files
with
325 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Rector | ||
|
||
Rector is a tool to instantly upgrade and automatically refactor your PHP 5.3+ code. | ||
It lives under the `rector` namespace and has following configurable parameters: | ||
|
||
## Composer | ||
```bash | ||
composer require --dev rectorphp/rector | ||
``` | ||
|
||
## Config | ||
```yaml | ||
# grumphp.yml | ||
grumphp: | ||
tasks: | ||
rector: | ||
config: null | ||
triggered_by: ['php'] | ||
ignore_patterns: [] | ||
clear_cache: true | ||
no_diffs: false | ||
``` | ||
**config** | ||
*Default: null* | ||
With this parameter you can specify the path your project's configuration file. When 'null' rector will run with the default file: rector.php | ||
**triggered_by** | ||
*Default: [php]* | ||
This is a list of extensions to be sniffed. | ||
**ignore_patterns** | ||
*Default: []* | ||
This is a list of patterns that will be ignored by Rector. With this option you can skip files like | ||
tests. Leave this option blank to run Rector for every php file/directory specified in your | ||
configuration. | ||
**clear_cache** | ||
*Default: true* | ||
With this parameter you can run Rector without using the cache. | ||
**no_diffs** | ||
*Default: false* | ||
With this parameter you can run Rector without showing file diffs. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GrumPHP\Task; | ||
|
||
use GrumPHP\Fixer\Provider\FixableProcessResultProvider; | ||
use GrumPHP\Runner\TaskResult; | ||
use GrumPHP\Runner\TaskResultInterface; | ||
use GrumPHP\Task\Context\ContextInterface; | ||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\Process\Process; | ||
|
||
class Rector extends AbstractExternalTask | ||
{ | ||
public static function getConfigurableOptions(): OptionsResolver | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefaults([ | ||
'config' => null, | ||
'triggered_by' => ['php'], | ||
'ignore_patterns' => [], | ||
'clear_cache' => true, | ||
'no_diffs' => false, | ||
]); | ||
|
||
$resolver->addAllowedTypes('config', ['null', 'string']); | ||
$resolver->addAllowedTypes('triggered_by', ['array']); | ||
$resolver->addAllowedTypes('ignore_patterns', ['array']); | ||
$resolver->addAllowedTypes('clear_cache', ['bool']); | ||
$resolver->addAllowedTypes('no_diffs', ['bool']); | ||
|
||
return $resolver; | ||
} | ||
|
||
public function canRunInContext(ContextInterface $context): bool | ||
{ | ||
return $context instanceof RunContext || $context instanceof GitPreCommitContext; | ||
} | ||
|
||
public function run(ContextInterface $context): TaskResultInterface | ||
{ | ||
$config = $this->getConfig()->getOptions(); | ||
|
||
$files = $context | ||
->getFiles() | ||
->notPaths($config['ignore_patterns']) | ||
->extensions($config['triggered_by']); | ||
|
||
if (0 === \count($files)) { | ||
return TaskResult::createSkipped($this, $context); | ||
} | ||
|
||
$arguments = $this->processBuilder->createArgumentsForCommand('rector'); | ||
$arguments->add('process'); | ||
$arguments->add('--dry-run'); | ||
$arguments->add('--ansi'); | ||
$arguments->add('--no-progress-bar'); | ||
|
||
$arguments->addOptionalArgument('--config=%s', $config['config']); | ||
$arguments->addOptionalArgument('--clear-cache', $config['clear_cache']); | ||
$arguments->addOptionalArgument('--no-diffs', $config['no_diffs']); | ||
|
||
if ($context instanceof GitPreCommitContext) { | ||
$arguments->addFiles($files); | ||
} | ||
|
||
$process = $this->processBuilder->buildProcess($arguments); | ||
$process->run(); | ||
|
||
if (!$process->isSuccessful()) { | ||
return FixableProcessResultProvider::provide( | ||
TaskResult::createFailed($this, $context, $this->formatter->format($process)), | ||
function () use ($arguments): Process { | ||
$arguments->removeElement('--dry-run'); | ||
|
||
return $this->processBuilder->buildProcess($arguments); | ||
} | ||
); | ||
} | ||
|
||
return TaskResult::createPassed($this, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GrumPHPTest\Unit\Task; | ||
|
||
use GrumPHP\Formatter\ProcessFormatterInterface; | ||
use GrumPHP\Formatter\RawProcessFormatter; | ||
use GrumPHP\Runner\FixableTaskResult; | ||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use GrumPHP\Task\Rector; | ||
use GrumPHP\Task\TaskInterface; | ||
use GrumPHP\Test\Task\AbstractExternalTaskTestCase; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
|
||
class RectorTest extends AbstractExternalTaskTestCase | ||
{ | ||
/** | ||
* @var ProcessFormatterInterface|ObjectProphecy | ||
*/ | ||
protected $formatter; | ||
|
||
protected function provideTask(): TaskInterface | ||
{ | ||
$this->formatter = $this->prophesize(RawProcessFormatter::class); | ||
|
||
return new Rector( | ||
$this->processBuilder->reveal(), | ||
$this->formatter->reveal() | ||
); | ||
} | ||
|
||
public function provideConfigurableOptions(): iterable | ||
{ | ||
yield 'defaults' => [ | ||
[], | ||
[ | ||
'config' => null, | ||
'triggered_by' => ['php'], | ||
'ignore_patterns' => [], | ||
'clear_cache' => true, | ||
'no_diffs' => false, | ||
] | ||
]; | ||
} | ||
|
||
public function provideRunContexts(): iterable | ||
{ | ||
yield 'run-context' => [ | ||
true, | ||
$this->mockContext(RunContext::class) | ||
]; | ||
|
||
yield 'pre-commit-context' => [ | ||
true, | ||
$this->mockContext(GitPreCommitContext::class) | ||
]; | ||
|
||
yield 'other' => [ | ||
false, | ||
$this->mockContext() | ||
]; | ||
} | ||
|
||
public function provideFailsOnStuff(): iterable | ||
{ | ||
yield 'exitCode1' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['hello.php']), | ||
function () { | ||
$this->mockProcessBuilder('rector', $process = $this->mockProcess(1)); | ||
|
||
$this->formatter->format($process)->willReturn($message = 'message'); | ||
}, | ||
'message', | ||
FixableTaskResult::class | ||
]; | ||
} | ||
|
||
public function providePassesOnStuff(): iterable | ||
{ | ||
yield 'exitCode0' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['hello.php']), | ||
function () { | ||
$this->mockProcessBuilder('rector', $this->mockProcess(0)); | ||
} | ||
]; | ||
} | ||
|
||
public function provideSkipsOnStuff(): iterable | ||
{ | ||
yield 'no-files' => [ | ||
[], | ||
$this->mockContext(RunContext::class), | ||
function () {} | ||
]; | ||
yield 'no-files-after-triggered-by' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['notaphpfile.txt']), | ||
function () {} | ||
]; | ||
yield 'no-files-after-ignore-patterns' => [ | ||
[ | ||
'ignore_patterns' => ['test/'], | ||
], | ||
$this->mockContext(RunContext::class, ['test/file.php']), | ||
function () {} | ||
]; | ||
} | ||
|
||
public function provideExternalTaskRuns(): iterable | ||
{ | ||
yield 'defaults' => [ | ||
[], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
'rector', | ||
[ | ||
'process', | ||
'--dry-run', | ||
'--ansi', | ||
'--no-progress-bar', | ||
'--clear-cache', | ||
] | ||
]; | ||
yield 'config' => [ | ||
[ | ||
'config' => 'rector-config.php', | ||
], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
'rector', | ||
[ | ||
'process', | ||
'--dry-run', | ||
'--ansi', | ||
'--no-progress-bar', | ||
'--config=rector-config.php', | ||
'--clear-cache', | ||
] | ||
]; | ||
yield 'no-clear-cache' => [ | ||
[ | ||
'clear_cache' => false, | ||
], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
'rector', | ||
[ | ||
'process', | ||
'--dry-run', | ||
'--ansi', | ||
'--no-progress-bar', | ||
] | ||
]; | ||
yield 'no-diffs' => [ | ||
[ | ||
'no_diffs' => true, | ||
], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
'rector', | ||
[ | ||
'process', | ||
'--dry-run', | ||
'--ansi', | ||
'--no-progress-bar', | ||
'--clear-cache', | ||
'--no-diffs' | ||
] | ||
]; | ||
} | ||
} |