NotFoundException   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 37
ccs 15
cts 15
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 3
A getBuildStack() 0 3 1
A getId() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di;
6
7
use Exception;
8
use Psr\Container\NotFoundExceptionInterface;
9
use Throwable;
10
11
/**
12
 * `NotFoundException` is thrown when no definition or class was found in the container for a given ID.
13
 */
14
final class NotFoundException extends Exception implements NotFoundExceptionInterface
15
{
16
    /**
17
     * @param string $id ID of the definition or name of the class that was not found.
18
     * @param string[] $buildStack Stack of IDs of services requested definition or class that was not found.
19
     */
20 19
    public function __construct(
21
        private string $id,
22
        private array $buildStack = [],
23
        ?Throwable $previous = null,
24
    ) {
25 19
        if (empty($this->buildStack)) {
26 3
            $message = sprintf('No definition or class found or resolvable for "%s".', $id);
27 16
        } elseif ($this->buildStack === [$id]) {
28 11
            $message = sprintf('No definition or class found or resolvable for "%s" while building it.', $id);
29
        } else {
30 6
            $message = sprintf(
31 6
                'No definition or class found or resolvable for "%s" while building "%s".',
32 6
                end($this->buildStack),
33 6
                implode('" -> "', $buildStack),
34 6
            );
35
        }
36
37 19
        parent::__construct($message, previous: $previous);
38
    }
39
40 12
    public function getId(): string
41
    {
42 12
        return $this->id;
43
    }
44
45
    /**
46
     * @return string[]
47
     */
48 2
    public function getBuildStack(): array
49
    {
50 2
        return $this->buildStack;
51
    }
52
}
53