From 62feafc00453579808be6041acb95587a526467a Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Wed, 2 Aug 2023 10:27:35 +0800 Subject: [PATCH 1/3] Remove trimming logic of `Autoloader::loadClass()` --- system/Autoloader/Autoloader.php | 3 --- tests/system/Autoloader/AutoloaderTest.php | 9 --------- 2 files changed, 12 deletions(-) diff --git a/system/Autoloader/Autoloader.php b/system/Autoloader/Autoloader.php index dc0b72cd6122..d5c155685e12 100644 --- a/system/Autoloader/Autoloader.php +++ b/system/Autoloader/Autoloader.php @@ -260,9 +260,6 @@ public function loadClassmap(string $class): void */ public function loadClass(string $class): void { - $class = trim($class, '\\'); - $class = str_ireplace('.php', '', $class); - $this->loadInNamespace($class); } diff --git a/tests/system/Autoloader/AutoloaderTest.php b/tests/system/Autoloader/AutoloaderTest.php index d877d66e406d..8097ce962b5a 100644 --- a/tests/system/Autoloader/AutoloaderTest.php +++ b/tests/system/Autoloader/AutoloaderTest.php @@ -146,15 +146,6 @@ public function testMatchesWithPrecedingSlash(): void $this->assertSame($expected, $actual); } - public function testMatchesWithFileExtension(): void - { - /** @var Autoloader&MockObject $classLoader */ - $classLoader = $this->getMockBuilder(Autoloader::class)->onlyMethods(['loadInNamespace'])->getMock(); - $classLoader->expects($this->once())->method('loadInNamespace')->with(Home::class); - - $classLoader->loadClass('\App\Controllers\Home.php'); - } - public function testMissingFile(): void { $this->assertFalse(($this->classLoader)('\App\Missing\Classname')); From c7135801417c924917e645740ba4a12a4ca6f2f8 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Wed, 2 Aug 2023 10:30:50 +0800 Subject: [PATCH 2/3] Add Closure signature --- tests/system/Autoloader/AutoloaderTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/system/Autoloader/AutoloaderTest.php b/tests/system/Autoloader/AutoloaderTest.php index 8097ce962b5a..4c81537dbf09 100644 --- a/tests/system/Autoloader/AutoloaderTest.php +++ b/tests/system/Autoloader/AutoloaderTest.php @@ -20,7 +20,6 @@ use Config\Modules; use Config\Services; use InvalidArgumentException; -use PHPUnit\Framework\MockObject\MockObject; use RuntimeException; use UnnamespacedClass; @@ -34,6 +33,10 @@ final class AutoloaderTest extends CIUnitTestCase use ReflectionHelper; private Autoloader $loader; + + /** + * @phpstan-var Closure(string): (false|string) + */ private Closure $classLoader; protected function setUp(): void From b93b35b908e7985ec5226109c4605a4e68dcc46c Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Wed, 2 Aug 2023 10:37:25 +0800 Subject: [PATCH 3/3] Add to changelog --- user_guide_src/source/changelogs/v4.4.0.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index 5cf06105d29f..4747e953fc29 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -58,6 +58,12 @@ Property Name The property ``Factories::$basenames`` has been renamed to ``$aliases``. +Autoloader +---------- + +Previously, CodeIgniter's autoloader allowed loading class names ending with the `.php` extension. This means instantiating objects like `new Foo.php()` was possible +and would instantiate as `new Foo()`. Since `Foo.php` is an invalid class name, this behavior of the autoloader is changed. Now, instantiating such classes would fail. + .. _v440-interface-changes: Interface Changes