Skip to content

Commit

Permalink
Revert "Revert "use NewOptimizedDirectorySourceLocator"" (#4846)
Browse files Browse the repository at this point in the history
* Revert "Revert "use NewOptimizedDirectorySourceLocator" (#4845)"

This reverts commit 07e36e3.

* wrap Expression

* Revert "wrap Expression"

This reverts commit f54fa71.
  • Loading branch information
samsonasik authored Aug 24, 2023
1 parent dce1084 commit 3c34942
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 71 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(PhpFilesFinder::class);
$container->make(FileNodesFetcher::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,13 +6,11 @@

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\OptimizedDirectorySourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory;
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 @@ -25,15 +23,15 @@ final class DynamicSourceLocatorProvider implements ResetableInterface
private array $filePaths = [];

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

private ?AggregateSourceLocator $aggregateSourceLocator = null;

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

Expand All @@ -50,6 +48,14 @@ 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 @@ -64,28 +70,18 @@ public function provide(): SourceLocator
$sourceLocators[] = new OptimizedSingleFileSourceLocator($this->fileNodesFetcher, $file);
}

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

$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->filesByDirectory === [];
return $this->filePaths === [] && $this->directories === [];
}

/**
Expand All @@ -94,7 +90,7 @@ public function isPathsEmpty(): bool
public function reset(): void
{
$this->filePaths = [];
$this->filesByDirectory = [];
$this->directories = [];
$this->aggregateSourceLocator = null;
}
}
6 changes: 0 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,3 @@ 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: 0 additions & 37 deletions src/FileSystem/PhpFilesFinder.php

This file was deleted.

7 changes: 1 addition & 6 deletions src/StaticReflection/DynamicSourceLocatorDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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

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

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

public function isPathsEmpty(): bool
Expand Down

0 comments on commit 3c34942

Please sign in to comment.