Completed
Pull Request — master (#19)
by Oskar
31:52
created

NonStringableObject::fromAttemptedToString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dont\Exception;
6
7
use LogicException;
8
9
class NonStringableObject extends LogicException implements ExceptionInterface
10
{
11
    private const ERROR_TEMPLATE = <<<'ERROR'
12
The given object %s#%s is not designed to be stringable.
13
14
You tried to access a method called "__toString()".
15
16
This error is raised because you cannot throw exceptions in "__toString()".
17
ERROR;
18
19
    /**
20
     * @param object $object
21
     *
22
     * @return NonStringableObject
23
     *
24
     * @throws TypeError
25
     */
26
    public static function fromAttemptedToString($object) : self
27
    {
28
        if (! is_object($object)) {
29
            throw TypeError::fromNonObject($object);
30
        }
31
32
        $className = get_class($object);
33
34
        return new self(sprintf(
35
            self::ERROR_TEMPLATE,
36
            $className,
37
            spl_object_hash($object)
38
        ));
39
    }
40
}
41