Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 4e2655f

Browse files
committed
Review and refactoring of #37
- Updates license docblocks of changed files to reflect current standards. - Use a ternary to get the return value of the `unserialize()` operation (simpler to read than if/else block). - No fluent operations in new classes. - Review all docblocks for formatting, grammar. - Use annotations to require PHP 7 for tests - split out a single test to demonstrate expected behavior in versions prior to 7.0
1 parent 643e8a6 commit 4e2655f

File tree

3 files changed

+58
-71
lines changed

3 files changed

+58
-71
lines changed

src/Adapter/PhpSerialize.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22
/**
3-
* Zend Framework (http://framework.zend.com/)
4-
*
5-
* @link http://github.com/zendframework/zf2 for the canonical source repository
6-
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
7-
* @license http://framework.zend.com/license/new-bsd New BSD License
3+
* @see https://github.com/zendframework/zend-serializer for the canonical source repository
4+
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-serializer/blob/master/LICENSE.md New BSD License
86
*/
97

108
namespace Zend\Serializer\Adapter;
@@ -124,12 +122,10 @@ public function unserialize($serialized)
124122

125123
ErrorHandler::start(E_NOTICE);
126124

127-
if (PHP_MAJOR_VERSION >= 7) {
128-
// the second parameter is only available on PHP 7.0 or higher
129-
$ret = unserialize($serialized, ['allowed_classes' => $this->getOptions()->getUnserializeClassWhitelist()]);
130-
} else {
131-
$ret = unserialize($serialized);
132-
}
125+
// The second parameter to unserialize() is only available on PHP 7.0 or higher
126+
$ret = PHP_MAJOR_VERSION >= 7
127+
? unserialize($serialized, ['allowed_classes' => $this->getOptions()->getUnserializeClassWhitelist()])
128+
: unserialize($serialized);
133129

134130
$err = ErrorHandler::stop();
135131
if ($ret === false) {

src/Adapter/PhpSerializeOptions.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22
/**
3-
* Zend Framework (http://framework.zend.com/)
4-
*
5-
* @link http://github.com/zendframework/zf2 for the canonical source repository
6-
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
7-
* @license http://framework.zend.com/license/new-bsd New BSD License
3+
* @see https://github.com/zendframework/zend-serializer for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-serializer/blob/master/LICENSE.md New BSD License
86
*/
97

108
namespace Zend\Serializer\Adapter;
@@ -15,31 +13,31 @@
1513
class PhpSerializeOptions extends AdapterOptions
1614
{
1715
/**
18-
* The list of allowed classes for unserialization (PHP 7.0+)
16+
* The list of allowed classes for unserialization (PHP 7.0+).
17+
*
1918
* Possible values:
20-
* Array of class names that are allowed to be unserialized
21-
* or true if all classes should be allowed (behavior of pre PHP 7.0)
22-
* or false if no classes should be allowed
19+
*
20+
* - `array` of class names that are allowed to be unserialized
21+
* - `true` if all classes should be allowed (behavior pre-PHP 7.0)
22+
* - `false` if no classes should be allowed
2323
*
2424
* @var string[]|bool
2525
*/
2626
protected $unserializeClassWhitelist = true;
2727

2828
/**
29-
* @param string[]|bool $unserializeClassWhitelist
30-
*
31-
* @return PhpSerializeOptions
29+
* @param string[]|bool $unserializeClassWhitelist
30+
* @return void
3231
*/
3332
public function setUnserializeClassWhitelist($unserializeClassWhitelist)
3433
{
35-
if (($unserializeClassWhitelist !== true) && (PHP_MAJOR_VERSION < 7)) {
34+
if ($unserializeClassWhitelist !== true && PHP_MAJOR_VERSION < 7) {
3635
throw new Exception\InvalidArgumentException(
37-
'Class whitelist for unserialize() is only available on PHP 7.0 or higher.'
36+
'Class whitelist for unserialize() is only available on PHP versions 7.0 or higher.'
3837
);
3938
}
4039

4140
$this->unserializeClassWhitelist = $unserializeClassWhitelist;
42-
return $this;
4341
}
4442

4543
/**

test/Adapter/PhpSerializeTest.php

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<?php
22
/**
3-
* Zend Framework (http://framework.zend.com/)
4-
*
5-
* @link http://github.com/zendframework/zf2 for the canonical source repository
6-
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7-
* @license http://framework.zend.com/license/new-bsd New BSD License
3+
* @see https://github.com/zendframework/zend-serializer for the canonical source repository
4+
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-serializer/blob/master/LICENSE.md New BSD License
86
*/
97

108
namespace ZendTest\Serializer\Adapter;
119

1210
use PHPUnit\Framework\TestCase;
11+
use stdClass;
1312
use Zend\Serializer;
1413
use Zend\Serializer\Exception\InvalidArgumentException;
1514

@@ -167,65 +166,59 @@ public function testUnserializingInvalidStringRaisesException($string, $expected
167166
$this->adapter->unserialize($string);
168167
}
169168

170-
public function testUnserializeNoWhitelistedClasses()
169+
/**
170+
* @requires PHP 7.0
171+
*/
172+
public function testPhp7WillNotUnserializeObjectsWhenUnserializeWhitelistedClassesIsFalse()
171173
{
172174
$value = 'O:8:"stdClass":0:{}';
175+
$this->adapter->getOptions()->setUnserializeClassWhitelist(false);
173176

174-
if (PHP_MAJOR_VERSION >= 7) {
175-
$this->adapter->getOptions()->setUnserializeClassWhitelist(false);
176-
177-
$data = $this->adapter->unserialize($value);
178-
179-
$this->assertNotInstanceOf(\stdClass::class, $data);
180-
$this->assertInstanceOf('__PHP_Incomplete_Class', $data);
181-
} else {
182-
// In PHP < 7.0 the options-class will throw an exception
183-
184-
self::expectException(InvalidArgumentException::class);
185-
self::expectExceptionMessage('Class whitelist for unserialize() is only available on PHP 7.0 or higher.');
177+
$data = $this->adapter->unserialize($value);
186178

187-
$this->adapter->getOptions()->setUnserializeClassWhitelist(false);
188-
}
179+
$this->assertNotInstanceOf(stdClass::class, $data);
180+
$this->assertInstanceOf('__PHP_Incomplete_Class', $data);
189181
}
190182

191-
public function testUnserializeClassNotAllowed()
183+
public function testWhenUnserializeClassWhiteListIsFalseButPHPIsPriorTo7AnExceptionIsRaised()
192184
{
193185
$value = 'O:8:"stdClass":0:{}';
194186

195187
if (PHP_MAJOR_VERSION >= 7) {
196-
$this->adapter->getOptions()->setUnserializeClassWhitelist([\My\Dummy::class]);
188+
$this->markTestSkipped(sprintf('Test %s is only needed in PHP versions prior to 7.0', __FUNCTION__));
189+
}
197190

198-
$data = $this->adapter->unserialize($value);
191+
self::expectException(InvalidArgumentException::class);
192+
self::expectExceptionMessage('Class whitelist for unserialize() is only available on PHP 7.0 or higher.');
193+
$this->adapter->getOptions()->setUnserializeClassWhitelist(false);
194+
}
195+
196+
/**
197+
* @requires PHP 7.0
198+
*/
199+
public function testUnserializeWillNotUnserializeClassesThatAreNotInTheWhitelist()
200+
{
201+
$value = 'O:8:"stdClass":0:{}';
199202

200-
$this->assertNotInstanceOf(\stdClass::class, $data);
201-
$this->assertInstanceOf('__PHP_Incomplete_Class', $data);
202-
} else {
203-
// In PHP < 7.0 the options-class will throw an exception
203+
$this->adapter->getOptions()->setUnserializeClassWhitelist([\My\Dummy::class]);
204204

205-
self::expectException(InvalidArgumentException::class);
206-
self::expectExceptionMessage('Class whitelist for unserialize() is only available on PHP 7.0 or higher.');
205+
$data = $this->adapter->unserialize($value);
207206

208-
$this->adapter->getOptions()->setUnserializeClassWhitelist(false);
209-
}
207+
$this->assertNotInstanceOf(stdClass::class, $data);
208+
$this->assertInstanceOf('__PHP_Incomplete_Class', $data);
210209
}
211210

212-
public function testUnserializeClassAllowed()
211+
/**
212+
* @requires PHP 7.0
213+
*/
214+
public function testUnserializeWillUnserializeAnyClassWhenUnserializeWhitelistedClassesIsTrue()
213215
{
214216
$value = 'O:8:"stdClass":0:{}';
215217

216-
if (PHP_MAJOR_VERSION >= 7) {
217-
$this->adapter->getOptions()->setUnserializeClassWhitelist([\stdClass::class]);
218-
219-
$data = $this->adapter->unserialize($value);
220-
$this->assertInstanceOf(\stdClass::class, $data);
221-
$this->assertNotInstanceOf('__PHP_Incomplete_Class', $data);
222-
} else {
223-
// In PHP < 7.0 the options-class will throw an exception
218+
$this->adapter->getOptions()->setUnserializeClassWhitelist([stdClass::class]);
224219

225-
self::expectException(InvalidArgumentException::class);
226-
self::expectExceptionMessage('Class whitelist for unserialize() is only available on PHP 7.0 or higher.');
227-
228-
$this->adapter->getOptions()->setUnserializeClassWhitelist(false);
229-
}
220+
$data = $this->adapter->unserialize($value);
221+
$this->assertInstanceOf(stdClass::class, $data);
222+
$this->assertNotInstanceOf('__PHP_Incomplete_Class', $data);
230223
}
231224
}

0 commit comments

Comments
 (0)