[BUGFIX] Replace calls to the deprecated PHP function each() 10/55510/20
authorReiner Teubner <rteubner@me.com>
Wed, 31 Jan 2018 14:30:21 +0000 (15:30 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 6 Feb 2018 10:18:46 +0000 (11:18 +0100)
This patch replaces the calls to the PHP function each() as it is
marked as deprecated in PHP 7.2. Additionally it adds unit tests for
QueryGenerator::getSubscript().

Resolves: #83737
Releases: master, 8.7, 7.6
Change-Id: Ie61a6d44fcdbd4ce6105a6c185085a3a68866fd8
Reviewed-on: https://review.typo3.org/55510
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Reiner Teubner <rteubner@me.com>
Tested-by: Reiner Teubner <rteubner@me.com>
Reviewed-by: Jan Helke <typo3@helke.de>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Alexander Opitz <opitz.alexander@googlemail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Tree/View/AbstractTreeView.php
typo3/sysext/core/Classes/Database/QueryGenerator.php
typo3/sysext/core/Tests/Unit/Database/QueryGeneratorTest.php [new file with mode: 0644]
typo3/sysext/workspaces/Classes/Service/StagesService.php

index f5eef50..2f520a1 100644 (file)
@@ -960,7 +960,9 @@ abstract class AbstractTreeView
             if ($res < 0) {
                 $row = false;
             } else {
-                list(, $row) = each($this->dataLookup[$res][$this->subLevelID]);
+                $key = key($this->dataLookup[$res][$this->subLevelID]);
+                next($this->dataLookup[$res][$this->subLevelID]);
+                $row = $this->dataLookup[$res][$this->subLevelID][$key];
             }
             return $row;
         }
index 39ef793..f0a7ca3 100644 (file)
@@ -1214,7 +1214,7 @@ class QueryGenerator
         $retArr = [];
         while (is_array($arr)) {
             reset($arr);
-            list($key, ) = each($arr);
+            $key = key($arr);
             $retArr[] = $key;
             $arr = $arr[$key];
         }
diff --git a/typo3/sysext/core/Tests/Unit/Database/QueryGeneratorTest.php b/typo3/sysext/core/Tests/Unit/Database/QueryGeneratorTest.php
new file mode 100644 (file)
index 0000000..8240066
--- /dev/null
@@ -0,0 +1,122 @@
+<?php
+declare(strict_types = 1);
+namespace TYPO3\CMS\Core\Tests\Unit\Database;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Database\QueryGenerator;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
+/**
+ * Test case
+ */
+class QueryGeneratorTest extends UnitTestCase
+{
+    /**
+     * @return array
+     */
+    public function getSubscriptReturnsExpectedValuesDataProvider(): array
+    {
+        return [
+            'multidimensional array input' => [
+                [
+                    'foo' => [
+                        'bar' => 1,
+                        'baz' => [
+                            'jane' => 1,
+                            'john' => 'doe',
+                        ],
+                        'fae' => 1,
+                    ],
+                    'don' => [
+                        'dan' => 1,
+                        'jim' => [
+                            'jon' => 1,
+                            'jin' => 'joh',
+                        ],
+                    ],
+                    'one' => [
+                        'two' => 1,
+                        'three' => [
+                            'four' => 1,
+                            'five' =>'six',
+                        ],
+                    ]
+                ],
+                [
+                    0 => 'foo',
+                    1 => 'bar',
+                ],
+            ],
+            'array with multiple entries input' => [
+                [
+                    'foo' => 1,
+                    'bar' => 2,
+                    'baz' => 3,
+                    'don' => 4,
+                ],
+                [
+                    0 => 'foo',
+                ],
+            ],
+            'array with one entry input' => [
+                [
+                    'foo' => 'bar',
+                ],
+                [
+                    0 => 'foo',
+                ],
+            ],
+            'empty array input' => [
+                [],
+                [
+                    0 => null,
+                ],
+            ],
+            'empty multidimensional array input' => [
+                [[[[]]], [[]], [[]]],
+                [
+                    0 => 0,
+                    1 => 0,
+                    2 => 0,
+                    3 => null,
+                ],
+            ],
+            'null input' => [
+                null,
+                [],
+            ],
+            'string input' => [
+                'foo bar',
+                [],
+            ],
+            'numeric input' => [
+                3.14,
+                [],
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider getSubscriptReturnsExpectedValuesDataProvider
+     * @param $input
+     * @param array $expectedArray
+     */
+    public function getSubscriptReturnsExpectedValues($input, array $expectedArray)
+    {
+        $subject = new QueryGenerator();
+        $this->assertSame($expectedArray, $subject->getSubscript($input));
+    }
+}
index 6d7855d..74efd55 100644 (file)
@@ -626,11 +626,11 @@ class StagesService implements \TYPO3\CMS\Core\SingletonInterface
                     if (trim($row['subgroup'])) {
                         // Make integer list
                         $theList = implode(',', GeneralUtility::intExplode(',', $row['subgroup']));
-                        // Get the subarray
-                        $subbarray = $this->fetchGroups($theList, $idList . ',' . $uid);
-                        list($subUid, $subArray) = each($subbarray);
-                        // Merge the subarray to the already existing userGroups array
-                        $this->userGroups[$subUid] = $subArray;
+                        // Get the subgroups
+                        $subGroups = $this->fetchGroups($theList, $idList . ',' . $uid);
+                        // Merge the subgroups to the already existing userGroups array
+                        $subUid = key($subGroups);
+                        $this->userGroups[$subUid] = $subGroups[$subUid];
                     }
                 }
             }