Skip to content

Commit

Permalink
fix: Save realpath() as variable
Browse files Browse the repository at this point in the history
  • Loading branch information
neznaika0 committed Nov 4, 2024
1 parent 14d4774 commit 9f48046
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
26 changes: 16 additions & 10 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =

foreach ($this->getNamespaces() as $namespace) {
if (isset($namespace['path']) && is_file($namespace['path'] . $path)) {
$fullPath = $namespace['path'] . $path;
$fullPath = realpath($fullPath) !== false ? realpath($fullPath) : $fullPath;
$fullPath = $namespace['path'] . $path;
$resolvedPath = realpath($fullPath);
$fullPath = $resolvedPath !== false ? $resolvedPath : $fullPath;

if ($prioritizeApp) {
$foundPaths[] = $fullPath;
Expand Down Expand Up @@ -273,14 +274,16 @@ protected function getNamespaces()
*/
public function findQualifiedNameFromPath(string $path)
{
$path = realpath($path) !== false ? realpath($path) : $path;
$resolvedPath = realpath($path);
$path = $resolvedPath !== false ? $resolvedPath : $path;

if (! is_file($path)) {
return false;
}

foreach ($this->getNamespaces() as $namespace) {
$namespace['path'] = realpath($namespace['path']) !== false ? realpath($namespace['path']) : $namespace['path'];
$resolvedNamespacePath = realpath($namespace['path']);
$namespace['path'] = $resolvedNamespacePath !== false ? $resolvedNamespacePath : $namespace['path'];

if ($namespace['path'] === '') {
continue;
Expand Down Expand Up @@ -332,8 +335,9 @@ public function listFiles(string $path): array
helper('filesystem');

foreach ($this->getNamespaces() as $namespace) {
$fullPath = $namespace['path'] . $path;
$fullPath = realpath($fullPath) !== false ? realpath($fullPath) : $fullPath;
$fullPath = $namespace['path'] . $path;
$resolvedPath = realpath($fullPath);
$fullPath = $resolvedPath !== false ? $resolvedPath : $fullPath;

if (! is_dir($fullPath)) {
continue;
Expand Down Expand Up @@ -366,8 +370,9 @@ public function listNamespaceFiles(string $prefix, string $path): array

// autoloader->getNamespace($prefix) returns an array of paths for that namespace
foreach ($this->autoloader->getNamespace($prefix) as $namespacePath) {
$fullPath = rtrim($namespacePath, '/') . '/' . $path;
$fullPath = realpath($fullPath) !== false ? realpath($fullPath) : $fullPath;
$fullPath = rtrim($namespacePath, '/') . '/' . $path;
$resolvedPath = realpath($fullPath);
$fullPath = $resolvedPath !== false ? $resolvedPath : $fullPath;

if (! is_dir($fullPath)) {
continue;
Expand All @@ -393,8 +398,9 @@ public function listNamespaceFiles(string $prefix, string $path): array
*/
protected function legacyLocate(string $file, ?string $folder = null)
{
$path = APPPATH . ($folder === null ? $file : $folder . '/' . $file);
$path = realpath($path) !== false ? realpath($path) : $path;
$path = APPPATH . ($folder === null ? $file : $folder . '/' . $file);
$resolvedPath = realpath($path);
$path = $resolvedPath !== false ? $resolvedPath : $path;

if (is_file($path)) {
return $path;
Expand Down
10 changes: 8 additions & 2 deletions tests/system/Autoloader/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ public function testServiceAutoLoaderFromShareInstances(): void
// look for Home controller, as that should be in base repo
$actual = $classLoader(Home::class);
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
$actual = realpath($actual) !== false ? realpath($actual) : $actual;

$resolvedPath = realpath($actual);
$actual = $resolvedPath !== false ? $resolvedPath : $actual;

$this->assertSame($expected, $actual);
}

Expand All @@ -132,7 +135,10 @@ public function testServiceAutoLoader(): void
// look for Home controller, as that should be in base repo
$actual = $classLoader(Home::class);
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
$actual = realpath($actual) !== false ? realpath($actual) : $actual;

$resolvedPath = realpath($actual);
$actual = $resolvedPath !== false ? $resolvedPath : $actual;

$this->assertSame($expected, $actual);

$autoloader->unregister();
Expand Down

0 comments on commit 9f48046

Please sign in to comment.