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

Adding more types in the FileFinder #2606

Merged
merged 3 commits into from Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 48 additions & 13 deletions lib/Extension/ClassMover/Application/Finder/FileFinder.php
Expand Up @@ -41,8 +41,8 @@ public function filesFor(Filesystem $filesystem, ReflectionClassLike $reflection
// we have public members or a non-class, we need to search the
// whole tree, but we can discount any files which do not contain
// the member name string.
return $this->allPhpFiles($filesystem)->filter(function (SplFileInfo $file) use ($memberName) {
return preg_match('{' . $memberName . '}', file_get_contents($file->getPathname()));
return $this->allPhpFiles($filesystem)->filter(function (SplFileInfo $file) use ($memberName): bool {
return preg_match('{' . $memberName . '}', file_get_contents($file->getPathname())) === 1;
});
}

Expand All @@ -57,12 +57,14 @@ public function filesFor(Filesystem $filesystem, ReflectionClassLike $reflection
return $this->pathsFromReflectionClass($reflection, $private);
}

private function pathsFromReflectionClass(ReflectionClass $reflection, bool $private)
private function pathsFromReflectionClass(ReflectionClass $reflection, bool $private): FileList
{
$path = $reflection->sourceCode()->uri()?->path();

if (!$path) {
throw new RuntimeException('Source has no path associated with it');
throw new RuntimeException(
sprintf('Source class "%s" has no path associated with it', $reflection->name()),
);
}

$filePaths = [ $path ];
Expand All @@ -78,35 +80,68 @@ private function pathsFromReflectionClass(ReflectionClass $reflection, bool $pri
return FileList::fromFilePaths($filePaths);
}

private function allPhpFiles(Filesystem $filesystem)
private function allPhpFiles(Filesystem $filesystem): FileList
{
$filePaths = $filesystem->fileList()->existing()->phpFiles();
return $filePaths;
return $filesystem->fileList()->existing()->phpFiles();
}

private function parentFilePaths(ReflectionClass $reflection, $filePaths)
/**
* @param array<string> $filePaths
*
* @return array<string>
Copy link
Collaborator

Choose a reason for hiding this comment

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

this was array<string|null>? is this definitely not handled? we could also skip if it's NULL for whatever reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah seeing that this never crashes it seems like it's never null but yeah, skipping those would probably be the better option.

*/
private function parentFilePaths(ReflectionClass $reflection, array $filePaths): array
{
$context = $reflection->parent();
while ($context) {
$filePaths[] = $context->sourceCode()->uri()?->path();
$path = $context->sourceCode()->uri()?->path();
if (!$path) {
throw new RuntimeException(
sprintf('Source class "%s" has no path associated with it', $context->name()),
);
}
$filePaths[] = $path;
$context = $context->parent();
}

return $filePaths;
}

private function traitFilePaths(ReflectionClass $reflection, $filePaths)
/**
* @param array<string> $filePaths
*
* @return array<string>
*/
private function traitFilePaths(ReflectionClass $reflection, array $filePaths): array
{
foreach ($reflection->traits() as $trait) {
$filePaths[] = $trait->sourceCode()->uri()?->path();
$path = $trait->sourceCode()->uri()?->path();
if (!$path) {
throw new RuntimeException(
sprintf('Source trait "%s" has no path associated with it', $trait->name()),
);
}
$filePaths[] = $path;
}
return $filePaths;
}

private function interfaceFilePaths(ReflectionClass $reflection, $filePaths)
/**
* @param array<string> $filePaths
*
* @return array<string>
*/
private function interfaceFilePaths(ReflectionClass $reflection, array $filePaths): array
{
foreach ($reflection->interfaces() as $interface) {
$filePaths[] = $interface->sourceCode()->uri()?->path();
$path = $interface->sourceCode()->uri()?->path();
if (!$path) {
throw new RuntimeException(
sprintf('Source interface "%s" has no path associated with it', $interface->name()),
);
}

$filePaths[] = $path;
}

return $filePaths;
Expand Down
40 changes: 0 additions & 40 deletions phpstan-baseline.neon
Expand Up @@ -1295,46 +1295,6 @@ parameters:
count: 1
path: lib/Extension/ClassMover/Application/ClassReferences.php

-
message: "#^Method Phpactor\\\\Extension\\\\ClassMover\\\\Application\\\\Finder\\\\FileFinder\\:\\:allPhpFiles\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Extension/ClassMover/Application/Finder/FileFinder.php

-
message: "#^Method Phpactor\\\\Extension\\\\ClassMover\\\\Application\\\\Finder\\\\FileFinder\\:\\:interfaceFilePaths\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Extension/ClassMover/Application/Finder/FileFinder.php

-
message: "#^Method Phpactor\\\\Extension\\\\ClassMover\\\\Application\\\\Finder\\\\FileFinder\\:\\:interfaceFilePaths\\(\\) has parameter \\$filePaths with no type specified\\.$#"
count: 1
path: lib/Extension/ClassMover/Application/Finder/FileFinder.php

-
message: "#^Method Phpactor\\\\Extension\\\\ClassMover\\\\Application\\\\Finder\\\\FileFinder\\:\\:parentFilePaths\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Extension/ClassMover/Application/Finder/FileFinder.php

-
message: "#^Method Phpactor\\\\Extension\\\\ClassMover\\\\Application\\\\Finder\\\\FileFinder\\:\\:parentFilePaths\\(\\) has parameter \\$filePaths with no type specified\\.$#"
count: 1
path: lib/Extension/ClassMover/Application/Finder/FileFinder.php

-
message: "#^Method Phpactor\\\\Extension\\\\ClassMover\\\\Application\\\\Finder\\\\FileFinder\\:\\:pathsFromReflectionClass\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Extension/ClassMover/Application/Finder/FileFinder.php

-
message: "#^Method Phpactor\\\\Extension\\\\ClassMover\\\\Application\\\\Finder\\\\FileFinder\\:\\:traitFilePaths\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Extension/ClassMover/Application/Finder/FileFinder.php

-
message: "#^Method Phpactor\\\\Extension\\\\ClassMover\\\\Application\\\\Finder\\\\FileFinder\\:\\:traitFilePaths\\(\\) has parameter \\$filePaths with no type specified\\.$#"
count: 1
path: lib/Extension/ClassMover/Application/Finder/FileFinder.php

-
message: "#^Parameter \\#2 \\$subject of function preg_match expects string, string\\|false given\\.$#"
count: 1
Expand Down