Completed
Push — master ( f96975...e289fc )
by Marco
9s
created

NonSerialisableObject::fromAttemptedSerialisation()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dont\Exception;
6
7
use LogicException;
8
9
class NonSerialisableObject extends LogicException implements ExceptionInterface
10
{
11
    private const ERROR_TEMPLATE = <<<'ERROR'
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_CONST, expecting T_FUNCTION
Loading history...
12
The given object %s#%s is not designed to be serialised, yet serialisation was attempted.
13
14
This error is raised because the author of %s didn't design it to be serialisable, nor can
15
guarantee that it will function correctly after serialisation, nor can guarantee that all
16
its internal components are serialisable.
17
18
Please do not serialise %s instances.
19
ERROR;
20
21
    /**
22
     * @param object $object
23
     *
24
     * @return NonSerialisableObject
25
     *
26
     * @throws TypeError
27
     */
28
    public static function fromAttemptedSerialisation($object) : self
29
    {
30
        if (! is_object($object)) {
31
            throw TypeError::fromNonObject($object);
32
        }
33
34
        $className = get_class($object);
35
36
        return new self(sprintf(
37
            self::ERROR_TEMPLATE,
38
            $className,
39
            spl_object_hash($object),
40
            $className,
41
            $className
42
        ));
43
    }
44
}
45