From ded08cbdcdb158b81eec09f1db8911ea9fbbd50e Mon Sep 17 00:00:00 2001 From: Alexander Schnitzler <git@alexanderschnitzler.de> Date: Sun, 5 Mar 2023 15:43:48 +0100 Subject: [PATCH] [TASK] Introduce type declarations for makeInstance() Releases: main Resolves: #100091 Change-Id: I20e180f174ad4511d33800d2c38b071de5ea4b86 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78031 Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de> Reviewed-by: Dmitry Dulepov <9csxfqr4jy@liamekaens.com> Tested-by: core-ci <typo3@b13.com> Tested-by: Dmitry Dulepov <9csxfqr4jy@liamekaens.com> Tested-by: Benni Mack <benni@typo3.org> Reviewed-by: Benni Mack <benni@typo3.org> Tested-by: Oliver Klee <typo3-coding@oliverklee.de> --- .../core/Classes/Utility/GeneralUtility.php | 7 ++- .../Tests/Unit/Utility/GeneralUtilityTest.php | 48 ------------------- 2 files changed, 3 insertions(+), 52 deletions(-) diff --git a/typo3/sysext/core/Classes/Utility/GeneralUtility.php b/typo3/sysext/core/Classes/Utility/GeneralUtility.php index 24f6ea1228ea..c9a4380fd389 100644 --- a/typo3/sysext/core/Classes/Utility/GeneralUtility.php +++ b/typo3/sysext/core/Classes/Utility/GeneralUtility.php @@ -2911,18 +2911,17 @@ class GeneralUtility * * @template T of object * @param class-string<T> $className name of the class to instantiate, must not be empty and not start with a backslash - * @param mixed $constructorArguments Arguments for the constructor * @return T the created instance * @throws \InvalidArgumentException if $className is empty or starts with a backslash */ - public static function makeInstance($className, ...$constructorArguments) + public static function makeInstance(string $className, mixed ...$constructorArguments): object { // PHPStan will complain about this check. That's okay as we're checking a contract violation here. - if (!is_string($className) || empty($className)) { + if ($className === '') { throw new \InvalidArgumentException('$className must be a non empty string.', 1288965219); } // Never instantiate with a beginning backslash, otherwise things like singletons won't work. - if ($className[0] === '\\') { + if (str_starts_with($className, '\\')) { throw new \InvalidArgumentException( '$className "' . $className . '" must not start with a backslash.', 1420281366 diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php index c2688b98a57f..6a4bb6775f73 100644 --- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php +++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php @@ -2922,54 +2922,6 @@ class GeneralUtilityTest extends UnitTestCase GeneralUtility::makeInstance(''); } - /** - * @test - */ - public function makeInstanceWithNullClassNameThrowsException(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionCode(1288965219); - - // @phpstan-ignore-next-line We're explicitly checking the behavior for a contract violation. - GeneralUtility::makeInstance(null); - } - - /** - * @test - */ - public function makeInstanceWithZeroStringClassNameThrowsException(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionCode(1288965219); - - // @phpstan-ignore-next-line We're explicitly checking the behavior for a contract violation. - GeneralUtility::makeInstance(0); - } - - /** - * @test - */ - public function makeInstanceWithEmptyArrayThrowsException(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionCode(1288965219); - - // @phpstan-ignore-next-line We're explicitly checking the behavior for a contract violation. - GeneralUtility::makeInstance([]); - } - - /** - * @test - */ - public function makeInstanceWithNonEmptyArrayThrowsException(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionCode(1288965219); - - // @phpstan-ignore-next-line We're explicitly checking the behavior for a contract violation. - GeneralUtility::makeInstance(['foo']); - } - /** * @test */ -- GitLab