NonCloneableObject   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromAttemptedCloning() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dont\Exception;
6
7
use LogicException;
8
9
class NonCloneableObject extends LogicException implements ExceptionInterface
10
{
11
    private const ERROR_TEMPLATE = <<<'ERROR'
12
The given object %s#%s is not designed to be cloned, yet cloning was attempted.
13
14
This error is raised because the author of %s didn't design it to be cloneable, nor can
15
guarantee that it will function correctly after cloning, nor can guarantee that all
16
its internal components are cloneable.
17
18
Please do not clone %s instances.
19
ERROR;
20
21
    /**
22
     * @param object $object
23
     *
24
     * @return NonCloneableObject
25
     *
26
     * @throws TypeError
27
     */
28 9
    public static function fromAttemptedCloning($object) : self
29
    {
30 9
        if (! is_object($object)) {
31 7
            throw TypeError::fromNonObject($object);
32
        }
33
34 2
        $className = get_class($object);
35
36 2
        return new self(sprintf(
37 2
            self::ERROR_TEMPLATE,
38 2
            $className,
39 2
            spl_object_hash($object),
40 2
            $className,
41 2
            $className
42
        ));
43
    }
44
}
45