SerializationFailedException::forValue()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 4
loc 4
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 2
eloc 2
nop 4
crap 2
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 serialized.
19
 *
20
 * @since  1.0
21
 *
22
 * @author Bernhard Schussek <[email protected]>
23
 */
24 View Code Duplication
class SerializationFailedException extends RuntimeException
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
25
{
26
    /**
27
     * Creates a new exception for the given value.
28
     *
29
     * @param mixed     $value  The value that could not be serialized.
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.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cause not be null|Exception?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
34
     *
35
     * @return static The new exception.
36
     */
37 16
    public static function forValue($value, $reason = '', $code = 0, Exception $cause = null)
38
    {
39 16
        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 serialized.
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.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cause not be null|Exception?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
50
     *
51
     * @return static The new exception.
52
     */
53 16
    public static function forType($type, $reason = '', $code = 0, Exception $cause = null)
54
    {
55 16
        return new static(sprintf(
56 16
            'Could not serialize value of type %s%s',
57
            $type,
58 16
            $reason ? ': '.$reason : '.'
59
        ), $code, $cause);
60
    }
61
}
62