Completed
Pull Request — master (#1)
by Marco
02:28
created

NonSerialisableObject   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 36
rs 10
c 0
b 0
f 0

1 Method

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