[TASK] Use lambda functions instead of create_function 82/23782/4
authorChristoph Dörfel <christoph.doerfel@gmail.com>
Thu, 12 Sep 2013 09:57:47 +0000 (11:57 +0200)
committerMarkus Klein <klein.t3@mfc-linz.at>
Thu, 12 Sep 2013 15:43:53 +0000 (17:43 +0200)
Make use of the possibilities of PHP 5.3+ and replace all
instances of create_function with an inlined function call.

Change-Id: I3ebabd56efe5d7e3ee07cffbf1f3bb6b5c85e7ff
Resolves: #51937
Releases: 6.2
Reviewed-on: https://review.typo3.org/23782
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Christoph Dörfel
Reviewed-by: Markus Klein
Tested-by: Markus Klein
typo3/sysext/backend/Classes/Form/Element/InlineElement.php
typo3/sysext/backend/Classes/Form/FlexFormsHelper.php
typo3/sysext/backend/Classes/Form/FormEngine.php
typo3/sysext/cms/ext_tables.php
typo3/sysext/core/Classes/Database/RelationHandler.php
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index 0a04346..0206783 100644 (file)
@@ -1507,9 +1507,16 @@ class InlineElement {
                if ($foreignConfig['type'] == 'select') {
                        // Getting the selector box items from the system
                        $selItems = $this->fObj->addSelectOptionsToItemArray($this->fObj->initItemArray($PA['fieldConf']), $PA['fieldConf'], $this->fObj->setTSconfig($table, $row), $field);
+
                        // Possibly filter some items:
-                       $keepItemsFunc = create_function('$value', 'return $value[1];');
-                       $selItems = GeneralUtility::keepItemsInArray($selItems, $PA['fieldTSConfig']['keepItems'], $keepItemsFunc);
+                       $selItems = GeneralUtility::keepItemsInArray(
+                               $selItems,
+                               $PA['fieldTSConfig']['keepItems'],
+                               function ($value) {
+                                       return $value[1];
+                               }
+                       );
+
                        // Possibly add some items:
                        $selItems = $this->fObj->addItems($selItems, $PA['fieldTSConfig']['addItems.']);
                        if (isset($config['itemsProcFunc']) && $config['itemsProcFunc']) {
index fbea122..d3ce565 100644 (file)
@@ -197,9 +197,16 @@ class FlexFormsHelper extends \TYPO3\CMS\Backend\Form\FormEngine {
                        }
                        // Getting the selector box items from system
                        $selItems = $this->addSelectOptionsToItemArray($this->initItemArray($field['TCEforms']), $field['TCEforms'], $this->setTSconfig($table, $tableRow), $tableField);
+
                        // Possibly filter some items
-                       $keepItemsFunc = create_function('$value', 'return $value[1];');
-                       $selItems = GeneralUtility::keepItemsInArray($selItems, $keepItems, $keepItemsFunc);
+                       $selItems = GeneralUtility::keepItemsInArray(
+                               $selItems,
+                               $keepItems,
+                               function ($value) {
+                                       return $value[1];
+                               }
+                       );
+
                        // Possibly add some items
                        $selItems = $this->addItems($selItems, $addItems);
                        // Process items by a user function
index f0ab270..d3576ce 100644 (file)
@@ -1741,9 +1741,16 @@ function ' . $evalData . '(value) {
                $specConf = $this->getSpecConfFromString($PA['extra'], $PA['fieldConf']['defaultExtras']);
                // Getting the selector box items from the system
                $selItems = $this->addSelectOptionsToItemArray($this->initItemArray($PA['fieldConf']), $PA['fieldConf'], $this->setTSconfig($table, $row), $field);
+
                // Possibly filter some items:
-               $keepItemsFunc = create_function('$value', 'return $value[1];');
-               $selItems = GeneralUtility::keepItemsInArray($selItems, $PA['fieldTSConfig']['keepItems'], $keepItemsFunc);
+               $selItems = GeneralUtility::keepItemsInArray(
+                       $selItems,
+                       $PA['fieldTSConfig']['keepItems'],
+                       function ($value) {
+                               return $value[1];
+                       }
+               );
+
                // Possibly add some items:
                $selItems = $this->addItems($selItems, $PA['fieldTSConfig']['addItems.']);
                // Process items by a user function:
@@ -2225,9 +2232,17 @@ function ' . $evalData . '(value) {
                $removeItems = GeneralUtility::trimExplode(',', $PA['fieldTSConfig']['removeItems'], TRUE);
                // Get the array with selected items:
                $itemArray = GeneralUtility::trimExplode(',', $PA['itemFormElValue'], TRUE);
+
                // Possibly filter some items:
-               $keepItemsFunc = create_function('$value', '$parts=explode(\'|\',$value,2); return rawurldecode($parts[0]);');
-               $itemArray = GeneralUtility::keepItemsInArray($itemArray, $PA['fieldTSConfig']['keepItems'], $keepItemsFunc);
+               $itemArray = GeneralUtility::keepItemsInArray(
+                       $itemArray,
+                       $PA['fieldTSConfig']['keepItems'],
+                       function ($value) {
+                               $parts = explode('|', $value, 2);
+                               return rawurldecode($parts[0]);
+                       }
+               );
+
                // Perform modification of the selected items array:
                foreach ($itemArray as $tk => $tv) {
                        $tvP = explode('|', $tv, 2);
index 5424b44..f5050e4 100755 (executable)
@@ -14,7 +14,12 @@ if (TYPO3_MODE == 'BE') {
 
 if (!function_exists('user_sortPluginList')) {
        function user_sortPluginList(array &$parameters) {
-               usort($parameters['items'], create_function('$item1,$item2', 'return strcasecmp($GLOBALS[\'LANG\']->sL($item1[0]),$GLOBALS[\'LANG\']->sL($item2[0]));'));
+               usort(
+                       $parameters['items'],
+                       function ($item1, $item2) {
+                               return strcasecmp($GLOBALS['LANG']->sL($item1[0]), $GLOBALS['LANG']->sL($item2[0]));
+                       }
+               );
        }
 }
 
index 622fd2d..21d34c9 100644 (file)
@@ -338,7 +338,12 @@ class RelationHandler {
        public function sortList($sortby) {
                // Sort directly without fetching addional data
                if ($sortby == 'uid') {
-                       usort($this->itemArray, create_function('$a,$b', 'return $a["id"] < $b["id"] ? -1 : 1;'));
+                       usort(
+                               $this->itemArray,
+                               function ($a, $b) {
+                                       return $a['id'] < $b['id'] ? -1 : 1;
+                               }
+                       );
                } elseif (count($this->tableArray) == 1) {
                        reset($this->tableArray);
                        $table = key($this->tableArray);
index 6765ae4..fee952b 100644 (file)
@@ -1445,7 +1445,7 @@ class GeneralUtility {
         *
         * @param array $array The initial array to be filtered/reduced
         * @param mixed $keepItems The items which are allowed/kept in the array - accepts array or csv string
-        * @param string $getValueFunc (optional) Unique function name set by create_function() used to get the value to keep
+        * @param string $getValueFunc (optional) Callback function used to get the value to keep
         * @return array The filtered/reduced array with the kept items
         */
        static public function keepItemsInArray(array $array, $keepItems, $getValueFunc = NULL) {
@@ -1454,15 +1454,15 @@ class GeneralUtility {
                        if (is_string($keepItems)) {
                                $keepItems = self::trimExplode(',', $keepItems);
                        }
-                       // create_function() returns a string:
-                       if (!is_string($getValueFunc)) {
+                       // Check if valueFunc can be executed:
+                       if (!is_callable($getValueFunc)) {
                                $getValueFunc = NULL;
                        }
                        // Do the filtering:
                        if (is_array($keepItems) && count($keepItems)) {
                                foreach ($array as $key => $value) {
                                        // Get the value to compare by using the callback function:
-                                       $keepValue = isset($getValueFunc) ? $getValueFunc($value) : $value;
+                                       $keepValue = isset($getValueFunc) ? call_user_func($getValueFunc, $value) : $value;
                                        if (!in_array($keepValue, $keepItems)) {
                                                unset($array[$key]);
                                        }
index 4a89ffb..b813c9b 100644 (file)
@@ -1181,6 +1181,30 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertEquals($expected, $match);
        }
 
+       /**
+        * Similar to keepItemsInArrayCanUseCallbackOnSearchArray(),
+        * but uses a closure instead of create_function()
+        *
+        * @test
+        */
+       public function keepItemsInArrayCanUseClosure() {
+               $array = array(
+                       'aa' => array('first', 'second'),
+                       'bb' => array('third', 'fourth'),
+                       'cc' => array('fifth', 'sixth')
+               );
+               $expected = array('bb' => array('third', 'fourth'));
+               $keepItems = 'third';
+               $match = Utility\GeneralUtility::keepItemsInArray(
+                       $array,
+                       $keepItems,
+                       function ($value) {
+                               return $value[0];
+                       }
+               );
+               $this->assertEquals($expected, $match);
+       }
+
        //////////////////////////////////
        // Tests concerning implodeArrayForUrl / explodeUrl2Array
        //////////////////////////////////