Skip to content

Commit

Permalink
Adding type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
mamazu committed Mar 17, 2024
1 parent 013413b commit 36cb706
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 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 @@ -62,7 +62,9 @@ private function pathsFromReflectionClass(ReflectionClass $reflection, bool $pri
$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 @@ -84,43 +86,62 @@ private function allPhpFiles(Filesystem $filesystem): FileList
}

/**
* @param array<string|null> $filePaths
* @param array<string> $filePaths
*
* @return array<string|null>
* @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) {
throw new RuntimeException(
sprintf('Source class "%s" has no path associated with it', $context->name()),
);
}
$filePaths[] = $path;
$context = $context->parent();
}

return $filePaths;
}

/**
* @param array<string|null> $filePaths
* @param array<string> $filePaths
*
* @return array<string|null>
* @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;
}

/**
* @param array<string|null> $filePaths
* @param array<string> $filePaths
*
* @return array<string|null>
* @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

0 comments on commit 36cb706

Please sign in to comment.