From bf87ad4fcfcb6fd653441ed0fe6a8e99c27a9a3e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 31 Jul 2018 03:24:26 +0200 Subject: [PATCH] Fixed E_NOTICE in listing worker queue and new utilities class added (#5521) * Fixes for E_NOTICE in workqueue: - introduced class `Friendica\Util\Arrays` which will hold static methods for handling arrays that cannot be done with PHP's functions, like implode() on multi-dimensional arrays - rewrote old-school for() loop to foreach() * Added intial unit test with some tests on empty delimiters and/or sinle and multi-dim array. * Added test for for 3-dimensional arrays, thanks to nupplaphil's feedback. --- mod/admin.php | 5 +- src/Util/Arrays.php | 49 +++++++++++++++ tests/src/Util/ArraysTest.php | 114 ++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 src/Util/Arrays.php create mode 100644 tests/src/Util/ArraysTest.php diff --git a/mod/admin.php b/mod/admin.php index fae20f79bd..09a04ae02c 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -21,6 +21,7 @@ use Friendica\Model\Item; use Friendica\Model\User; use Friendica\Module\Login; use Friendica\Module\Tos; +use Friendica\Util\Arrays; use Friendica\Util\DateTimeFormat; use Friendica\Util\Temporal; @@ -783,9 +784,9 @@ function admin_page_workerqueue(App $a) $statement = DBA::select('workerqueue', ['id', 'parameter', 'created', 'priority'], ['done' => 0], ['order'=> ['priority']]); $r = DBA::toArray($statement); - for($i = 0; $i < count($r); $i++) { + foreach ($r as $key => $rr) { // fix GH-5469. ref: src/Core/Worker.php:217 - $r[$i]['parameter'] = implode(json_decode($r[$i]['parameter'], true), ': '); + $r[$key]['parameter'] = Arrays::recursiveImplode(json_decode($rr['parameter'], true), ': '); } $t = get_markup_template('admin/workerqueue.tpl'); diff --git a/src/Util/Arrays.php b/src/Util/Arrays.php new file mode 100644 index 0000000000..b17dc8bfe9 --- /dev/null +++ b/src/Util/Arrays.php @@ -0,0 +1,49 @@ + + */ +namespace Friendica\Util; + +/** + * @brief Array utility class + */ +class Arrays +{ + /** + * @brief Private constructor + */ + private function __construct () { + // Utitlities don't have instances + } + + /** + * @briefs Implodes recursively a multi-dimensional array where a normal implode() will fail. + * + * @param array $array Array to implode + * @param string $glue Glue for imploded elements + * @return string String with elements from array + */ + public static function recursiveImplode (array $array, $glue) { + // Init returned string + $string = ''; + + // Loop through all records + foreach ($array as $element) { + // Is an array found? + if (is_array($element)) { + // Invoke cursively + $string .= '{' . self::recursiveImplode($element, $glue) . '}' . $glue; + } else { + // Append normally + $string .= $element . $glue; + } + } + + // Remove last glue + $string = trim($string, $glue); + + // Return it + return $string; + } +} diff --git a/tests/src/Util/ArraysTest.php b/tests/src/Util/ArraysTest.php new file mode 100644 index 0000000000..2028cfc90e --- /dev/null +++ b/tests/src/Util/ArraysTest.php @@ -0,0 +1,114 @@ + + */ +namespace Friendica\Test\Util; + +use Friendica\Util\Arrays; +use PHPUnit\Framework\TestCase; + +/** + * @brief Array utility testing class + */ +class ArraysTest extends TestCase +{ + /** + * @brief Tests if an empty array and an empty delimiter returns an empty string. + */ + public function testEmptyArrayEmptyDelimiter() + { + $str = Arrays::recursiveImplode([], ''); + $this->assertEmpty($str); + } + + /** + * @brief Tests if an empty array and a non-empty delimiter returns an empty string. + */ + public function testEmptyArrayNonEmptyDelimiter() + { + $str = Arrays::recursiveImplode([], ','); + $this->assertEmpty($str); + } + + /** + * @brief Tests if a non-empty array and an empty delimiter returns the value (1). + */ + public function testNonEmptyArrayEmptyDelimiter() + { + $str = Arrays::recursiveImplode([1], ''); + $this->assertSame($str, '1'); + } + + /** + * @brief Tests if a non-empty array and an empty delimiter returns the value (12). + */ + public function testNonEmptyArray2EmptyDelimiter() + { + $str = Arrays::recursiveImplode([1, 2], ''); + $this->assertSame($str, '12'); + } + + /** + * @brief Tests if a non-empty array and a non-empty delimiter returns the value (1). + */ + public function testNonEmptyArrayNonEmptyDelimiter() + { + $str = Arrays::recursiveImplode([1], ','); + $this->assertSame($str, '1'); + } + + /** + * @brief Tests if a non-empty array and a non-empty delimiter returns the value (1,2). + */ + public function testNonEmptyArray2NonEmptyDelimiter() + { + $str = Arrays::recursiveImplode([1, 2], ','); + $this->assertSame($str, '1,2'); + } + + /** + * @brief Tests if a 2-dim array and an empty delimiter returns the expected string. + */ + public function testEmptyMultiArray2EmptyDelimiter() + { + $str = Arrays::recursiveImplode([[1], []], ''); + $this->assertSame($str, '{1}{}'); + } + + /** + * @brief Tests if a 2-dim array and an empty delimiter returns the expected string. + */ + public function testEmptyMulti2Array2EmptyDelimiter() + { + $str = Arrays::recursiveImplode([[1], [2]], ''); + $this->assertSame($str, '{1}{2}'); + } + + /** + * @brief Tests if a 2-dim array and a non-empty delimiter returns the expected string. + */ + public function testEmptyMultiArray2NonEmptyDelimiter() + { + $str = Arrays::recursiveImplode([[1], []], ','); + $this->assertSame($str, '{1},{}'); + } + + /** + * @brief Tests if a 2-dim array and a non-empty delimiter returns the expected string. + */ + public function testEmptyMulti2Array2NonEmptyDelimiter() + { + $str = Arrays::recursiveImplode([[1], [2]], ','); + $this->assertSame($str, '{1},{2}'); + } + + /** + * @brief Tests if a 3-dim array and a non-empty delimiter returns the expected string. + */ + public function testEmptyMulti3Array2NonEmptyDelimiter() + { + $str = Arrays::recursiveImplode([[1], [2, [3]]], ','); + $this->assertSame($str, '{1},{2,{3}}'); + } +} -- 2.39.5