From 923ce4ca613298da244d82db4667776b7ede47b3 Mon Sep 17 00:00:00 2001
From: Reiner Teubner <rteubner@me.com>
Date: Wed, 31 Jan 2018 15:30:21 +0100
Subject: [PATCH] [BUGFIX] Replace calls to the deprecated PHP function each()

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/55570
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../Classes/Tree/View/AbstractTreeView.php    |   4 +-
 .../core/Classes/Database/QueryGenerator.php  |   2 +-
 .../Unit/Database/QueryGeneratorTest.php      | 121 ++++++++++++++++++
 .../Classes/Service/StagesService.php         |  10 +-
 4 files changed, 130 insertions(+), 7 deletions(-)
 create mode 100644 typo3/sysext/core/Tests/Unit/Database/QueryGeneratorTest.php

diff --git a/typo3/sysext/backend/Classes/Tree/View/AbstractTreeView.php b/typo3/sysext/backend/Classes/Tree/View/AbstractTreeView.php
index 73ebfc461dc5..00f6f4f03e6c 100644
--- a/typo3/sysext/backend/Classes/Tree/View/AbstractTreeView.php
+++ b/typo3/sysext/backend/Classes/Tree/View/AbstractTreeView.php
@@ -941,7 +941,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;
         } else {
diff --git a/typo3/sysext/core/Classes/Database/QueryGenerator.php b/typo3/sysext/core/Classes/Database/QueryGenerator.php
index 1957ccc44b0a..321cc4edf313 100644
--- a/typo3/sysext/core/Classes/Database/QueryGenerator.php
+++ b/typo3/sysext/core/Classes/Database/QueryGenerator.php
@@ -1206,7 +1206,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
index 000000000000..0581c81a9556
--- /dev/null
+++ b/typo3/sysext/core/Tests/Unit/Database/QueryGeneratorTest.php
@@ -0,0 +1,121 @@
+<?php
+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\CMS\Core\Tests\UnitTestCase;
+
+/**
+ * Test case
+ */
+class QueryGeneratorTest extends UnitTestCase
+{
+    /**
+     * @return array
+     */
+    public function getSubscriptReturnsExpectedValuesDataProvider()
+    {
+        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));
+    }
+}
diff --git a/typo3/sysext/workspaces/Classes/Service/StagesService.php b/typo3/sysext/workspaces/Classes/Service/StagesService.php
index 1bb434b7d9de..c96f2c5130a0 100644
--- a/typo3/sysext/workspaces/Classes/Service/StagesService.php
+++ b/typo3/sysext/workspaces/Classes/Service/StagesService.php
@@ -615,11 +615,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];
                     }
                 }
             }
-- 
GitLab