Skip to content

Commit 7f21bf7

Browse files
committed
fix: Save realpath() as variable
1 parent 8f00426 commit 7f21bf7

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
@@ -117,7 +117,10 @@ public function testServiceAutoLoaderFromShareInstances(): void
117117
// look for Home controller, as that should be in base repo
118118
$actual = $classLoader(Home::class);
119119
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
120-
$actual = realpath($actual) !== false ? realpath($actual) : $actual;
120+
121+
$resolvedPath = realpath($actual);
122+
$actual = $resolvedPath !== false ? $resolvedPath : $actual;
123+
121124
$this->assertSame($expected, $actual);
122125
}
123126

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

138144
$autoloader->unregister();

0 commit comments

Comments
 (0)