From 7f21bf76c842a3482caccf3a7f0beab3af999889 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Sun, 3 Nov 2024 08:51:59 +0300 Subject: [PATCH] fix: Save `realpath()` as variable --- system/Autoloader/FileLocator.php | 26 +++++++++++++--------- tests/system/Autoloader/AutoloaderTest.php | 10 +++++++-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index dfeadd25958c..8e2564e00941 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/tests/system/Autoloader/AutoloaderTest.php b/tests/system/Autoloader/AutoloaderTest.php index 040cfa76375a..f69bb87b169d 100644 --- a/tests/system/Autoloader/AutoloaderTest.php +++ b/tests/system/Autoloader/AutoloaderTest.php @@ -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); } @@ -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();