Skip to content

Commit

Permalink
[DX] Skip vendor warning in case of downgrade (#5526)
Browse files Browse the repository at this point in the history
* [DX] Skip vendor warning in case of downgrade

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jan 30, 2024
1 parent 0ff8cb0 commit a6b0d2c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
21 changes: 4 additions & 17 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Configuration\VendorMissAnalyseGuard;
use Rector\Parallel\Application\ParallelFileProcessor;
use Rector\Provider\CurrentFileProvider;
use Rector\Skipper\FileSystem\PathNormalizer;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use Rector\Util\ArrayParametersMerger;
use Rector\ValueObject\Application\File;
Expand Down Expand Up @@ -48,15 +48,16 @@ public function __construct(
private readonly ChangedFilesDetector $changedFilesDetector,
private readonly CurrentFileProvider $currentFileProvider,
private readonly FileProcessor $fileProcessor,
private readonly ArrayParametersMerger $arrayParametersMerger
private readonly ArrayParametersMerger $arrayParametersMerger,
private readonly VendorMissAnalyseGuard $vendorMissAnalyseGuard,
) {
}

public function run(Configuration $configuration, InputInterface $input): ProcessResult
{
$filePaths = $this->fileFactory->findFilesInPaths($configuration->getPaths(), $configuration);

if ($this->containsVendorPath($filePaths)) {
if ($this->vendorMissAnalyseGuard->isVendorAnalyzed($filePaths)) {
$this->symfonyStyle->warning(sprintf(
'Rector is running on your "/vendor" directory. This is not necessary, as Rector access /vendor by composer autoload. It will cause Rector tu run much slower and possibly with errors.%sRemove "/vendor" from Rector paths and run again.',
PHP_EOL . PHP_EOL
Expand Down Expand Up @@ -265,18 +266,4 @@ private function resolveCalledRectorBinary(): ?string

return $potentialRectorBinaryPath;
}

/**
* @param string[] $filePaths
*/
private function containsVendorPath(array $filePaths): bool
{
foreach ($filePaths as $filePath) {
if (str_contains(PathNormalizer::normalize($filePath), '/vendor/')) {
return true;
}
}

return false;
}
}
50 changes: 50 additions & 0 deletions src/Configuration/VendorMissAnalyseGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Rector\Configuration;

use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Skipper\FileSystem\PathNormalizer;

final class VendorMissAnalyseGuard
{
/**
* @param string[] $filePaths
*/
public function isVendorAnalyzed(array $filePaths): bool
{
if ($this->hasDowngradeSets()) {
return false;
}

return $this->containsVendorPath($filePaths);
}

private function hasDowngradeSets(): bool
{
$registeredRectorSets = SimpleParameterProvider::provideArrayParameter(Option::REGISTERED_RECTOR_SETS);

foreach ($registeredRectorSets as $registeredRectorSet) {
if (str_contains((string) $registeredRectorSet, 'downgrade-')) {
return true;
}
}

return false;
}

/**
* @param string[] $filePaths
*/
private function containsVendorPath(array $filePaths): bool
{
foreach ($filePaths as $filePath) {
if (str_contains(PathNormalizer::normalize($filePath), '/vendor/')) {
return true;
}
}

return false;
}
}

0 comments on commit a6b0d2c

Please sign in to comment.