Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] tv less file provider #6049

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,12 @@ parameters:
message: '#Parameters should have "PhpParser\\Node\\Stmt\\ClassMethod" types as the only types passed to this method#'
path: src/VendorLocker/ParentClassMethodTypeOverrideGuard.php

# known value
-
message: '#Parameter \#4 \$file of method Rector\\NodeTypeResolver\\PhpDocNodeVisitor\\NameImportingPhpDocNodeVisitor\:\:processFqnNameImport\(\) expects Rector\\ValueObject\\Application\\File, Rector\\ValueObject\\Application\\File\|null given#'
path: src/NodeTypeResolver/PhpDocNodeVisitor/NameImportingPhpDocNodeVisitor.php

- '#Parameter (.*?) Rector\\ValueObject\\Application\\File, Rector\\ValueObject\\Application\\File\|null given#'
- '#Cannot call method getFilePath\(\) on Rector\\ValueObject\\Application\\File\|null#'

- '#Method Rector\\PhpParser\\Node\\BetterNodeFinder\:\:findFirstInstancesOf\(\) should return \(T of PhpParser\\Node\)\|null but returns PhpParser\\Node#'
8 changes: 4 additions & 4 deletions rules/CodingStyle/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ private function addUseImport(
FullyQualified $fullyQualified,
FullyQualifiedObjectType $fullyQualifiedObjectType
): void {
if ($this->useNodesToAddCollector->hasImport($file, $fullyQualified, $fullyQualifiedObjectType)) {
if ($this->useNodesToAddCollector->hasImport($file, $fullyQualifiedObjectType)) {
return;
}

if ($fullyQualified->getAttribute(AttributeKey::IS_FUNCCALL_NAME) === true) {
$this->useNodesToAddCollector->addFunctionUseImport($fullyQualifiedObjectType);
$this->useNodesToAddCollector->addFunctionUseImport($file, $fullyQualifiedObjectType);
} elseif ($fullyQualified->getAttribute(AttributeKey::IS_CONSTFETCH_NAME) === true) {
$this->useNodesToAddCollector->addConstantUseImport($fullyQualifiedObjectType);
$this->useNodesToAddCollector->addConstantUseImport($file, $fullyQualifiedObjectType);
} else {
$this->useNodesToAddCollector->addUseImport($fullyQualifiedObjectType);
$this->useNodesToAddCollector->addUseImport($file, $fullyQualifiedObjectType);
}
}
}
28 changes: 10 additions & 18 deletions rules/Naming/Naming/UseImportsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\ValueObject\Application\File;

final readonly class UseImportsResolver
{
public function __construct(
private CurrentFileProvider $currentFileProvider
private CurrentFileProvider $currentFileProvider,
private BetterNodeFinder $betterNodeFinder,
) {
}

/**
* @return Use_[]|GroupUse[]
* @return array<Use_|GroupUse>
*/
public function resolve(): array
{
// @todo pass argument here :)
$namespace = $this->resolveNamespace();
if (! $namespace instanceof Node) {
return [];
Expand Down Expand Up @@ -57,7 +60,7 @@ public function resolvePrefix(Use_|GroupUse $use): string
: '';
}

private function resolveNamespace(): Namespace_|FileWithoutNamespace|null
private function resolveNamespace(?File $file = null): Namespace_|FileWithoutNamespace|null
{
/** @var File|null $file */
$file = $this->currentFileProvider->getFile();
Expand All @@ -67,27 +70,16 @@ private function resolveNamespace(): Namespace_|FileWithoutNamespace|null

$newStmts = $file->getNewStmts();

if ($newStmts === []) {
return null;
}

$namespaces = array_filter($newStmts, static fn (Stmt $stmt): bool => $stmt instanceof Namespace_);

// multiple namespaces is not supported
if (count($namespaces) > 1) {
return null;
}

$currentNamespace = current($namespaces);
if ($currentNamespace instanceof Namespace_) {
return $currentNamespace;
}

$currentStmt = current($newStmts);
if (! $currentStmt instanceof FileWithoutNamespace) {
return null;
}

return $currentStmt;
return $this->betterNodeFinder->findFirstInstancesOf($newStmts, [
Namespace_::class,
FileWithoutNamespace::class,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ private function completeExtraUseImports(array $attributeGroups): void
continue;
}

$this->useNodesToAddCollector->addUseImport(new FullyQualifiedObjectType($namespacedAttrName));
$this->useNodesToAddCollector->addUseImport(
$this->file,
new FullyQualifiedObjectType($namespacedAttrName)
);
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,6 @@ public function narrowToFullyQualifiedOrAliasedObjectType(
}

$uses = $this->useImportsResolver->resolve();
if ($uses === []) {
if (! $this->reflectionProvider->hasClass($objectType->getClassName())) {
return new NonExistingObjectType($objectType->getClassName());
}

return new FullyQualifiedObjectType(
$objectType->getClassName(),
null,
$objectType->getClassReflection()
);
}

$aliasedObjectType = $this->matchAliasedObjectType($objectType, $uses);
if ($aliasedObjectType instanceof AliasedObjectType) {
Expand Down
7 changes: 4 additions & 3 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\FileSystem\FilesFinder;
use Rector\Parallel\Application\ParallelFileProcessor;
use Rector\Reporting\MissConfigurationReporter;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
Expand All @@ -19,7 +20,6 @@
use Rector\ValueObject\FileProcessResult;
use Rector\ValueObject\ProcessResult;
use Rector\ValueObject\Reporting\FileDiff;
use Rector\ValueObjectFactory\Application\FileFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\EasyParallel\CpuCoreCountProvider;
Expand All @@ -40,8 +40,8 @@ final class ApplicationFileProcessor
private array $systemErrors = [];

public function __construct(
private readonly FilesFinder $filesFinder,
private readonly SymfonyStyle $symfonyStyle,
private readonly FileFactory $fileFactory,
private readonly ParallelFileProcessor $parallelFileProcessor,
private readonly ScheduleFactory $scheduleFactory,
private readonly CpuCoreCountProvider $cpuCoreCountProvider,
Expand All @@ -55,7 +55,8 @@ public function __construct(

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

$this->missConfigurationReporter->reportVendorInPaths($filePaths);
$this->missConfigurationReporter->reportStartWithShortOpenTag();

Expand Down
3 changes: 3 additions & 0 deletions src/Application/Provider/CurrentFileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Rector\ValueObject\Application\File;

/**
* @internal Avoid this services if possible, pass File value object directly
*/
final class CurrentFileProvider
{
private ?File $file = null;
Expand Down
29 changes: 26 additions & 3 deletions src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Rector\FileSystem;

use Nette\Utils\FileSystem;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Caching\UnchangedFilesFilter;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Skipper\Skipper\PathSkipper;
use Rector\ValueObject\Configuration;
use Symfony\Component\Finder\Finder;

/**
Expand All @@ -21,7 +23,8 @@ public function __construct(
private UnchangedFilesFilter $unchangedFilesFilter,
private FileAndDirectoryFilter $fileAndDirectoryFilter,
private PathSkipper $pathSkipper,
private FilePathHelper $filePathHelper
private FilePathHelper $filePathHelper,
private ChangedFilesDetector $changedFilesDetector,
) {
}

Expand Down Expand Up @@ -53,7 +56,10 @@ public function findInDirectoriesAndFiles(array $source, array $suffixes = [], b
$filteredFilePaths,
function (string $file): bool {
if ($this->isStartWithShortPHPTag(FileSystem::read($file))) {
SimpleParameterProvider::addParameter(Option::SKIPPED_START_WITH_SHORT_OPEN_TAG_FILES, $this->filePathHelper->relativePath($file));
SimpleParameterProvider::addParameter(
Option::SKIPPED_START_WITH_SHORT_OPEN_TAG_FILES,
$this->filePathHelper->relativePath($file)
);
return false;
}

Expand All @@ -69,6 +75,20 @@ function (string $file): bool {
return $this->unchangedFilesFilter->filterFilePaths($filePaths);
}

/**
* @param string[] $paths
* @return string[]
*/
public function findFilesInPaths(array $paths, Configuration $configuration): array
{
if ($configuration->shouldClearCache()) {
$this->changedFilesDetector->clear();
}

$supportedFileExtensions = $configuration->getFileExtensions();
return $this->findInDirectoriesAndFiles($paths, $supportedFileExtensions);
}

/**
* Exclude short "<?=" tags as lead to invalid changes
*/
Expand Down Expand Up @@ -119,7 +139,10 @@ private function findInDirectories(array $directories, array $suffixes, bool $so
}

if ($this->isStartWithShortPHPTag($fileInfo->getContents())) {
SimpleParameterProvider::addParameter(Option::SKIPPED_START_WITH_SHORT_OPEN_TAG_FILES, $this->filePathHelper->relativePath($path));
SimpleParameterProvider::addParameter(
Option::SKIPPED_START_WITH_SHORT_OPEN_TAG_FILES,
$this->filePathHelper->relativePath($path)
);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use Rector\NodeTypeResolver\PhpDocNodeVisitor\NameImportingPhpDocNodeVisitor;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\ValueObject\Application\File;

final readonly class DocBlockNameImporter
{
Expand All @@ -16,13 +17,13 @@ public function __construct(
) {
}

public function importNames(PhpDocNode $phpDocNode, Node $node): bool
public function importNames(PhpDocNode $phpDocNode, Node $node, File $file): bool
{
if ($phpDocNode->children === []) {
return false;
}

$this->nameImportingPhpDocNodeVisitor->setCurrentNode($node);
$this->nameImportingPhpDocNodeVisitor->setCurrentFileAndNode($file, $node);

$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->addPhpDocNodeVisitor($this->nameImportingPhpDocNodeVisitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
Expand All @@ -32,10 +31,11 @@ final class NameImportingPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor

private bool $hasChanged = false;

private ?File $currentFile = null;

public function __construct(
private readonly ClassNameImportSkipper $classNameImportSkipper,
private readonly UseNodesToAddCollector $useNodesToAddCollector,
private readonly CurrentFileProvider $currentFileProvider,
private readonly ReflectionProvider $reflectionProvider,
private readonly IdentifierTypeMapper $identifierTypeMapper,
) {
Expand Down Expand Up @@ -82,17 +82,13 @@ public function enterNode(Node $node): ?Node
return null;
}

$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
return null;
}

return $this->processFqnNameImport($this->currentPhpParserNode, $node, $staticType, $file);
return $this->processFqnNameImport($this->currentPhpParserNode, $node, $staticType, $this->currentFile);
}

public function setCurrentNode(PhpParserNode $phpParserNode): void
public function setCurrentFileAndNode(File $file, PhpParserNode $phpParserNode): void
{
$this->hasChanged = false;
$this->currentFile = $file;
$this->currentPhpParserNode = $phpParserNode;
}

Expand Down Expand Up @@ -138,7 +134,7 @@ private function processFqnNameImport(
}

if ($this->shouldImport($newNode, $identifierTypeNode, $fullyQualifiedObjectType)) {
$this->useNodesToAddCollector->addUseImport($fullyQualifiedObjectType);
$this->useNodesToAddCollector->addUseImport($file, $fullyQualifiedObjectType);
$this->hasChanged = true;

return $newNode;
Expand Down Expand Up @@ -215,16 +211,11 @@ private function processDoctrineAnnotationTagValueNode(
$staticType = new FullyQualifiedObjectType($staticType->getClassName());
}

$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
return;
}

$shortentedIdentifierTypeNode = $this->processFqnNameImport(
$currentPhpParserNode,
$identifierTypeNode,
$staticType,
$file
$this->currentFile
);

if (! $shortentedIdentifierTypeNode instanceof IdentifierTypeNode) {
Expand Down Expand Up @@ -268,16 +259,11 @@ private function enterSpacelessPhpDocTagNode(
$staticType = new FullyQualifiedObjectType($staticType->getClassName());
}

$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
return null;
}

$importedName = $this->processFqnNameImport(
$currentPhpParserNode,
$identifierTypeNode,
$staticType,
$file
$this->currentFile
);

if ($importedName instanceof IdentifierTypeNode) {
Expand Down
20 changes: 20 additions & 0 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ public function findFirstInstanceOf(Node | array $nodes, string $type): ?Node
return $this->nodeFinder->findFirstInstanceOf($nodes, $type);
}

/**
* @template T of Node
*
* @param Node[] $nodes
* @param array<class-string<T>> $types
*
* @return T|null
*/
public function findFirstInstancesOf(array $nodes, array $types): ?Node
{
foreach ($types as $type) {
$foundNode = $this->nodeFinder->findFirstInstanceOf($nodes, $type);
if ($foundNode instanceof Node) {
return $foundNode;
}
}

return null;
}

/**
* @param class-string<Node> $type
* @param Node[] $nodes
Expand Down
2 changes: 1 addition & 1 deletion src/PostRector/Application/PostFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function __construct(
private readonly Skipper $skipper,
private readonly UseAddingPostRector $useAddingPostRector,
private readonly NameImportingPostRector $nameImportingPostRector,
private readonly ClassRenamingPostRector $classRenamingPostRector,
private readonly DocblockNameImportingPostRector $docblockNameImportingPostRector,
private readonly ClassRenamingPostRector $classRenamingPostRector,
private readonly UnusedImportRemovingPostRector $unusedImportRemovingPostRector,
) {
}
Expand Down
Loading