From f73db111e6809133aafff2b5967575bbf79deae8 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sat, 21 Feb 2026 21:14:26 +0800 Subject: [PATCH] refactor: remove deprecated methods in `Autoloader` --- system/Autoloader/Autoloader.php | 83 --------------------- tests/system/Autoloader/AutoloaderTest.php | 49 ------------ user_guide_src/source/changelogs/v4.8.0.rst | 3 + 3 files changed, 3 insertions(+), 132 deletions(-) diff --git a/system/Autoloader/Autoloader.php b/system/Autoloader/Autoloader.php index 65c535e8611e..08641a8ba45f 100644 --- a/system/Autoloader/Autoloader.php +++ b/system/Autoloader/Autoloader.php @@ -320,51 +320,6 @@ protected function includeFile(string $file) return false; } - /** - * Check file path. - * - * Checks special characters that are illegal in filenames on certain - * operating systems and special characters requiring special escaping - * to manipulate at the command line. Replaces spaces and consecutive - * dashes with a single dash. Trim period, dash and underscore from beginning - * and end of filename. - * - * @return string The sanitized filename - * - * @deprecated No longer used. See https://github.com/codeigniter4/CodeIgniter4/issues/7055 - */ - public function sanitizeFilename(string $filename): string - { - // Only allow characters deemed safe for POSIX portable filenames. - // Plus the forward slash for directory separators since this might be a path. - // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_278 - // Modified to allow backslash and colons for on Windows machines. - $result = preg_match_all('/[^0-9\p{L}\s\/\-_.:\\\\]/u', $filename, $matches); - - if ($result > 0) { - $chars = implode('', $matches[0]); - - throw new InvalidArgumentException( - 'The file path contains special characters "' . $chars - . '" that are not allowed: "' . $filename . '"', - ); - } - if ($result === false) { - $message = preg_last_error_msg(); - - throw new RuntimeException($message . '. filename: "' . $filename . '"'); - } - - // Clean up our filename edges. - $cleanFilename = trim($filename, '.-_'); - - if ($filename !== $cleanFilename) { - throw new InvalidArgumentException('The characters ".-_" are not allowed in filename edges: "' . $filename . '"'); - } - - return $cleanFilename; - } - /** * @param array{only?: list, exclude?: list} $composerPackages */ @@ -442,44 +397,6 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa $this->addNamespace($newPaths); } - /** - * Locates autoload information from Composer, if available. - * - * @deprecated No longer used. - * - * @return void - */ - protected function discoverComposerNamespaces() - { - if (! is_file(COMPOSER_PATH)) { - return; - } - - /** - * @var ClassLoader $composer - */ - $composer = include COMPOSER_PATH; - $paths = $composer->getPrefixesPsr4(); - $classes = $composer->getClassMap(); - - unset($composer); - - // Get rid of CodeIgniter so we don't have duplicates - if (isset($paths['CodeIgniter\\'])) { - unset($paths['CodeIgniter\\']); - } - - $newPaths = []; - - foreach ($paths as $key => $value) { - // Composer stores namespaces with trailing slash. We don't. - $newPaths[rtrim($key, '\\ ')] = $value; - } - - $this->prefixes = array_merge($this->prefixes, $newPaths); - $this->classmap = array_merge($this->classmap, $classes); - } - /** * Loads helpers */ diff --git a/tests/system/Autoloader/AutoloaderTest.php b/tests/system/Autoloader/AutoloaderTest.php index cd73356958fe..7fcd730740f1 100644 --- a/tests/system/Autoloader/AutoloaderTest.php +++ b/tests/system/Autoloader/AutoloaderTest.php @@ -16,8 +16,6 @@ use App\Controllers\Home; use Closure; use CodeIgniter\Exceptions\ConfigException; -use CodeIgniter\Exceptions\InvalidArgumentException; -use CodeIgniter\Exceptions\RuntimeException; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\ReflectionHelper; use Config\Autoload; @@ -231,53 +229,6 @@ public function testloadClassNonNamespaced(): void $this->assertFalse(($this->classLoader)('Modules')); } - public function testSanitizationContailsSpecialChars(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage( - 'The file path contains special characters "${}!#" that are not allowed: "${../path}!#/to/some/file.php_"', - ); - - $test = '${../path}!#/to/some/file.php_'; - - $this->loader->sanitizeFilename($test); - } - - public function testSanitizationFilenameEdges(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage( - 'The characters ".-_" are not allowed in filename edges: "/path/to/some/file.php_"', - ); - - $test = '/path/to/some/file.php_'; - - $this->loader->sanitizeFilename($test); - } - - public function testSanitizationRegexError(): void - { - $this->expectException(RuntimeException::class); - - $test = mb_convert_encoding('クラスファイル.php', 'EUC-JP', 'UTF-8'); - - $this->loader->sanitizeFilename($test); - } - - public function testSanitizationAllowUnicodeChars(): void - { - $test = 'Ä/path/to/some/file.php'; - - $this->assertSame($test, $this->loader->sanitizeFilename($test)); - } - - public function testSanitizationAllowsWindowsFilepaths(): void - { - $test = 'C:\path\to\some/file.php'; - - $this->assertSame($test, $this->loader->sanitizeFilename($test)); - } - public function testFindsComposerRoutes(): void { $config = new Autoload(); diff --git a/user_guide_src/source/changelogs/v4.8.0.rst b/user_guide_src/source/changelogs/v4.8.0.rst index b45b1fda6b16..056207038ed0 100644 --- a/user_guide_src/source/changelogs/v4.8.0.rst +++ b/user_guide_src/source/changelogs/v4.8.0.rst @@ -32,6 +32,9 @@ Method Signature Changes Removed Deprecated Items ======================== +- **Autoloader:** Removed the following deprecated methods: + - ``CodeIgniter\Autoloader\Autoloader::sanitizeFileName()`` + - ``CodeIgniter\Autoloader\Autoloader::discoverComposerNamespaces()`` - **Exceptions:** Removed the following properties and methods deprecated since v4.4.0: - ``CodeIgniter\Debug\Exceptions::$ob_level`` - ``CodeIgniter\Debug\Exceptions::$viewPath``