From a18977abaf61932c2e71283d8788a52606d59fdd Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 20 Mar 2021 23:13:02 +0100 Subject: [PATCH 1/3] FilteredIterator: fix PHP 5.2 compatibility and stabilize the code The `$callback` passed to the constructor of the `FilteredIterator` class may not be callable and if so, would cause a fatal error. This puts some defensive coding in place to guard against that and adds a unit test for it as well. This also fixes the tests failing on PHP 5.2, as when the object is unserialized on PHP 5.2, the `$callback` property will no longer be set. This looks to have been a bug in PHP 5.2 which was fixed in PHP 5.3. --- library/Requests/Utility/FilteredIterator.php | 6 +++++- tests/Utility/FilteredIterator.php | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/library/Requests/Utility/FilteredIterator.php b/library/Requests/Utility/FilteredIterator.php index 04700cd58..66ab323ee 100644 --- a/library/Requests/Utility/FilteredIterator.php +++ b/library/Requests/Utility/FilteredIterator.php @@ -39,7 +39,11 @@ public function __construct($data, $callback) { */ public function current() { $value = parent::current(); - $value = call_user_func($this->callback, $value); + + if (is_callable($this->callback)) { + $value = call_user_func($this->callback, $value); + } + return $value; } diff --git a/tests/Utility/FilteredIterator.php b/tests/Utility/FilteredIterator.php index 5e833ccf2..ebec00c28 100644 --- a/tests/Utility/FilteredIterator.php +++ b/tests/Utility/FilteredIterator.php @@ -27,6 +27,7 @@ public function dataSerializeDeserializeObjects() { return array( array(new Requests_Utility_FilteredIterator(array(1), 'md5')), array(new Requests_Utility_FilteredIterator(array(1, 2), 'sha1')), + array(new Requests_Utility_FilteredIterator(array(1, 2, 3), 'doesnotexist')), array(new ArrayIterator(array(1, 2, 3))), ); } From c93e0c12b563c62d754a48c3c28df21107be2d5a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 20 Mar 2021 00:40:39 +0100 Subject: [PATCH 2/3] Tests/FilteredIterator: fix a parse error on PHP 5.3 Even though the code is wrapped in a version check for running, the code still needs to parse correctly on low PHP versions. This was not the case now, which would break the running of the tests of PHP 5.3. Fixed now. --- tests/Utility/FilteredIterator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Utility/FilteredIterator.php b/tests/Utility/FilteredIterator.php index ebec00c28..03df70e29 100644 --- a/tests/Utility/FilteredIterator.php +++ b/tests/Utility/FilteredIterator.php @@ -9,8 +9,8 @@ public function testDeserializeRequestUtilityFilteredIteratorObjects($value) { if (get_class($value) === 'Requests_Utility_FilteredIterator') { $new_value = unserialize($serialized); if (version_compare(PHP_VERSION, '5.3', '>=')) { - // phpcs:ignore PHPCompatibility.Syntax.NewClassMemberAccess.OnNewFound -- Wrapped in version check. - $property = (new ReflectionClass('Requests_Utility_FilteredIterator'))->getProperty('callback'); + $reflection = new ReflectionClass('Requests_Utility_FilteredIterator'); + $property = $reflection->getProperty('callback'); $property->setAccessible(true); $callback_value = $property->getValue($new_value); $this->assertSame(null, $callback_value); From 36217558904cee91907dbf9d876b0556a7789117 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 20 Mar 2021 20:09:21 +0100 Subject: [PATCH 3/3] Tests: actually run the test for the FilteredIterator As all tests are explicitly named in the test configuration, the test for the `FilteredIterator` as added in 422 were not being run as they hadn't been added to the config. This fixes that. --- tests/phpunit.xml.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit.xml.dist b/tests/phpunit.xml.dist index 7f8186c7e..c29b6a5e6 100644 --- a/tests/phpunit.xml.dist +++ b/tests/phpunit.xml.dist @@ -27,6 +27,7 @@ Response/Headers.php Session.php SSL.php + Utility/FilteredIterator.php