Skip to content

Commit

Permalink
Revert "use NewOptimizedDirectorySourceLocator" (#4845)
Browse files Browse the repository at this point in the history
* Revert "Remove unused patches/ files as already in vendor-patches directory and use NewOptimizedDirectorySourceLocator (#4844)"

This reverts commit a480c51.

* remove patch dir

* phpstan notice
  • Loading branch information
samsonasik committed Aug 24, 2023
1 parent a480c51 commit 07e36e3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages-tests/Skipper/Skipper/SkipperRectorRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Rector\Tests\Skipper\Skipper;

use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
use Illuminate\Container\RewindableGenerator;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\FileSystem\PhpFilesFinder;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

Expand All @@ -34,7 +34,7 @@ public function testRemovingServiceFromContainer(): void
$container = self::getContainer();

// to invoke before resolving
$container->make(FileNodesFetcher::class);
$container->make(PhpFilesFinder::class);

// here 1 rule should be removed and 1 should remain
/** @var RewindableGenerator<int, RectorInterface> $rectorsIterator */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator;
use Rector\Core\Contract\DependencyInjection\ResetableInterface;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use Webmozart\Assert\Assert;

/**
* @api phpstan external
Expand All @@ -23,15 +25,15 @@ final class DynamicSourceLocatorProvider implements ResetableInterface
private array $filePaths = [];

/**
* @var string[]
* @var array<string, string[]>
*/
private array $directories = [];
private array $filesByDirectory = [];

private ?AggregateSourceLocator $aggregateSourceLocator = null;

public function __construct(
private readonly FileNodesFetcher $fileNodesFetcher,
private readonly OptimizedDirectorySourceLocatorFactory $optimizedDirectorySourceLocatorFactory
private readonly PhpVersion $phpVersion
) {
}

Expand All @@ -48,14 +50,6 @@ public function addFiles(array $files): void
$this->filePaths = array_merge($this->filePaths, $files);
}

/**
* @param string[] $directories
*/
public function addDirectories(array $directories): void
{
$this->directories = array_merge($this->directories, $directories);
}

public function provide(): SourceLocator
{
// do not cache for PHPUnit, as in test every fixture is different
Expand All @@ -70,18 +64,28 @@ public function provide(): SourceLocator
$sourceLocators[] = new OptimizedSingleFileSourceLocator($this->fileNodesFetcher, $file);
}

foreach ($this->directories as $directory) {
$sourceLocators[] = $this->optimizedDirectorySourceLocatorFactory->createByDirectory($directory);
foreach ($this->filesByDirectory as $files) {
$sourceLocators[] = new OptimizedDirectorySourceLocator($this->fileNodesFetcher, $this->phpVersion, $files);
}

$this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators);

return $this->aggregateSourceLocator;
}

/**
* @param string[] $files
*/
public function addFilesByDirectory(string $directory, array $files): void
{
Assert::allString($files);

$this->filesByDirectory[$directory] = $files;
}

public function isPathsEmpty(): bool
{
return $this->filePaths === [] && $this->directories === [];
return $this->filePaths === [] && $this->filesByDirectory === [];
}

/**
Expand All @@ -90,7 +94,7 @@ public function isPathsEmpty(): bool
public function reset(): void
{
$this->filePaths = [];
$this->directories = [];
$this->filesByDirectory = [];
$this->aggregateSourceLocator = null;
}
}
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,9 @@ parameters:
-
message: '#Function "(class_exists|interface_exists)\(\)" cannot be used/left in the code\: use ReflectionProvider\->has\*\(\) instead#'
path: packages/Skipper/SkipCriteriaResolver/SkippedClassResolver.php

# todo: to be updated to use NewOptimizedDirectorySourceLocator later
# currently changing to NewOptimizedDirectorySourceLocator cause error, @see https://github.com/rectorphp/rector-src/actions/runs/5965685296/job/16183665877#step:10:19
-
message: '#Instantiation of deprecated class PHPStan\\Reflection\\BetterReflection\\SourceLocator\\OptimizedDirectorySourceLocator#'
path: packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php
37 changes: 37 additions & 0 deletions src/FileSystem/PhpFilesFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Rector\Core\FileSystem;

use Rector\Caching\UnchangedFilesFilter;

final class PhpFilesFinder
{
public function __construct(
private readonly FilesFinder $filesFinder,
private readonly UnchangedFilesFilter $unchangedFilesFilter
) {
}

/**
* @param string[] $paths
* @return string[]
*/
public function findInPaths(array $paths): array
{
$filePaths = $this->filesFinder->findInDirectoriesAndFiles($paths, ['php'], false);

// filter out non-PHP files
foreach ($filePaths as $key => $filePath) {
/**
* check .blade.php early so next .php check in next if can be skipped
*/
if (str_ends_with($filePath, '.blade.php')) {
unset($filePaths[$key]);
}
}

return $this->unchangedFilesFilter->filterFileInfos($filePaths);
}
}
7 changes: 6 additions & 1 deletion src/StaticReflection/DynamicSourceLocatorDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Rector\Core\FileSystem\FileAndDirectoryFilter;
use Rector\Core\FileSystem\FilesystemTweaker;
use Rector\Core\FileSystem\PhpFilesFinder;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;

/**
Expand All @@ -16,6 +17,7 @@ final class DynamicSourceLocatorDecorator
{
public function __construct(
private readonly DynamicSourceLocatorProvider $dynamicSourceLocatorProvider,
private readonly PhpFilesFinder $phpFilesFinder,
private readonly FileAndDirectoryFilter $fileAndDirectoryFilter,
private readonly FilesystemTweaker $filesystemTweaker
) {
Expand All @@ -36,7 +38,10 @@ public function addPaths(array $paths): void
$this->dynamicSourceLocatorProvider->addFiles($files);

$directories = $this->fileAndDirectoryFilter->filterDirectories($paths);
$this->dynamicSourceLocatorProvider->addDirectories($directories);
foreach ($directories as $directory) {
$filesInDirectory = $this->phpFilesFinder->findInPaths([$directory]);
$this->dynamicSourceLocatorProvider->addFilesByDirectory($directory, $filesInDirectory);
}
}

public function isPathsEmpty(): bool
Expand Down

0 comments on commit 07e36e3

Please sign in to comment.