Skip to content

Commit 951b1b3

Browse files
committed
fix: Save realpath() as variable
1 parent 0713306 commit 951b1b3

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

system/Autoloader/FileLocator.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =
195195

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

201202
if ($prioritizeApp) {
202203
$foundPaths[] = $fullPath;
@@ -273,14 +274,16 @@ protected function getNamespaces()
273274
*/
274275
public function findQualifiedNameFromPath(string $path)
275276
{
276-
$path = realpath($path) !== false ? realpath($path) : $path;
277+
$resolvedPath = realpath($path);
278+
$path = $resolvedPath !== false ? $resolvedPath : $path;
277279

278280
if (! is_file($path)) {
279281
return false;
280282
}
281283

282284
foreach ($this->getNamespaces() as $namespace) {
283-
$namespace['path'] = realpath($namespace['path']) !== false ? realpath($namespace['path']) : $namespace['path'];
285+
$resolvedNamespacePath = realpath($namespace['path']);
286+
$namespace['path'] = $resolvedNamespacePath !== false ? $resolvedNamespacePath : $namespace['path'];
284287

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

334337
foreach ($this->getNamespaces() as $namespace) {
335-
$fullPath = $namespace['path'] . $path;
336-
$fullPath = realpath($fullPath) !== false ? realpath($fullPath) : $fullPath;
338+
$fullPath = $namespace['path'] . $path;
339+
$resolvedPath = realpath($fullPath);
340+
$fullPath = $resolvedPath !== false ? $resolvedPath : $fullPath;
337341

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

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

372377
if (! is_dir($fullPath)) {
373378
continue;
@@ -393,8 +398,9 @@ public function listNamespaceFiles(string $prefix, string $path): array
393398
*/
394399
protected function legacyLocate(string $file, ?string $folder = null)
395400
{
396-
$path = APPPATH . ($folder === null ? $file : $folder . '/' . $file);
397-
$path = realpath($path) !== false ? realpath($path) : $path;
401+
$path = APPPATH . ($folder === null ? $file : $folder . '/' . $file);
402+
$resolvedPath = realpath($path);
403+
$path = $resolvedPath !== false ? $resolvedPath : $path;
398404

399405
if (is_file($path)) {
400406
return $path;

tests/system/Autoloader/AutoloaderTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ public function testServiceAutoLoaderFromShareInstances(): void
116116
// look for Home controller, as that should be in base repo
117117
$actual = $classLoader(Home::class);
118118
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
119-
$actual = realpath($actual) !== false ? realpath($actual) : $actual;
119+
120+
$resolvedPath = realpath($actual);
121+
$actual = $resolvedPath !== false ? $resolvedPath : $actual;
122+
120123
$this->assertSame($expected, $actual);
121124
}
122125

@@ -131,7 +134,10 @@ public function testServiceAutoLoader(): void
131134
// look for Home controller, as that should be in base repo
132135
$actual = $classLoader(Home::class);
133136
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
134-
$actual = realpath($actual) !== false ? realpath($actual) : $actual;
137+
138+
$resolvedPath = realpath($actual);
139+
$actual = $resolvedPath !== false ? $resolvedPath : $actual;
140+
135141
$this->assertSame($expected, $actual);
136142

137143
$autoloader->unregister();

0 commit comments

Comments
 (0)