Skip to content

Commit ccd49e4

Browse files
authored
Merge pull request #5681 from paulbalandan/deprecate-object-clean-path-methods
Deprecate object implementations of `clean_path()` function
2 parents d76e2a2 + 40446ff commit ccd49e4

File tree

9 files changed

+28
-67
lines changed

9 files changed

+28
-67
lines changed

app/Views/errors/html/error_exception.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
<!-- Source -->
3232
<div class="container">
33-
<p><b><?= esc(static::cleanPath($file, $line)) ?></b> at line <b><?= esc($line) ?></b></p>
33+
<p><b><?= esc(clean_path($file)) ?></b> at line <b><?= esc($line) ?></b></p>
3434

3535
<?php if (is_file($file)) : ?>
3636
<div class="source">
@@ -64,12 +64,12 @@
6464
<?php if (isset($row['file']) && is_file($row['file'])) :?>
6565
<?php
6666
if (isset($row['function']) && in_array($row['function'], ['include', 'include_once', 'require', 'require_once'], true)) {
67-
echo esc($row['function'] . ' ' . static::cleanPath($row['file']));
67+
echo esc($row['function'] . ' ' . clean_path($row['file']));
6868
} else {
69-
echo esc(static::cleanPath($row['file']) . ' : ' . $row['line']);
69+
echo esc(clean_path($row['file']) . ' : ' . $row['line']);
7070
}
7171
?>
72-
<?php else : ?>
72+
<?php else: ?>
7373
{PHP internal code}
7474
<?php endif; ?>
7575

@@ -350,7 +350,7 @@
350350

351351
<ol>
352352
<?php foreach ($files as $file) :?>
353-
<li><?= esc(static::cleanPath($file)) ?></li>
353+
<li><?= esc(clean_path($file)) ?></li>
354354
<?php endforeach ?>
355355
</ol>
356356
</div>

system/Debug/Exceptions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ protected function determineCodes(Throwable $exception): array
318318

319319
/**
320320
* This makes nicer looking paths for the error output.
321+
*
322+
* @deprecated Use dedicated `clean_path()` function.
321323
*/
322324
public static function cleanPath(string $file): string
323325
{

system/Debug/Toolbar/Collectors/BaseCollector.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace CodeIgniter\Debug\Toolbar\Collectors;
1313

14-
use CodeIgniter\Debug\Exceptions;
15-
1614
/**
1715
* Base Toolbar collector
1816
*/
@@ -174,13 +172,13 @@ public function display()
174172
}
175173

176174
/**
177-
* Clean Path
178-
*
179175
* This makes nicer looking paths for the error output.
176+
*
177+
* @deprecated Use the dedicated `clean_path()` function.
180178
*/
181179
public function cleanPath(string $file): string
182180
{
183-
return Exceptions::cleanPath($file);
181+
return clean_path($file);
184182
}
185183

186184
/**

system/Debug/Toolbar/Collectors/Files.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function display(): array
5858
$userFiles = [];
5959

6060
foreach ($rawFiles as $file) {
61-
$path = $this->cleanPath($file);
61+
$path = clean_path($file);
6262

6363
if (strpos($path, 'SYSTEMPATH') !== false) {
6464
$coreFiles[] = [

system/Log/Logger.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public function determineFile(): array
398398
// Find the first reference to a Logger class method
399399
foreach ($stackFrames as $frame) {
400400
if (\in_array($frame['function'], $logFunctions, true)) {
401-
$file = isset($frame['file']) ? $this->cleanFileNames($frame['file']) : 'unknown';
401+
$file = isset($frame['file']) ? clean_path($frame['file']) : 'unknown';
402402
$line = $frame['line'] ?? 'unknown';
403403

404404
return [
@@ -421,12 +421,11 @@ public function determineFile(): array
421421
* /var/www/site/app/Controllers/Home.php
422422
* becomes:
423423
* APPPATH/Controllers/Home.php
424+
*
425+
* @deprecated Use dedicated `clean_path()` function.
424426
*/
425427
protected function cleanFileNames(string $file): string
426428
{
427-
$file = str_replace(APPPATH, 'APPPATH/', $file);
428-
$file = str_replace(SYSTEMPATH, 'SYSTEMPATH/', $file);
429-
430-
return str_replace(FCPATH, 'FCPATH/', $file);
429+
return clean_path($file);
431430
}
432431
}

system/Test/TestLogger.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,15 @@ public static function didLog(string $level, $message)
7070
return false;
7171
}
7272

73-
// Expose cleanFileNames()
73+
/**
74+
* Expose filenames.
75+
*
76+
* @param string $file
77+
*
78+
* @deprecated No longer needed as underlying protected method is also deprecated.
79+
*/
7480
public function cleanup($file)
7581
{
76-
return $this->cleanFileNames($file);
82+
return clean_path($file);
7783
}
7884
}

tests/system/Debug/ExceptionsTest.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,4 @@ public function testDetermineCodes(): void
6161
$this->assertSame([500, 1], $determineCodes(new RuntimeException('That.', 600)));
6262
$this->assertSame([404, 1], $determineCodes(new RuntimeException('There.', 404)));
6363
}
64-
65-
/**
66-
* @dataProvider dirtyPathsProvider
67-
*/
68-
public function testCleanPaths(string $file, string $expected): void
69-
{
70-
$this->assertSame($expected, Exceptions::cleanPath($file));
71-
}
72-
73-
public function dirtyPathsProvider()
74-
{
75-
$ds = DIRECTORY_SEPARATOR;
76-
77-
yield from [
78-
[
79-
APPPATH . 'Config' . $ds . 'App.php',
80-
'APPPATH' . $ds . 'Config' . $ds . 'App.php',
81-
],
82-
[
83-
SYSTEMPATH . 'CodeIgniter.php',
84-
'SYSTEMPATH' . $ds . 'CodeIgniter.php',
85-
],
86-
[
87-
VENDORPATH . 'autoload.php',
88-
'VENDORPATH' . $ds . 'autoload.php',
89-
],
90-
[
91-
FCPATH . 'index.php',
92-
'FCPATH' . $ds . 'index.php',
93-
],
94-
];
95-
}
9664
}

tests/system/Log/LoggerTest.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use CodeIgniter\Log\Exceptions\LogException;
1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use CodeIgniter\Test\Mock\MockLogger as LoggerConfig;
18-
use CodeIgniter\Test\TestLogger;
1918
use Exception;
2019
use Tests\Support\Log\Handlers\TestHandler;
2120

@@ -32,7 +31,7 @@ public function testThrowsExceptionWithBadHandlerSettings()
3231
$this->expectException(FrameworkException::class);
3332
$this->expectExceptionMessage(lang('Core.noHandlers', ['LoggerConfig']));
3433

35-
$logger = new Logger($config);
34+
new Logger($config);
3635
}
3736

3837
public function testLogThrowsExceptionOnInvalidLevel()
@@ -60,12 +59,9 @@ public function testLogReturnsFalseWhenLogNotHandled()
6059
public function testLogActuallyLogs()
6160
{
6261
$config = new LoggerConfig();
63-
// $Config->handlers['TestHandler']['handles'] = [LogLevel::CRITICAL];
64-
6562
$logger = new Logger($config);
6663

6764
$expected = 'DEBUG - ' . date('Y-m-d') . ' --> Test message';
68-
6965
$logger->log('debug', 'Test message');
7066

7167
$logs = TestHandler::getLogs();
@@ -76,7 +72,8 @@ public function testLogActuallyLogs()
7672

7773
public function testLogDoesnotLogUnhandledLevels()
7874
{
79-
$config = new LoggerConfig();
75+
$config = new LoggerConfig();
76+
8077
$config->handlers['Tests\Support\Log\Handlers\TestHandler']['handles'] = ['critical'];
8178

8279
$logger = new Logger($config);
@@ -374,17 +371,6 @@ public function testNonStringMessage()
374371
$this->assertStringContainsString($expected, $logs[0]);
375372
}
376373

377-
public function testFilenameCleaning()
378-
{
379-
$config = new LoggerConfig();
380-
$logger = new TestLogger($config);
381-
382-
$ohoh = APPPATH . 'LoggerTest';
383-
$expected = 'APPPATH/LoggerTest';
384-
385-
$this->assertSame($expected, $logger->cleanup($ohoh));
386-
}
387-
388374
public function testDetermineFileNoStackTrace()
389375
{
390376
$config = new LoggerConfig();

user_guide_src/source/changelogs/v4.2.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Deprecations
4040
************
4141

4242
- ``CodeIgniter\Database\SQLSRV\Connection::getError()`` is deprecated. Use ``CodeIgniter\Database\SQLSRV\Connection::error()`` instead.
43+
- ``CodeIgniter\Debug\Exceptions::cleanPath()`` and ``CodeIgniter\Debug\Toolbar\Collectors\BaseCollector::cleanPath()`` are deprecated. Use the ``clean_path()`` function instead.
44+
- ``CodeIgniter\Log\Logger::cleanFilenames()`` and ``CodeIgniter\Test\TestLogger::cleanup()`` are both deprecated. Use the ``clean_path()`` function instead.
4345

4446
Bugs Fixed
4547
**********

0 commit comments

Comments
 (0)