mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 20:00:50 +00:00
remove deprecated console differ, to use symplify (#5402)
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
This commit is contained in:
parent
5ff1d911a3
commit
9a8cce6545
|
@ -96,7 +96,6 @@
|
|||
"Rector\\CodeQuality\\": "rules/code-quality/src",
|
||||
"Rector\\CodingStyle\\": "rules/coding-style/src",
|
||||
"Rector\\Composer\\": "rules/composer/src",
|
||||
"Rector\\ConsoleDiffer\\": "packages/console-differ/src",
|
||||
"Rector\\Core\\": "src",
|
||||
"Rector\\DeadCode\\": "rules/dead-code/src",
|
||||
"Rector\\DependencyInjection\\": "rules/dependency-injection/src",
|
||||
|
|
|
@ -7,12 +7,13 @@ namespace Rector\ChangesReporting\Application;
|
|||
use PhpParser\Node;
|
||||
use PHPStan\AnalysedCodeException;
|
||||
use Rector\ChangesReporting\Collector\RectorChangeCollector;
|
||||
use Rector\ConsoleDiffer\DifferAndFormatter;
|
||||
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
|
||||
use Rector\Core\Differ\DefaultDiffer;
|
||||
use Rector\Core\Error\ExceptionCorrector;
|
||||
use Rector\Core\ValueObject\Application\RectorError;
|
||||
use Rector\Core\ValueObject\Reporting\FileDiff;
|
||||
use Rector\PostRector\Collector\NodesToRemoveCollector;
|
||||
use Symplify\ConsoleColorDiff\Console\Output\ConsoleDiffer;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
use Throwable;
|
||||
|
||||
|
@ -28,11 +29,6 @@ final class ErrorAndDiffCollector
|
|||
*/
|
||||
private $fileDiffs = [];
|
||||
|
||||
/**
|
||||
* @var DifferAndFormatter
|
||||
*/
|
||||
private $differAndFormatter;
|
||||
|
||||
/**
|
||||
* @var RectorChangeCollector
|
||||
*/
|
||||
|
@ -53,18 +49,30 @@ final class ErrorAndDiffCollector
|
|||
*/
|
||||
private $nodesToRemoveCollector;
|
||||
|
||||
/**
|
||||
* @var ConsoleDiffer
|
||||
*/
|
||||
private $consoleDiffer;
|
||||
|
||||
/**
|
||||
* @var DefaultDiffer
|
||||
*/
|
||||
private $defaultDiffer;
|
||||
|
||||
public function __construct(
|
||||
DifferAndFormatter $differAndFormatter,
|
||||
ExceptionCorrector $exceptionCorrector,
|
||||
NodesToRemoveCollector $nodesToRemoveCollector,
|
||||
RectorChangeCollector $rectorChangeCollector,
|
||||
RemovedAndAddedFilesCollector $removedAndAddedFilesCollector
|
||||
RemovedAndAddedFilesCollector $removedAndAddedFilesCollector,
|
||||
ConsoleDiffer $consoleDiffer,
|
||||
DefaultDiffer $defaultDiffer
|
||||
) {
|
||||
$this->differAndFormatter = $differAndFormatter;
|
||||
$this->rectorChangeCollector = $rectorChangeCollector;
|
||||
$this->exceptionCorrector = $exceptionCorrector;
|
||||
$this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector;
|
||||
$this->nodesToRemoveCollector = $nodesToRemoveCollector;
|
||||
$this->consoleDiffer = $consoleDiffer;
|
||||
$this->defaultDiffer = $defaultDiffer;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,12 +120,13 @@ final class ErrorAndDiffCollector
|
|||
$rectorChanges = $this->rectorChangeCollector->getRectorChangesByFileInfo($smartFileInfo);
|
||||
|
||||
// always keep the most recent diff
|
||||
$this->fileDiffs[$smartFileInfo->getRealPath()] = new FileDiff(
|
||||
$fileDiff = new FileDiff(
|
||||
$smartFileInfo,
|
||||
$this->differAndFormatter->diff($oldContent, $newContent),
|
||||
$this->differAndFormatter->diffAndFormat($oldContent, $newContent),
|
||||
$this->defaultDiffer->diff($oldContent, $newContent),
|
||||
$this->consoleDiffer->diff($oldContent, $newContent),
|
||||
$rectorChanges
|
||||
);
|
||||
$this->fileDiffs[$smartFileInfo->getRealPath()] = $fileDiff;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\ConsoleDiffer\Diff\Output\CompleteUnifiedDiffOutputBuilderFactory;
|
||||
use Rector\ConsoleDiffer\DifferAndFormatter;
|
||||
use Rector\ConsoleDiffer\MarkdownDifferAndFormatter;
|
||||
use SebastianBergmann\Diff\Differ;
|
||||
use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;
|
||||
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
|
||||
use Symplify\ConsoleColorDiff\Console\Formatter\ColorConsoleDiffFormatter;
|
||||
use Symplify\ConsoleColorDiff\Console\Output\ConsoleDiffer;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
|
||||
$services->defaults()
|
||||
->public()
|
||||
->autowire()
|
||||
->autoconfigure();
|
||||
|
||||
$services->load('Rector\ConsoleDiffer\\', __DIR__ . '/../src');
|
||||
|
||||
$services->set(DifferAndFormatter::class)
|
||||
->arg('$differ', service('differ'));
|
||||
|
||||
$services->set(MarkdownDifferAndFormatter::class)
|
||||
->arg('$markdownDiffer', service('markdownDiffer'));
|
||||
|
||||
$services->set('diffOutputBuilder', StrictUnifiedDiffOutputBuilder::class)
|
||||
->arg('$options', [
|
||||
'fromFile' => 'Original',
|
||||
'toFile' => 'New',
|
||||
]);
|
||||
|
||||
$services->set('differ', Differ::class)
|
||||
->arg('$outputBuilder', service('diffOutputBuilder'));
|
||||
|
||||
$services->set('markdownDiffOutputBuilder', UnifiedDiffOutputBuilder::class)
|
||||
->factory([service(CompleteUnifiedDiffOutputBuilderFactory::class), 'create']);
|
||||
|
||||
$services->set('markdownDiffer', Differ::class)
|
||||
->arg('$outputBuilder', service('markdownDiffOutputBuilder'));
|
||||
|
||||
$services->set(ColorConsoleDiffFormatter::class);
|
||||
|
||||
$services->set(ConsoleDiffer::class);
|
||||
};
|
|
@ -1,36 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\ConsoleDiffer\Diff\Output;
|
||||
|
||||
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
|
||||
use Symplify\PackageBuilder\Reflection\PrivatesAccessor;
|
||||
|
||||
/**
|
||||
* Creates @see UnifiedDiffOutputBuilder with "$contextLines = 1000;"
|
||||
*/
|
||||
final class CompleteUnifiedDiffOutputBuilderFactory
|
||||
{
|
||||
/**
|
||||
* @var PrivatesAccessor
|
||||
*/
|
||||
private $privatesAccessor;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->privatesAccessor = new PrivatesAccessor();
|
||||
}
|
||||
|
||||
/**
|
||||
* @api
|
||||
*/
|
||||
public function create(): UnifiedDiffOutputBuilder
|
||||
{
|
||||
$unifiedDiffOutputBuilder = new UnifiedDiffOutputBuilder('');
|
||||
|
||||
$this->privatesAccessor->setPrivateProperty($unifiedDiffOutputBuilder, 'contextLines', 1000);
|
||||
|
||||
return $unifiedDiffOutputBuilder;
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\ConsoleDiffer;
|
||||
|
||||
use SebastianBergmann\Diff\Differ;
|
||||
use Symplify\ConsoleColorDiff\Console\Formatter\ColorConsoleDiffFormatter;
|
||||
|
||||
/**
|
||||
* @deprecated Move to symplify
|
||||
*/
|
||||
final class DifferAndFormatter
|
||||
{
|
||||
/**
|
||||
* @var Differ
|
||||
*/
|
||||
private $differ;
|
||||
|
||||
/**
|
||||
* @var ColorConsoleDiffFormatter
|
||||
*/
|
||||
private $colorConsoleDiffFormatter;
|
||||
|
||||
public function __construct(ColorConsoleDiffFormatter $colorConsoleDiffFormatter, Differ $differ)
|
||||
{
|
||||
$this->differ = $differ;
|
||||
$this->colorConsoleDiffFormatter = $colorConsoleDiffFormatter;
|
||||
}
|
||||
|
||||
public function diff(string $old, string $new): string
|
||||
{
|
||||
if ($old === $new) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->differ->diff($old, $new);
|
||||
}
|
||||
|
||||
public function diffAndFormat(string $old, string $new): string
|
||||
{
|
||||
if ($old === $new) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$diff = $this->diff($old, $new);
|
||||
|
||||
return $this->colorConsoleDiffFormatter->format($diff);
|
||||
}
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\ConsoleDiffer;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use SebastianBergmann\Diff\Differ;
|
||||
|
||||
/**
|
||||
* @deprecated Move to symplify
|
||||
*/
|
||||
final class MarkdownDifferAndFormatter
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
* @see https://regex101.com/r/LE9Xwo/1
|
||||
*/
|
||||
private const METADATA_REGEX = '#^(.*\n){1}#';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @see https://regex101.com/r/yf7u2L/1
|
||||
*/
|
||||
private const SPACE_AND_NEWLINE_REGEX = '#( ){1,}\n#';
|
||||
|
||||
/**
|
||||
* @var Differ
|
||||
*/
|
||||
private $markdownDiffer;
|
||||
|
||||
public function __construct(Differ $markdownDiffer)
|
||||
{
|
||||
$this->markdownDiffer = $markdownDiffer;
|
||||
}
|
||||
|
||||
public function bareDiffAndFormatWithoutColors(string $old, string $new): string
|
||||
{
|
||||
if ($old === $new) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$diff = $this->markdownDiffer->diff($old, $new);
|
||||
|
||||
// remove first line, just meta info added by UnifiedDiffOutputBuilder
|
||||
$diff = Strings::replace($diff, self::METADATA_REGEX, '');
|
||||
|
||||
return $this->removeTrailingWhitespaces($diff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes UnifiedDiffOutputBuilder generated pre-spaces " \n" => "\n"
|
||||
*/
|
||||
private function removeTrailingWhitespaces(string $diff): string
|
||||
{
|
||||
$diff = Strings::replace($diff, self::SPACE_AND_NEWLINE_REGEX, PHP_EOL);
|
||||
|
||||
return rtrim($diff);
|
||||
}
|
||||
}
|
33
src/Differ/DefaultDiffer.php
Normal file
33
src/Differ/DefaultDiffer.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Differ;
|
||||
|
||||
use SebastianBergmann\Diff\Differ;
|
||||
use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;
|
||||
|
||||
final class DefaultDiffer
|
||||
{
|
||||
/**
|
||||
* @var Differ
|
||||
*/
|
||||
private $differ;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$strictUnifiedDiffOutputBuilder = new StrictUnifiedDiffOutputBuilder([
|
||||
'fromFile' => 'Original',
|
||||
'toFile' => 'New',
|
||||
]);
|
||||
$this->differ = new Differ($strictUnifiedDiffOutputBuilder);
|
||||
}
|
||||
|
||||
public function diff(string $old, string $new): string
|
||||
{
|
||||
if ($old === $new) {
|
||||
return '';
|
||||
}
|
||||
return $this->differ->diff($old, $new);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user