Skip to content

Commit

Permalink
[DX] Warn about run on /vendor directory (#5525)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 30, 2024
1 parent 6edf23f commit 0ff8cb0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
6 changes: 0 additions & 6 deletions build/target-repository/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,6 @@ $node = new String_('hello world!');

// prints node to string, as PHP code displays it
print_node($node);

// dump nested node object with nested properties
dump_node($node);

// 2nd argument is how deep the nesting is - this makes sure the dump is short and useful
dump_node($node, 1);
```

<br>
Expand Down
24 changes: 24 additions & 0 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\Configuration\Parameter\SimpleParameterProvider;
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 @@ -55,6 +56,15 @@ public function run(Configuration $configuration, InputInterface $input): Proces
{
$filePaths = $this->fileFactory->findFilesInPaths($configuration->getPaths(), $configuration);

if ($this->containsVendorPath($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
));

sleep(3);
}

// no files found
if ($filePaths === []) {
return new ProcessResult([], []);
Expand Down Expand Up @@ -255,4 +265,18 @@ 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;
}
}
12 changes: 4 additions & 8 deletions src/FileSystem/FilePathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\FileSystem;

use Nette\Utils\Strings;
use Rector\Skipper\FileSystem\PathNormalizer;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -60,7 +61,7 @@ public function normalizePathAndSchema(string $originalPath): string
$path = $originalPath;
}

$normalizedPath = str_replace('\\', '/', (string) $path);
$normalizedPath = PathNormalizer::normalize((string) $path);
$path = Strings::replace($normalizedPath, self::TWO_AND_MORE_SLASHES_REGEX, '/');

$pathRoot = str_starts_with($path, '/') ? $directorySeparator : '';
Expand All @@ -69,23 +70,18 @@ public function normalizePathAndSchema(string $originalPath): string
$normalizedPathParts = $this->normalizePathParts($pathParts, $scheme);

$pathStart = ($scheme !== self::SCHEME_UNDEFINED ? $scheme . '://' : '');
return $this->normalizePath($pathStart . $pathRoot . implode($directorySeparator, $normalizedPathParts));
return PathNormalizer::normalize($pathStart . $pathRoot . implode($directorySeparator, $normalizedPathParts));
}

private function relativeFilePathFromDirectory(string $fileRealPath, string $directory): string
{
Assert::directory($directory);
$normalizedFileRealPath = $this->normalizePath($fileRealPath);
$normalizedFileRealPath = PathNormalizer::normalize($fileRealPath);

$relativeFilePath = $this->filesystem->makePathRelative($normalizedFileRealPath, $directory);
return rtrim($relativeFilePath, '/');
}

private function normalizePath(string $filePath): string
{
return \str_replace('\\', '/', $filePath);
}

/**
* @param string[] $pathParts
* @return string[]
Expand Down
11 changes: 0 additions & 11 deletions src/functions/node_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;

// @deprecated, use dump() or dd() instead
if (! function_exists('dump_node')) {
function dump_node(mixed $variable, int $depth = 2): never
{
trigger_error(
'This function is deprecated, to avoid enforcing of Rector debug package. Use your own favorite debugging package instead'
);
exit;
}
}

if (! function_exists('print_node')) {
/**
* @param Node|Node[] $node
Expand Down

0 comments on commit 0ff8cb0

Please sign in to comment.