fromAttemptedDeSerialisation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dont\Exception;
6
7
use LogicException;
8
9
class NonDeserialisableObject extends LogicException implements ExceptionInterface
10
{
11
    private const ERROR_TEMPLATE = <<<'ERROR'
12
The given object %s#%s is not designed to be deserialised, yet deserialisation was attempted.
13
14
This error is raised because the author of %s didn't design it to be deserialisable, nor can
15
guarantee that it will function correctly after deserialisation, nor can guarantee that all
16
its internal components are deserialisable.
17
18
Please do not use unserialize() to produce %s instances.
19
ERROR;
20
21 9
    public static function fromAttemptedDeSerialisation($object) : self
22
    {
23 9
        if (! is_object($object)) {
24 7
            throw TypeError::fromNonObject($object);
25
        }
26
27 2
        $className = get_class($object);
28
29 2
        return new self(sprintf(
30 2
            self::ERROR_TEMPLATE,
31 2
            $className,
32 2
            spl_object_hash($object),
33 2
            $className,
34 2
            $className
35
        ));
36
    }
37
}
38