[FEATURE] Add properties default values in class schema 23/58923/7
authorRomain Canon <romain.hydrocanon@gmail.com>
Wed, 21 Nov 2018 13:45:58 +0000 (14:45 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Fri, 11 Jan 2019 16:20:08 +0000 (17:20 +0100)
Allows accessing the default values of a class property, without having
to use the reflection everytime it is needed.

The default value can be accessed with the key `defaultValue` in the
property schema.

Resolves: #86964
Releases: master
Change-Id: I17c1dfd3db9c26deaffd760270e480543521a9a4
Reviewed-on: https://review.typo3.org/58923
Reviewed-by: Alexander Schnitzler <typo3@alexanderschnitzler.de>
Tested-by: Alexander Schnitzler <typo3@alexanderschnitzler.de>
Tested-by: TYPO3com <noreply@typo3.com>
Reviewed-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Tested-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Documentation/Changelog/master/Feature-86964-AllowGettingClassPropertyDefaultValue.rst [new file with mode: 0644]
typo3/sysext/extbase/Classes/Reflection/ClassSchema.php
typo3/sysext/extbase/Tests/Unit/Reflection/ClassSchemaTest.php
typo3/sysext/extbase/Tests/Unit/Reflection/Fixture/DummyClassWithAllTypesOfProperties.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-86964-AllowGettingClassPropertyDefaultValue.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-86964-AllowGettingClassPropertyDefaultValue.rst
new file mode 100644 (file)
index 0000000..736d998
--- /dev/null
@@ -0,0 +1,27 @@
+.. include:: ../../Includes.txt
+
+============================================================
+Feature: #86964 - Allow getting class property default value
+============================================================
+
+See :issue:`86964`
+
+Description
+===========
+
+It is now possible to get the default value of a class property when using the ``ReflectionService``.
+
+.. code-block:: php
+
+       class MyClass
+       {
+           public $myProperty = 'foo';
+       }
+
+       $property = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class)
+           ->getClassSchema(MyClass::class)
+           ->getProperty('myProperty');
+
+       $defaultValue = $property['defaultValue']; // "foo"
+
+.. index:: PHP-API, ext:extbase, NotScanned
index 883310e..555aa9e 100644 (file)
@@ -149,20 +149,23 @@ class ClassSchema
     {
         $annotationReader = new AnnotationReader();
 
+        $defaultProperties = $reflectionClass->getDefaultProperties();
+
         foreach ($reflectionClass->getProperties() as $reflectionProperty) {
             $propertyName = $reflectionProperty->getName();
 
             $this->properties[$propertyName] = [
-                'default'     => $reflectionProperty->isDefault(),
-                'private'     => $reflectionProperty->isPrivate(),
-                'protected'   => $reflectionProperty->isProtected(),
-                'public'      => $reflectionProperty->isPublic(),
-                'static'      => $reflectionProperty->isStatic(),
-                'type'        => null, // Extbase
-                'elementType' => null, // Extbase
-                'annotations' => [],
-                'tags'        => [],
-                'validators'  => []
+                'default'      => $reflectionProperty->isDefault(),
+                'defaultValue' => $defaultProperties[$propertyName] ?? null,
+                'private'      => $reflectionProperty->isPrivate(),
+                'protected'    => $reflectionProperty->isProtected(),
+                'public'       => $reflectionProperty->isPublic(),
+                'static'       => $reflectionProperty->isStatic(),
+                'type'         => null, // Extbase
+                'elementType'  => null, // Extbase
+                'annotations'  => [],
+                'tags'         => [],
+                'validators'   => []
             ];
 
             $docCommentParser = new DocCommentParser(true);
index 8cd9a47..c318c67 100644 (file)
@@ -95,6 +95,7 @@ class ClassSchemaTest extends UnitTestCase
                 'publicStaticProperty',
                 'protectedStaticProperty',
                 'privateStaticProperty',
+                'publicPropertyWithDefaultValue',
                 'propertyWithIgnoredTags',
                 'propertyWithInjectAnnotation',
                 'propertyWithTransientAnnotation',
@@ -277,6 +278,17 @@ class ClassSchemaTest extends UnitTestCase
     /**
      * @test
      */
+    public function classSchemaDetectsPropertyDefaultValue()
+    {
+        $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
+
+        $propertyDefinition = $classSchema->getProperty('publicPropertyWithDefaultValue');
+        static::assertSame('foo', $propertyDefinition['defaultValue']);
+    }
+
+    /**
+     * @test
+     */
     public function classSchemaDetectsPropertyVisibility()
     {
         $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
index 1f70785..2694eea 100644 (file)
@@ -34,6 +34,8 @@ class DummyClassWithAllTypesOfProperties
 
     private static $privateStaticProperty;
 
+    public $publicPropertyWithDefaultValue = 'foo';
+
     /**
      * @license
      * @copyright