1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the webmozart/key-value-store package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webmozart\KeyValueStore\Api; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use RuntimeException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Thrown when a value cannot be unserialized. |
19
|
|
|
* |
20
|
|
|
* @since 1.0 |
21
|
|
|
* |
22
|
|
|
* @author Bernhard Schussek <[email protected]> |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
class UnserializationFailedException extends RuntimeException |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Creates a new exception for the given value. |
28
|
|
|
* |
29
|
|
|
* @param mixed $value The value that could not be unserialized. |
30
|
|
|
* @param string $reason The reason why the value could not be |
31
|
|
|
* unserialized. |
32
|
|
|
* @param int $code The exception code. |
33
|
|
|
* @param Exception $cause The exception that caused this exception. |
|
|
|
|
34
|
|
|
* |
35
|
|
|
* @return static The new exception. |
36
|
|
|
*/ |
37
|
38 |
|
public static function forValue($value, $reason = '', $code = 0, Exception $cause = null) |
38
|
|
|
{ |
39
|
38 |
|
return self::forType(is_object($value) ? get_class($value) : gettype($value), $reason, $code, $cause); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates a new exception for the given value type. |
44
|
|
|
* |
45
|
|
|
* @param string $type The type that could not be unserialized. |
46
|
|
|
* @param string $reason The reason why the value could not be |
47
|
|
|
* unserialized. |
48
|
|
|
* @param int $code The exception code. |
49
|
|
|
* @param Exception $cause The exception that caused this exception. |
|
|
|
|
50
|
|
|
* |
51
|
|
|
* @return static The new exception. |
52
|
|
|
*/ |
53
|
38 |
|
public static function forType($type, $reason = '', $code = 0, Exception $cause = null) |
54
|
|
|
{ |
55
|
38 |
|
return new static(sprintf( |
56
|
38 |
|
'Could not unserialize value of type %s%s', |
57
|
|
|
$type, |
58
|
38 |
|
$reason ? ': '.$reason : '.' |
59
|
|
|
), $code, $cause); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.