File reports (#6130)

This commit is contained in:
Tomas Votruba 2021-04-14 04:15:10 +02:00 committed by GitHub
parent b03fa60c52
commit 1e24618a9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 141 deletions

View File

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver;
use Rector\ChangesReporting\Annotation\RectorsChangelogResolver;
use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\HttpKernel\RectorKernel;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source\RectorWithChangelog;
@ -46,30 +46,12 @@ final class RectorsChangelogResolverTest extends AbstractKernelTestCase
private function createFileDiff(): FileDiff
{
// This is by intention to test the array_unique functionality
$rectorWithFileAndLineChange1 = new RectorWithFileAndLineChange(
new RectorWithChangelog(),
__DIR__ . '/Source/RectorWithChangelog.php',
1
);
$rectorWithLineChange = new RectorWithLineChange(new RectorWithChangelog(), 1);
$rectorWithLineChange2 = new RectorWithLineChange(new RectorWithChangelog(), 1);
$rectorWithLineChange3 = new RectorWithLineChange(new RectorWithOutChangelog(), 1);
$rectorWithFileAndLineChange2 = new RectorWithFileAndLineChange(
new RectorWithChangelog(),
__DIR__ . '/Source/RectorWithChangelog.php',
1
);
$rectorWithLineChanges = [$rectorWithLineChange, $rectorWithLineChange2, $rectorWithLineChange3];
$rectorWithFileAndLineChange3 = new RectorWithFileAndLineChange(
new RectorWithOutChangelog(),
__DIR__ . '/Source/RectorWithOutChangelog.php',
1
);
$rectorWithFileAndLineChanges = [
$rectorWithFileAndLineChange1,
$rectorWithFileAndLineChange2,
$rectorWithFileAndLineChange3,
];
return new FileDiff(new SmartFileInfo(__FILE__), 'foo', 'foo', $rectorWithFileAndLineChanges);
return new FileDiff(new SmartFileInfo(__FILE__), 'foo', 'foo', $rectorWithLineChanges);
}
}

View File

@ -5,22 +5,15 @@ declare(strict_types=1);
namespace Rector\ChangesReporting\Collector;
use PhpParser\Node;
use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Logging\CurrentRectorProvider;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Symplify\SmartFileSystem\SmartFileInfo;
final class RectorChangeCollector
{
/**
* @var RectorWithFileAndLineChange[]
*/
private $rectorWithFileAndLineChanges = [];
/**
* @var CurrentRectorProvider
*/
@ -39,17 +32,10 @@ final class RectorChangeCollector
$this->currentFileProvider = $currentFileProvider;
}
/**
* @return RectorWithFileAndLineChange[]
*/
public function getRectorChangesByFileInfo(SmartFileInfo $smartFileInfo): array
public function notifyFileChange(File $file, Node $node, RectorInterface $rector): void
{
return array_filter(
$this->rectorWithFileAndLineChanges,
function (RectorWithFileAndLineChange $rectorWithFileAndLineChange) use ($smartFileInfo): bool {
return $rectorWithFileAndLineChange->getRealPath() === $smartFileInfo->getRealPath();
}
);
$rectorWithLineChange = new RectorWithLineChange($rector, $node->getLine());
$file->addRectorClassWithLine($rectorWithLineChange);
}
public function notifyNodeFileInfo(Node $node): void
@ -61,27 +47,12 @@ final class RectorChangeCollector
return;
}
$smartFileInfo = $file->getSmartFileInfo();
$currentRector = $this->currentRectorProvider->getCurrentRector();
if (! $currentRector instanceof RectorInterface) {
throw new ShouldNotHappenException();
}
// @old
$this->addRectorClassWithLine($currentRector, $smartFileInfo, $node->getLine());
// @new
$rectorWithLineChange = new RectorWithLineChange($currentRector, $node->getLine());
$file->addRectorClassWithLine($rectorWithLineChange);
}
private function addRectorClassWithLine(RectorInterface $rector, SmartFileInfo $smartFileInfo, int $line): void
{
$this->rectorWithFileAndLineChanges[] = new RectorWithFileAndLineChange(
$rector,
$smartFileInfo->getRealPath(),
$line
);
}
}

View File

@ -1,56 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\ChangesReporting\ValueObject;
use Rector\Core\Contract\Rector\RectorInterface;
final class RectorWithFileAndLineChange
{
/**
* @var string
*/
private $realPath;
/**
* @var int
*/
private $line;
/**
* @var RectorInterface
*/
private $rector;
public function __construct(RectorInterface $rector, string $realPath, int $line)
{
$this->rector = $rector;
$this->line = $line;
$this->realPath = $realPath;
}
public function getRectorDefinitionsDescription(): string
{
$ruleDefinition = $this->rector->getRuleDefinition();
return $ruleDefinition->getDescription();
}
/**
* @return class-string<RectorInterface>
*/
public function getRectorClass(): string
{
return get_class($this->rector);
}
public function getLine(): int
{
return $this->line;
}
public function getRealPath(): string
{
return $this->realPath;
}
}

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Rector\ChangesReporting\ValueObjectFactory;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\Core\Differ\DefaultDiffer;
use Rector\Core\ValueObject\Application\File;
use Rector\Core\ValueObject\Reporting\FileDiff;
@ -12,11 +11,6 @@ use Symplify\ConsoleColorDiff\Console\Output\ConsoleDiffer;
final class FileDiffFactory
{
/**
* @var RectorChangeCollector
*/
private $rectorChangeCollector;
/**
* @var DefaultDiffer
*/
@ -27,27 +21,20 @@ final class FileDiffFactory
*/
private $consoleDiffer;
public function __construct(
RectorChangeCollector $rectorChangeCollector,
DefaultDiffer $defaultDiffer,
ConsoleDiffer $consoleDiffer
) {
$this->rectorChangeCollector = $rectorChangeCollector;
public function __construct(DefaultDiffer $defaultDiffer, ConsoleDiffer $consoleDiffer)
{
$this->defaultDiffer = $defaultDiffer;
$this->consoleDiffer = $consoleDiffer;
}
public function createFileDiff(File $file, string $oldContent, string $newContent): FileDiff
{
$smartFileInfo = $file->getSmartFileInfo();
$rectorChanges = $this->rectorChangeCollector->getRectorChangesByFileInfo($smartFileInfo);
// always keep the most recent diff
return new FileDiff(
$smartFileInfo,
$file->getSmartFileInfo(),
$this->defaultDiffer->diff($oldContent, $newContent),
$this->consoleDiffer->diff($oldContent, $newContent),
$rectorChanges
$file->getRectorWithLineChanges()
);
}
}

View File

@ -526,3 +526,7 @@ parameters:
- '#Cannot call method getSmartFileInfo\(\) on Rector\\Core\\ValueObject\\Application\\File\|null#'
- '#Class with base "Parser" name is already used in "Hoa\\Compiler\\Llk\\Parser", "Rector\\Core\\PhpParser\\Parser\\Parser"\. Use unique name to make classes easy to recognize#'
- '#Parameter \#1 \$file of method Rector\\CodingStyle\\ClassNameImport\\ShortNameResolver\:\:resolveForNode\(\) expects Rector\\Core\\ValueObject\\Application\\File, Rector\\Core\\ValueObject\\Application\\File\|null given#'
-
message: '#\$this as argument is not allowed\. Refactor method to service composition#'
path: rules/TypeDeclaration/Rector/ClassMethod/ParamTypeFromStrictTypedPropertyRector.php

View File

@ -36,6 +36,10 @@ final class MethodReflectionClassMethodResolver
}
$classReflection = $this->reflectionProvider->getClass($className);
if ($classReflection->isAnonymous()) {
return null;
}
if (! $classReflection->hasMethod($methodName)) {
return null;
}

View File

@ -134,7 +134,7 @@ CODE_SAMPLE
return null;
}
$this->rectorChangeCollector->notifyNodeFileInfo($node);
$this->rectorChangeCollector->notifyFileChange($this->file, $node, $this);
$param->type = $singlePropertyTypeNode;
return NodeTraverser::STOP_TRAVERSAL;

View File

@ -328,7 +328,8 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
// changed!
if ($this->hasNodeChanged($originalNode, $node)) {
$this->updateAttributes($node);
$this->rectorChangeCollector->notifyNodeFileInfo($node);
$this->rectorChangeCollector->notifyFileChange($this->file, $node, $this);
// update parents relations
$this->connectParentNodes($node);

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Rector\Core\ValueObject\Reporting;
use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Contract\Rector\RectorInterface;
use Symplify\SmartFileSystem\SmartFileInfo;
@ -21,9 +21,9 @@ final class FileDiff
private $diffConsoleFormatted;
/**
* @var RectorWithFileAndLineChange[]
* @var RectorWithLineChange[]
*/
private $rectorWithFileAndLineChanges = [];
private $rectorWithLineChanges = [];
/**
* @var SmartFileInfo
@ -31,17 +31,17 @@ final class FileDiff
private $smartFileInfo;
/**
* @param RectorWithFileAndLineChange[] $rectorWithFileAndLineChanges
* @param RectorWithLineChange[] $rectorWithLineChanges
*/
public function __construct(
SmartFileInfo $smartFileInfo,
string $diff,
string $diffConsoleFormatted,
array $rectorWithFileAndLineChanges = []
array $rectorWithLineChanges = []
) {
$this->smartFileInfo = $smartFileInfo;
$this->diff = $diff;
$this->rectorWithFileAndLineChanges = $rectorWithFileAndLineChanges;
$this->rectorWithLineChanges = $rectorWithLineChanges;
$this->diffConsoleFormatted = $diffConsoleFormatted;
}
@ -66,11 +66,11 @@ final class FileDiff
}
/**
* @return RectorWithFileAndLineChange[]
* @return RectorWithLineChange[]
*/
public function getRectorChanges(): array
{
return $this->rectorWithFileAndLineChanges;
return $this->rectorWithLineChanges;
}
/**
@ -79,8 +79,8 @@ final class FileDiff
public function getRectorClasses(): array
{
$rectorClasses = [];
foreach ($this->rectorWithFileAndLineChanges as $rectorWithFileAndLineChange) {
$rectorClasses[] = $rectorWithFileAndLineChange->getRectorClass();
foreach ($this->rectorWithLineChanges as $rectorWithLineChange) {
$rectorClasses[] = $rectorWithLineChange->getRectorClass();
}
return $this->sortClasses($rectorClasses);