Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba I tested this in CodeIgniter4 project, it make consistency 1 second faster.

Also make correct apply ::class tested at:

I will check more if this while improve performance, on complex files in target dir, eg contains .xml file, it read unnecessary files, since it fetch files under directory instead of read only listed files in the directory.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba I debugged, the OptimizedDirectorySourceLocatorFactory utilize PHPStan FileFinder that already only cover php extensions, so I guess it already cover only scan php files.

PHPStan\File\FileFinder #3115
   fileExcluder: PHPStan\File\FileExcluder #3113
   fileExtensions: array (1)
   |  0 => 'php'

so I think it already ok 👍

$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