Merge pull request #827 from rectorphp/reporting

Improve --debug reporting, fix MethodNameReplacerRector
This commit is contained in:
Tomáš Votruba 2018-12-10 15:56:03 +01:00 committed by GitHub
commit 8b1b44dd20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 25 deletions

View File

@ -80,7 +80,7 @@ final class RectorApplication
// 2. change nodes with Rectors
foreach ($fileInfos as $fileInfo) {
$this->advance();
$this->fileProcessor->refactor($fileInfo);
$this->refactorFileInfo($fileInfo);
}
// 3. print to file or string
@ -97,19 +97,31 @@ final class RectorApplication
}
private function processFileInfo(SmartFileInfo $fileInfo): void
{
$oldContent = $fileInfo->getContents();
if ($this->configuration->isDryRun()) {
$newContent = $this->fileProcessor->printToString($fileInfo);
} else {
$newContent = $this->fileProcessor->printToFile($fileInfo);
}
$this->errorAndDiffCollector->addFileDiff($fileInfo, $newContent, $oldContent);
$this->fileSystemFileProcessor->processFileInfo($fileInfo);
}
private function advance(): void
{
if ($this->symfonyStyle->isVerbose() === false) {
$this->symfonyStyle->progressAdvance();
}
}
private function refactorFileInfo(SmartFileInfo $fileInfo): void
{
try {
$oldContent = $fileInfo->getContents();
if ($this->configuration->isDryRun()) {
$newContent = $this->fileProcessor->printToString($fileInfo);
} else {
$newContent = $this->fileProcessor->printToFile($fileInfo);
}
$this->errorAndDiffCollector->addFileDiff($fileInfo, $newContent, $oldContent);
$this->fileSystemFileProcessor->processFileInfo($fileInfo);
$this->fileProcessor->refactor($fileInfo);
} catch (AnalysedCodeException $analysedCodeException) {
if ($this->configuration->shouldHideAutoloadErrors()) {
return;
@ -124,11 +136,4 @@ final class RectorApplication
$this->errorAndDiffCollector->addThrowableWithFileInfo($throwable, $fileInfo);
}
}
private function advance(): void
{
if ($this->symfonyStyle->isVerbose() === false) {
$this->symfonyStyle->progressAdvance();
}
}
}

View File

@ -11,6 +11,7 @@ use Rector\Application\AppliedRectorCollector;
use Rector\Contract\Rector\PhpRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\Attribute;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\PackageBuilder\FileSystem\SmartFileInfo;
use function Safe\sprintf;
@ -28,12 +29,20 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
*/
private $appliedRectorCollector;
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
/**
* @required
*/
public function setAbstractRectorDependencies(AppliedRectorCollector $appliedRectorCollector): void
{
public function setAbstractRectorDependencies(
AppliedRectorCollector $appliedRectorCollector,
SymfonyStyle $symfonyStyle
): void {
$this->appliedRectorCollector = $appliedRectorCollector;
$this->symfonyStyle = $symfonyStyle;
}
/**
@ -45,6 +54,11 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
return null;
}
// show current Rector class on --debug
if ($this->symfonyStyle->isDebug()) {
$this->symfonyStyle->writeln(static::class);
}
$originalNode = $node;
$node = $this->refactor($node);
if ($node === null) {

View File

@ -3,7 +3,10 @@
namespace Rector\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
@ -65,6 +68,9 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
if (! $this->isInstanceOf($parentNode, [MethodCall::class, StaticCall::class, ClassMethod::class])) {
return null;
}
foreach ($this->oldToNewMethodsByClass as $type => $oldToNewMethods) {
if (! $this->isType($parentNode, $type)) {
@ -83,4 +89,18 @@ CODE_SAMPLE
return $node;
}
/**
* @param string[] $nodeClasses
*/
private function isInstanceOf(Node $node, array $nodeClasses): bool
{
foreach ($nodeClasses as $nodeClass) {
if (is_a($node, $nodeClass, true)) {
return true;
}
}
return false;
}
}

View File

@ -125,7 +125,7 @@ abstract class AbstractRectorTestCase extends TestCase
foreach ($files as $file) {
$smartFileInfo = new SmartFileInfo($file);
[$originalContent, $changedContent] = $this->splitContentToOriginalFileAndExpectedFile($smartFileInfo);
$this->doTestFileMatchesExpectedContent($originalContent, $changedContent);
$this->doTestFileMatchesExpectedContent($originalContent, $changedContent, $smartFileInfo->getRealPath());
}
$this->autoloadTestFixture = true;
@ -183,8 +183,11 @@ abstract class AbstractRectorTestCase extends TestCase
);
}
private function doTestFileMatchesExpectedContent(string $originalFile, string $expectedFile): void
{
private function doTestFileMatchesExpectedContent(
string $originalFile,
string $expectedFile,
string $fixtureFile
): void {
$this->parameterProvider->changeParameter(Option::SOURCE, [$originalFile]);
$smartFileInfo = new SmartFileInfo($originalFile);
@ -194,6 +197,6 @@ abstract class AbstractRectorTestCase extends TestCase
$this->fileProcessor->refactor($smartFileInfo);
$changedContent = $this->fileProcessor->printToString($smartFileInfo);
$this->assertStringEqualsFile($expectedFile, $changedContent);
$this->assertStringEqualsFile($expectedFile, $changedContent, 'Caused by ' . $fixtureFile);
}
}