mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 08:50:50 +00:00
Add AppliedRectorCollector
This commit is contained in:
parent
9cd672b571
commit
e0ff2bcf60
1
ecs.yml
1
ecs.yml
|
@ -101,6 +101,7 @@ parameters:
|
|||
- '*Command.php'
|
||||
- '*NodeVisitor.php'
|
||||
- '*CompilerPass.php'
|
||||
- 'src/Rector/AbstractRector.php'
|
||||
- 'packages/Php/src/Rector/Unset_/UnsetCastRector.php'
|
||||
- 'packages/Php/src/Rector/ConstFetch/BarewordStringRector.php'
|
||||
# array type check
|
||||
|
|
29
src/Application/AppliedRectorCollector.php
Normal file
29
src/Application/AppliedRectorCollector.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Application;
|
||||
|
||||
final class AppliedRectorCollector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $rectorClasses = [];
|
||||
|
||||
public function addRectorClass(string $rectorClass): void
|
||||
{
|
||||
$this->rectorClasses[] = $rectorClass;
|
||||
}
|
||||
|
||||
public function reset(): void
|
||||
{
|
||||
$this->rectorClasses = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getRectorClasses(): array
|
||||
{
|
||||
return $this->rectorClasses;
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ namespace Rector\Console\Command;
|
|||
|
||||
use Nette\Utils\FileSystem;
|
||||
use PHPStan\AnalysedCodeException;
|
||||
use Rector\Application\AppliedRectorCollector;
|
||||
use Rector\Application\Error;
|
||||
use Rector\Application\ErrorCollector;
|
||||
use Rector\Application\FileProcessor;
|
||||
|
@ -103,6 +104,11 @@ final class ProcessCommand extends Command
|
|||
*/
|
||||
private $afterRectorCodingStyle;
|
||||
|
||||
/**
|
||||
* @var AppliedRectorCollector
|
||||
*/
|
||||
private $appliedRectorCollector;
|
||||
|
||||
public function __construct(
|
||||
FileProcessor $fileProcessor,
|
||||
SymfonyStyle $symfonyStyle,
|
||||
|
@ -115,7 +121,8 @@ final class ProcessCommand extends Command
|
|||
RectorGuard $rectorGuard,
|
||||
FileSystemFileProcessor $fileSystemFileProcessor,
|
||||
ErrorCollector $errorCollector,
|
||||
AfterRectorCodingStyle $afterRectorCodingStyle
|
||||
AfterRectorCodingStyle $afterRectorCodingStyle,
|
||||
AppliedRectorCollector $appliedRectorCollector
|
||||
) {
|
||||
parent::__construct();
|
||||
|
||||
|
@ -131,6 +138,7 @@ final class ProcessCommand extends Command
|
|||
$this->fileSystemFileProcessor = $fileSystemFileProcessor;
|
||||
$this->errorCollector = $errorCollector;
|
||||
$this->afterRectorCodingStyle = $afterRectorCodingStyle;
|
||||
$this->appliedRectorCollector = $appliedRectorCollector;
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
|
@ -264,7 +272,8 @@ final class ProcessCommand extends Command
|
|||
if ($newContent !== $oldContent) {
|
||||
$this->fileDiffs[] = new FileDiff(
|
||||
$fileInfo->getPathname(),
|
||||
$this->differAndFormatter->diffAndFormat($oldContent, $newContent)
|
||||
$this->differAndFormatter->diffAndFormat($oldContent, $newContent),
|
||||
$this->appliedRectorCollector->getRectorClasses()
|
||||
);
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -3,10 +3,7 @@
|
|||
namespace Rector\Console\Output;
|
||||
|
||||
use Rector\Application\Error;
|
||||
use Rector\Contract\Rector\RectorInterface;
|
||||
use Rector\PhpParser\NodeTraverser\RectorNodeTraverser;
|
||||
use Rector\Reporting\FileDiff;
|
||||
use Rector\YamlRector\YamlFileProcessor;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use function Safe\sprintf;
|
||||
|
||||
|
@ -17,24 +14,9 @@ final class ProcessCommandReporter
|
|||
*/
|
||||
private $symfonyStyle;
|
||||
|
||||
/**
|
||||
* @var RectorNodeTraverser
|
||||
*/
|
||||
private $rectorNodeTraverser;
|
||||
|
||||
/**
|
||||
* @var YamlFileProcessor
|
||||
*/
|
||||
private $yamlFileProcessor;
|
||||
|
||||
public function __construct(
|
||||
RectorNodeTraverser $rectorNodeTraverser,
|
||||
SymfonyStyle $symfonyStyle,
|
||||
YamlFileProcessor $yamlFileProcessor
|
||||
) {
|
||||
public function __construct(SymfonyStyle $symfonyStyle)
|
||||
{
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
$this->rectorNodeTraverser = $rectorNodeTraverser;
|
||||
$this->yamlFileProcessor = $yamlFileProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,6 +53,13 @@ final class ProcessCommandReporter
|
|||
$this->symfonyStyle->newLine();
|
||||
$this->symfonyStyle->writeln($fileDiff->getDiff());
|
||||
$this->symfonyStyle->newLine();
|
||||
|
||||
if ($fileDiff->getAppliedRectorClasses()) {
|
||||
$this->symfonyStyle->writeln('Applied rectors:');
|
||||
$this->symfonyStyle->newLine();
|
||||
$this->symfonyStyle->listing($fileDiff->getAppliedRectorClasses());
|
||||
$this->symfonyStyle->newLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace Rector\Rector;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Application\AppliedRectorCollector;
|
||||
use Rector\Contract\Rector\PhpRectorInterface;
|
||||
use Rector\PhpParser\Node\Builder\ExpressionAdder;
|
||||
use Rector\PhpParser\Node\Builder\PropertyAdder;
|
||||
|
@ -29,13 +30,31 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
|
|||
*/
|
||||
private $propertyAdder;
|
||||
|
||||
/**
|
||||
* @var AppliedRectorCollector
|
||||
*/
|
||||
private $appliedRectorCollector;
|
||||
|
||||
/**
|
||||
* @required
|
||||
*/
|
||||
public function setAbstractRectorDependencies(PropertyAdder $propertyAdder, ExpressionAdder $expressionAdder): void
|
||||
{
|
||||
public function setAbstractRectorDependencies(
|
||||
PropertyAdder $propertyAdder,
|
||||
ExpressionAdder $expressionAdder,
|
||||
AppliedRectorCollector $appliedRectorCollector
|
||||
): void {
|
||||
$this->propertyAdder = $propertyAdder;
|
||||
$this->expressionAdder = $expressionAdder;
|
||||
$this->appliedRectorCollector = $appliedRectorCollector;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @return array|Node[]|null
|
||||
*/
|
||||
public function beforeTraverse(array $nodes)
|
||||
{
|
||||
$this->appliedRectorCollector->reset();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -47,7 +66,19 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
|
|||
if (! $this->isMatchingNodeType($nodeClass)) {
|
||||
return null;
|
||||
}
|
||||
return $this->refactor($node);
|
||||
|
||||
$originalNode = $node;
|
||||
$node = $this->refactor($node);
|
||||
if ($node === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// changed!
|
||||
if ($originalNode !== $node) {
|
||||
$this->appliedRectorCollector->addRectorClass(static::class);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,6 +89,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
|
|||
{
|
||||
$nodes = $this->expressionAdder->addExpressionsToNodes($nodes);
|
||||
$nodes = $this->propertyAdder->addPropertiesToNodes($nodes);
|
||||
|
||||
return $this->removeFromNodes($nodes);
|
||||
}
|
||||
|
||||
|
|
|
@ -14,10 +14,19 @@ final class FileDiff
|
|||
*/
|
||||
private $file;
|
||||
|
||||
public function __construct(string $file, string $diff)
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $appliedRectorClasses = [];
|
||||
|
||||
/**
|
||||
* @param string[] $appliedRectorClasses
|
||||
*/
|
||||
public function __construct(string $file, string $diff, array $appliedRectorClasses = [])
|
||||
{
|
||||
$this->file = $file;
|
||||
$this->diff = $diff;
|
||||
$this->appliedRectorClasses = array_unique($appliedRectorClasses);
|
||||
}
|
||||
|
||||
public function getDiff(): string
|
||||
|
@ -29,4 +38,12 @@ final class FileDiff
|
|||
{
|
||||
return $this->file;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAppliedRectorClasses(): array
|
||||
{
|
||||
return $this->appliedRectorClasses;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user