Skip to content

Commit

Permalink
Adding more types in the FileFinder (#2606)
Browse files Browse the repository at this point in the history
* Adding more types in the FileFinder

* Adding type safety

* Using continue instead of exceptions
  • Loading branch information
mamazu committed Mar 22, 2024
1 parent b770c2e commit a6f3a78
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 53 deletions.
54 changes: 41 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,61 @@ 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>
*/
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 === null) {
continue;
}
$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 === null) {
continue;
}
$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 === null) {
continue;
}
$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

0 comments on commit a6f3a78

Please sign in to comment.