[TASK] Harden \TYPO3\CMS\Extbase\Object\ObjectManager 00/59600/4
authorJohannes Seipelt <johannes.seipelt@3m5.de>
Thu, 31 Jan 2019 20:09:23 +0000 (21:09 +0100)
committerAndreas Wolf <andreas.wolf@typo3.org>
Fri, 1 Feb 2019 16:29:03 +0000 (17:29 +0100)
- Use strict type mode
- Use type hints whereever possible
- fix test of creating viewhelper instance using object manager

Releases: master
Resolves: #87605
Change-Id: Ia626ae1156dacd7c9ddcfefed9bb03d26ba91404
Reviewed-on: https://review.typo3.org/59600
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: TYPO3com <noreply@typo3.com>
Reviewed-by: Johannes Seipelt <johannes.seipelt@3m5.de>
Reviewed-by: Felix Döring <Felix.Doering@3m5.de>
Reviewed-by: André Schließer <andy.schliesser@gmail.com>
Tested-by: André Schließer <andy.schliesser@gmail.com>
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
typo3/sysext/extbase/Classes/Object/ObjectManager.php
typo3/sysext/extbase/Classes/Object/ObjectManagerInterface.php
typo3/sysext/fluid/Tests/Unit/Core/ViewHelper/ViewHelperResolverTest.php

index 3af8625..ccf191d 100644 (file)
@@ -1,4 +1,6 @@
 <?php
+declare(strict_types = 1);
+
 namespace TYPO3\CMS\Extbase\Object;
 
 /*
@@ -78,7 +80,7 @@ class ObjectManager implements ObjectManagerInterface
      * @return bool TRUE if the object has been registered, otherwise FALSE
      * @internal only to be used within Extbase, not part of TYPO3 Core API.
      */
-    public function isRegistered($objectName)
+    public function isRegistered(string $objectName): bool
     {
         return class_exists($objectName, true);
     }
@@ -90,7 +92,7 @@ class ObjectManager implements ObjectManagerInterface
      * @param array $constructorArguments
      * @return object The object instance
      */
-    public function get($objectName, ...$constructorArguments)
+    public function get(string $objectName, ...$constructorArguments): object
     {
         if ($objectName === 'DateTime') {
             $instance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($objectName, ...$constructorArguments);
@@ -107,7 +109,7 @@ class ObjectManager implements ObjectManagerInterface
      * @return int One of the Container::SCOPE_ constants
      * @throws \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException
      */
-    public function getScope($objectName)
+    public function getScope(string $objectName): int
     {
         if (!$this->isRegistered($objectName)) {
             throw new \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException('Object "' . $objectName . '" is not registered.', 1265367590);
@@ -121,7 +123,7 @@ class ObjectManager implements ObjectManagerInterface
      * @param string $className
      * @return object
      */
-    public function getEmptyObject($className)
+    public function getEmptyObject(string $className): object
     {
         return $this->objectContainer->getEmptyObject($className);
     }
index 7e0188a..12eaa7f 100644 (file)
@@ -1,4 +1,6 @@
 <?php
+declare(strict_types = 1);
+
 namespace TYPO3\CMS\Extbase\Object;
 
 /*
@@ -26,7 +28,7 @@ interface ObjectManagerInterface extends \TYPO3\CMS\Core\SingletonInterface
      * @return bool TRUE if the object has been registered, otherwise FALSE
      * @internal only to be used within Extbase, not part of TYPO3 Core API.
      */
-    public function isRegistered($objectName);
+    public function isRegistered(string $objectName): bool;
 
     /**
      * Returns a fresh or existing instance of the object specified by $objectName.
@@ -35,7 +37,7 @@ interface ObjectManagerInterface extends \TYPO3\CMS\Core\SingletonInterface
      * @param array ...$constructorArguments
      * @return object The object instance
      */
-    public function get($objectName, ...$constructorArguments);
+    public function get(string $objectName, ...$constructorArguments): object;
 
     /**
      * Create an instance of $className without calling its constructor
@@ -43,7 +45,7 @@ interface ObjectManagerInterface extends \TYPO3\CMS\Core\SingletonInterface
      * @param string $className
      * @return object
      */
-    public function getEmptyObject($className);
+    public function getEmptyObject(string $className): object;
 
     /**
      * Returns the scope of the specified object.
@@ -52,5 +54,5 @@ interface ObjectManagerInterface extends \TYPO3\CMS\Core\SingletonInterface
      * @return int One of the Container::SCOPE_ constants
      * @internal only to be used within Extbase, not part of TYPO3 Core API.
      */
-    public function getScope($objectName);
+    public function getScope(string $objectName): int;
 }
index 7ccee84..c1adb06 100644 (file)
@@ -34,12 +34,12 @@ class ViewHelperResolverTest extends UnitTestCase
             ->setMethods(['get'])
             ->disableOriginalConstructor()
             ->getMock();
-        $objectManager->expects($this->once())->method('get')->with('x')->willReturn('y');
+        $objectManager->expects($this->once())->method('get')->with('x')->willReturn(new \stdClass());
         $resolver = $this->getMockBuilder(ViewHelperResolver::class)
             ->setMethods(['getObjectManager'])
             ->getMock();
         $resolver->expects($this->once())->method('getObjectManager')->willReturn($objectManager);
-        $this->assertEquals('y', $resolver->createViewHelperInstanceFromClassName('x'));
+        $this->assertInstanceOf(\stdClass::class, $resolver->createViewHelperInstanceFromClassName('x'));
     }
 
     /**